@@ -7,33 +7,42 @@ 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
2323 public FitnessInfo Evaluate ( IBlackBox < double > phenome )
2424 {
2525 var uuid = Guid . NewGuid ( ) ;
2626
27+ // var start = new ProcessStartInfo
28+ // {
29+ // FileName = @"pythonw.exe",
30+ // WorkingDirectory = @"./",
31+ // Arguments = string.Format(CultureInfo.InvariantCulture, @"gymnasium/main.py -uuid {0} -render {1} -test False", uuid.ToString(), _test),
32+ // UseShellExecute = false,
33+ // RedirectStandardOutput = false
34+ // };
35+
2736 var start = new ProcessStartInfo
2837 {
29- FileName = @"pythonw .exe" ,
38+ FileName = @"D:\projects\sharpneat-fork\src\SharpNeat.Windows.App\gymnasium\main .exe" ,
3039 WorkingDirectory = @"./" ,
31- Arguments = string . Format ( CultureInfo . InvariantCulture , @"gymnasium/main.py -uuid {0} -render {1}" , uuid . ToString ( ) , _test ) ,
40+ Arguments = string . Format ( CultureInfo . InvariantCulture , @"-uuid {0} -render {1} -test False " , uuid . ToString ( ) , _test ) ,
3241 UseShellExecute = false ,
3342 RedirectStandardOutput = false
3443 } ;
3544
36- var process = Process . Start ( start ) ?? throw new InvalidOperationException ( "No proccess resource is started" ) ;
45+ var process = Process . Start ( start ) ?? throw new InvalidOperationException ( "No process resource is started" ) ;
3746 var totalReward = 0.0 ;
3847
3948 try
@@ -52,10 +61,9 @@ public FitnessInfo Evaluate(IBlackBox<double> phenome)
5261 var inputs = phenome . Inputs . Span ;
5362 inputs . Clear ( ) ;
5463
55- var observationTuple = ReadObservation ( namedPipeClientStream , _inputCount ) ;
56- var observation = observationTuple . observation ;
57- totalReward = observationTuple . reward [ 0 ] ;
58- var done = observationTuple . done [ 0 ] ;
64+ var ( observation , rewardArray , doneArray ) = ReadObservation ( namedPipeClientStream , _inputCount ) ;
65+ totalReward = rewardArray [ 0 ] ;
66+ var done = doneArray [ 0 ] ;
5967
6068 if ( done == 1 )
6169 {
@@ -66,7 +74,7 @@ public FitnessInfo Evaluate(IBlackBox<double> phenome)
6674 phenome . Activate ( ) ;
6775
6876 // var clampedOutputs = outputs.Select(output => Math.Clamp(output, -1.0, 1.0)).ToArray();
69- if ( _isContinious )
77+ if ( _isContinuous )
7078 {
7179 var outputBuffer = new byte [ _outputCount * sizeof ( float ) ] ;
7280 var outputs = new double [ _outputCount ] ;
@@ -76,7 +84,7 @@ public FitnessInfo Evaluate(IBlackBox<double> phenome)
7684 }
7785 else
7886 {
79- int maxSigIndex = ReadMaxSigIndex ( phenome ) ;
87+ var maxSigIndex = ReadMaxSigIndex ( phenome ) ;
8088 var outputBuffer = new byte [ sizeof ( int ) ] ;
8189 Buffer . BlockCopy ( new int [ ] { maxSigIndex } , 0 , outputBuffer , 0 , outputBuffer . Length ) ;
8290 namedPipeClientStream . Write ( outputBuffer , 0 , outputBuffer . Length ) ;
@@ -101,16 +109,16 @@ public FitnessInfo Evaluate(IBlackBox<double> phenome)
101109 return new FitnessInfo ( maskedReward ) ;
102110 }
103111
104- static ( double [ ] observation , double [ ] reward , int [ ] done ) ReadObservation ( NamedPipeClientStream namedPipeClientStream , int count )
112+ private static ( double [ ] observation , double [ ] reward , int [ ] done ) ReadObservation ( NamedPipeClientStream namedPipeClientStream , int count )
105113 {
106114 var count0 = count * sizeof ( double ) ;
107- var count1 = sizeof ( double ) ;
108- var count2 = sizeof ( int ) ;
115+ const int count1 = sizeof ( double ) ;
116+ const int count2 = sizeof ( int ) ;
109117 var inputBuffer = new byte [ count0 + count1 + count2 ] ;
110118 namedPipeClientStream . Read ( inputBuffer , 0 , inputBuffer . Length ) ;
111- double [ ] observation = new double [ count ] ;
112- double [ ] reward = new double [ 1 ] ;
113- int [ ] done = new int [ 1 ] ;
119+ var observation = new double [ count ] ;
120+ var reward = new double [ 1 ] ;
121+ var done = new int [ 1 ] ;
114122 var offset1 = count0 ;
115123 var offset2 = count0 + count1 ;
116124 Buffer . BlockCopy ( inputBuffer , 0 , observation , 0 , count0 ) ;
@@ -119,46 +127,44 @@ public FitnessInfo Evaluate(IBlackBox<double> phenome)
119127 return ( observation , reward , done ) ;
120128 }
121129
122- static double [ ] ReadDoubleArray ( NamedPipeClientStream namedPipeClientStream , int count )
130+ private static double [ ] ReadDoubleArray ( NamedPipeClientStream namedPipeClientStream , int count )
123131 {
124132 var inputBuffer = new byte [ count * sizeof ( double ) ] ;
125133 namedPipeClientStream . Read ( inputBuffer , 0 , inputBuffer . Length ) ;
126- double [ ] values = new double [ inputBuffer . Length / sizeof ( double ) ] ;
134+ var values = new double [ inputBuffer . Length / sizeof ( double ) ] ;
127135 Buffer . BlockCopy ( inputBuffer , 0 , values , 0 , values . Length * sizeof ( double ) ) ;
128136 return values ;
129137 }
130138
131- static float [ ] ReadFloatArray ( NamedPipeClientStream namedPipeClientStream , int count )
139+ private static float [ ] ReadFloatArray ( NamedPipeClientStream namedPipeClientStream , int count )
132140 {
133141 var inputBuffer = new byte [ count * sizeof ( float ) ] ;
134142 namedPipeClientStream . Read ( inputBuffer , 0 , inputBuffer . Length ) ;
135- float [ ] values = new float [ inputBuffer . Length / sizeof ( float ) ] ;
143+ var values = new float [ inputBuffer . Length / sizeof ( float ) ] ;
136144 Buffer . BlockCopy ( inputBuffer , 0 , values , 0 , values . Length * sizeof ( float ) ) ;
137145 return values ;
138146 }
139147
140- static int [ ] ReadIntArray ( NamedPipeClientStream namedPipeClientStream , int count )
148+ private static int [ ] ReadIntArray ( NamedPipeClientStream namedPipeClientStream , int count )
141149 {
142150 var inputBuffer = new byte [ count * sizeof ( int ) ] ;
143151 namedPipeClientStream . Read ( inputBuffer , 0 , inputBuffer . Length ) ;
144- int [ ] values = new int [ inputBuffer . Length / sizeof ( int ) ] ;
152+ var values = new int [ inputBuffer . Length / sizeof ( int ) ] ;
145153 Buffer . BlockCopy ( inputBuffer , 0 , values , 0 , values . Length * sizeof ( int ) ) ;
146154 return values ;
147155 }
148156
149- int ReadMaxSigIndex ( IBlackBox < double > phenome )
157+ private int ReadMaxSigIndex ( IBlackBox < double > phenome )
150158 {
151- double maxSig = phenome . Outputs . Span [ 0 ] ;
152- int maxSigIdx = 0 ;
159+ var maxSig = phenome . Outputs . Span [ 0 ] ;
160+ var maxSigIdx = 0 ;
153161
154- for ( int i = 1 ; i < _outputCount ; i ++ )
162+ for ( var i = 1 ; i < _outputCount ; i ++ )
155163 {
156- double v = phenome . Outputs . Span [ i ] ;
157- if ( v > maxSig )
158- {
159- maxSig = v ;
160- maxSigIdx = i ;
161- }
164+ var v = phenome . Outputs . Span [ i ] ;
165+ if ( ! ( v > maxSig ) ) continue ;
166+ maxSig = v ;
167+ maxSigIdx = i ;
162168 }
163169
164170 return maxSigIdx ;
0 commit comments