@@ -7,16 +7,16 @@ namespace SharpNeat.Tasks.Gymnasium;
77
88public sealed class GymnasiumEpisode
99{
10- readonly int _inputCount ;
11- readonly int _outputCount ;
12- readonly bool _isContinious ;
13- readonly bool _test ;
10+ private readonly int _inputCount ;
11+ private readonly int _outputCount ;
12+ private readonly bool _isContinuous ;
13+ private readonly bool _test ;
1414
15- public GymnasiumEpisode ( int inputCount , int outputCount , bool isContinious , bool test )
15+ public GymnasiumEpisode ( int inputCount , int outputCount , bool isContinuous , bool test )
1616 {
1717 _inputCount = inputCount ;
1818 _outputCount = outputCount ;
19- _isContinious = isContinious ;
19+ _isContinuous = isContinuous ;
2020 _test = test ;
2121 }
2222
@@ -28,12 +28,12 @@ public FitnessInfo Evaluate(IBlackBox<double> phenome)
2828 {
2929 FileName = @"pythonw.exe" ,
3030 WorkingDirectory = @"./" ,
31- Arguments = string . Format ( CultureInfo . InvariantCulture , @"gymnasium/main.py -uuid {0} -render {1}" , uuid . ToString ( ) , _test ) ,
31+ Arguments = string . Format ( CultureInfo . InvariantCulture , @"gymnasium/main.py -uuid {0} -render {1} -test False " , uuid . ToString ( ) , _test ) ,
3232 UseShellExecute = false ,
3333 RedirectStandardOutput = false
3434 } ;
3535
36- var process = Process . Start ( start ) ?? throw new InvalidOperationException ( "No proccess resource is started" ) ;
36+ var process = Process . Start ( start ) ?? throw new InvalidOperationException ( "No process resource is started" ) ;
3737 var totalReward = 0.0 ;
3838
3939 try
@@ -52,10 +52,9 @@ public FitnessInfo Evaluate(IBlackBox<double> phenome)
5252 var inputs = phenome . Inputs . Span ;
5353 inputs . Clear ( ) ;
5454
55- var observationTuple = ReadObservation ( namedPipeClientStream , _inputCount ) ;
56- var observation = observationTuple . observation ;
57- totalReward = observationTuple . reward [ 0 ] ;
58- var done = observationTuple . done [ 0 ] ;
55+ var ( observation , rewardArray , doneArray ) = ReadObservation ( namedPipeClientStream , _inputCount ) ;
56+ totalReward = rewardArray [ 0 ] ;
57+ var done = doneArray [ 0 ] ;
5958
6059 if ( done == 1 )
6160 {
@@ -66,7 +65,7 @@ public FitnessInfo Evaluate(IBlackBox<double> phenome)
6665 phenome . Activate ( ) ;
6766
6867 // var clampedOutputs = outputs.Select(output => Math.Clamp(output, -1.0, 1.0)).ToArray();
69- if ( _isContinious )
68+ if ( _isContinuous )
7069 {
7170 var outputBuffer = new byte [ _outputCount * sizeof ( float ) ] ;
7271 var outputs = new double [ _outputCount ] ;
@@ -76,7 +75,7 @@ public FitnessInfo Evaluate(IBlackBox<double> phenome)
7675 }
7776 else
7877 {
79- int maxSigIndex = ReadMaxSigIndex ( phenome ) ;
78+ var maxSigIndex = ReadMaxSigIndex ( phenome ) ;
8079 var outputBuffer = new byte [ sizeof ( int ) ] ;
8180 Buffer . BlockCopy ( new int [ ] { maxSigIndex } , 0 , outputBuffer , 0 , outputBuffer . Length ) ;
8281 namedPipeClientStream . Write ( outputBuffer , 0 , outputBuffer . Length ) ;
@@ -101,16 +100,16 @@ public FitnessInfo Evaluate(IBlackBox<double> phenome)
101100 return new FitnessInfo ( maskedReward ) ;
102101 }
103102
104- static ( double [ ] observation , double [ ] reward , int [ ] done ) ReadObservation ( NamedPipeClientStream namedPipeClientStream , int count )
103+ private static ( double [ ] observation , double [ ] reward , int [ ] done ) ReadObservation ( Stream namedPipeClientStream , int count )
105104 {
106105 var count0 = count * sizeof ( double ) ;
107- var count1 = sizeof ( double ) ;
108- var count2 = sizeof ( int ) ;
106+ const int count1 = sizeof ( double ) ;
107+ const int count2 = sizeof ( int ) ;
109108 var inputBuffer = new byte [ count0 + count1 + count2 ] ;
110109 namedPipeClientStream . Read ( inputBuffer , 0 , inputBuffer . Length ) ;
111- double [ ] observation = new double [ count ] ;
112- double [ ] reward = new double [ 1 ] ;
113- int [ ] done = new int [ 1 ] ;
110+ var observation = new double [ count ] ;
111+ var reward = new double [ 1 ] ;
112+ var done = new int [ 1 ] ;
114113 var offset1 = count0 ;
115114 var offset2 = count0 + count1 ;
116115 Buffer . BlockCopy ( inputBuffer , 0 , observation , 0 , count0 ) ;
@@ -119,46 +118,44 @@ public FitnessInfo Evaluate(IBlackBox<double> phenome)
119118 return ( observation , reward , done ) ;
120119 }
121120
122- static double [ ] ReadDoubleArray ( NamedPipeClientStream namedPipeClientStream , int count )
121+ private static double [ ] ReadDoubleArray ( Stream namedPipeClientStream , int count )
123122 {
124123 var inputBuffer = new byte [ count * sizeof ( double ) ] ;
125124 namedPipeClientStream . Read ( inputBuffer , 0 , inputBuffer . Length ) ;
126- double [ ] values = new double [ inputBuffer . Length / sizeof ( double ) ] ;
125+ var values = new double [ inputBuffer . Length / sizeof ( double ) ] ;
127126 Buffer . BlockCopy ( inputBuffer , 0 , values , 0 , values . Length * sizeof ( double ) ) ;
128127 return values ;
129128 }
130129
131- static float [ ] ReadFloatArray ( NamedPipeClientStream namedPipeClientStream , int count )
130+ private static float [ ] ReadFloatArray ( Stream namedPipeClientStream , int count )
132131 {
133132 var inputBuffer = new byte [ count * sizeof ( float ) ] ;
134133 namedPipeClientStream . Read ( inputBuffer , 0 , inputBuffer . Length ) ;
135- float [ ] values = new float [ inputBuffer . Length / sizeof ( float ) ] ;
134+ var values = new float [ inputBuffer . Length / sizeof ( float ) ] ;
136135 Buffer . BlockCopy ( inputBuffer , 0 , values , 0 , values . Length * sizeof ( float ) ) ;
137136 return values ;
138137 }
139138
140- static int [ ] ReadIntArray ( NamedPipeClientStream namedPipeClientStream , int count )
139+ private static int [ ] ReadIntArray ( Stream namedPipeClientStream , int count )
141140 {
142141 var inputBuffer = new byte [ count * sizeof ( int ) ] ;
143142 namedPipeClientStream . Read ( inputBuffer , 0 , inputBuffer . Length ) ;
144- int [ ] values = new int [ inputBuffer . Length / sizeof ( int ) ] ;
143+ var values = new int [ inputBuffer . Length / sizeof ( int ) ] ;
145144 Buffer . BlockCopy ( inputBuffer , 0 , values , 0 , values . Length * sizeof ( int ) ) ;
146145 return values ;
147146 }
148147
149- int ReadMaxSigIndex ( IBlackBox < double > phenome )
148+ private int ReadMaxSigIndex ( IBlackBox < double > phenome )
150149 {
151- double maxSig = phenome . Outputs . Span [ 0 ] ;
152- int maxSigIdx = 0 ;
150+ var maxSig = phenome . Outputs . Span [ 0 ] ;
151+ var maxSigIdx = 0 ;
153152
154- for ( int i = 1 ; i < _outputCount ; i ++ )
153+ for ( var i = 1 ; i < _outputCount ; i ++ )
155154 {
156- double v = phenome . Outputs . Span [ i ] ;
157- if ( v > maxSig )
158- {
159- maxSig = v ;
160- maxSigIdx = i ;
161- }
155+ var v = phenome . Outputs . Span [ i ] ;
156+ if ( ! ( v > maxSig ) ) continue ;
157+ maxSig = v ;
158+ maxSigIdx = i ;
162159 }
163160
164161 return maxSigIdx ;
0 commit comments