質問編集履歴

3

コメントの追加

2020/11/16 13:13

投稿

shi.hi
shi.hi

スコア1

test CHANGED
File without changes
test CHANGED
@@ -48,6 +48,8 @@
48
48
 
49
49
  class Game(gym.core.Env):
50
50
 
51
+ #初期条件や各種変数の初期格納する。
52
+
51
53
  def __init__(self):
52
54
 
53
55
  self.hunter_Position_X=random.randint(0,5)
@@ -56,13 +58,15 @@
56
58
 
57
59
  print("鬼の初期位置は"+str(self.hunter_Position_X),self.hunter_Position_Y)
58
60
 
61
+ #selfでグローバル変数化している。鬼のx,y座標をランダムに配置。
62
+
59
63
  self.fugitive_Position_X=random.randint(0,5)
60
64
 
61
65
  self.fugitive_Position_Y=random.randint(0,5)
62
66
 
63
67
  print("逃亡者の初期位置は"+str(self.fugitive_Position_X),self.fugitive_Position_Y)
64
68
 
65
-
69
+ #selfでグローバル変数化している。逃亡者のx,y座標をランダムに配置。fugitiveは逃亡者という意味。
66
70
 
67
71
  while self.hunter_Position_X == self.fugitive_Position_X and self.hunter_Position_Y == self.fugitive_Position_Y:
68
72
 
@@ -70,24 +74,32 @@
70
74
 
71
75
  self.hunter_Position_Y=random.randint(0,5)
72
76
 
73
-
77
+ #print(self.hunter_Position_X,self.hunter_Position_Y)
78
+
79
+ #逃亡者と鬼の位置が完全に一致している場合、鬼の初期位置を再度決める。
74
80
 
75
81
  self.game_count=0
76
82
 
77
-
83
+ #1ゲームで行動できる上限を設定をしている。今回は10回とする。
78
84
 
79
85
  self.initial_distance=int(100*math.sqrt((self.hunter_Position_X-self.fugitive_Position_X)**2+(self.hunter_Position_Y-self.fugitive_Position_Y)**2))
80
86
 
81
87
  print("初期の距離は"+str(self.initial_distance))
82
88
 
83
-
89
+ #鬼と逃亡者の距離を定義する。ただの三平方の定理。自然数で処理するため100倍した。
84
90
 
85
91
  self.lists = []
86
92
 
93
+ #距離を格納するリスト。
94
+
87
95
  self.current_hunter_profit_lists = []
88
96
 
97
+ #鬼の報酬を各ステップごとに加える。
98
+
89
99
  self.current_fugitive_profit_lists = []
90
100
 
101
+ #逃亡者の報酬を各ステップごとに加える。
102
+
91
103
 
92
104
 
93
105
  self.action_space = gym.spaces.Discrete(4)
@@ -98,14 +110,22 @@
98
110
 
99
111
  self.observation_space = gym.spaces.Box(low, high, dtype=np.int64)
100
112
 
113
+ #逃走エリアを定義している。
114
+
101
115
  self.hunter_reward=0
102
116
 
103
117
  self.fugitive_reward=0
104
118
 
119
+ #鬼と逃亡者の報酬を0で初期化している。
120
+
105
121
  self.learn_count=0
106
122
 
123
+ #学習回数を10000回と制限。
124
+
107
125
  self.lists.append(self.initial_distance)
108
126
 
127
+ #開始時の距離を格納する。
128
+
109
129
 
110
130
 
111
131
  def step(self,action):
@@ -136,7 +156,11 @@
136
156
 
137
157
  print("逃亡者の位置は"+str(self.fugitive_Position_X),self.fugitive_Position_Y)
138
158
 
159
+
160
+
139
-
161
+ #鬼の行動を4つ設け選択できるようにする。上下左右に移動できる。
162
+
163
+
140
164
 
141
165
  if action == 0 and self.hunter_Position_X == 5:
142
166
 
@@ -154,20 +178,28 @@
154
178
 
155
179
  pass
156
180
 
157
-
181
+ #例外処理としてエリア外に出る行為は1ターン無駄に消費する事とする。andは&と書くと想定外の動作となった為使用禁止。
158
182
 
159
183
  time.sleep(0.01)
160
184
 
185
+ #間隔を0.01秒とする。
186
+
161
187
  self.d = self.cal_distance(h_X = self.hunter_Position_X , h_Y = self.hunter_Position_Y , f_X = self.fugitive_Position_X , f_Y = self.fugitive_Position_Y)
162
188
 
163
189
  self.lists.append(self.d)
164
190
 
191
+ #距離を格納
192
+
165
193
  self.observation = (self.hunter_Position_X,self.hunter_Position_Y,self.fugitive_Position_X,self.fugitive_Position_Y)
166
194
 
195
+ #鬼と逃亡者の位置を毎回格納する。
196
+
167
197
 
168
198
 
169
199
  hunter_reward,fugitive_reward=self.calc_profit()
170
200
 
201
+ #報酬はcalc_profitcalc_profit関数で計算するのでそちらを参照。
202
+
171
203
  print("鬼の報酬は"+str(hunter_reward),"逃亡者の報酬は"+str(fugitive_reward))
172
204
 
173
205
  print("鬼の総合報酬は",sum(self.current_hunter_profit_lists),"逃亡者の総合報酬は",sum(self.current_fugitive_profit_lists))
@@ -180,7 +212,27 @@
180
212
 
181
213
  print("return値は",np.array(self.observation),hunter_reward,action)
182
214
 
183
- **return np.array(self.observation),hunter_reward,action,{}**
215
+ return np.array(self.observation),hunter_reward,action,{}
216
+
217
+ #値は4つ必要。学習が良くない時は上記の変数値を変える必要あり。行動を決める要素を入れる。
218
+
219
+
220
+
221
+ #if action == 4:
222
+
223
+ #self.fugitive_Position_X += 1
224
+
225
+ #if action == 5:
226
+
227
+ #self.fugitive_Position_X -= 1
228
+
229
+ #if action == 6:
230
+
231
+ #self.fugitive_Position_Y += 1
232
+
233
+ #if action == 7:
234
+
235
+ #self.fugitive_Position_Y -= 1
184
236
 
185
237
 
186
238
 
@@ -206,6 +258,12 @@
206
258
 
207
259
  return hunter_Position_X,hunter_Position_Y,fugitive_Position_X,fugitive_Position_Y
208
260
 
261
+ #返り値を残しておく。
262
+
263
+ #1ゲームの終了条件を満たしたときに行う指示を記載。
264
+
265
+ #鬼、逃亡者をランダムに配置する。
266
+
209
267
 
210
268
 
211
269
  def cal_distance(self , h_X , h_Y ,f_X ,f_Y):
@@ -244,6 +302,8 @@
244
302
 
245
303
  self.lists.append(self.cal_distance(self.hunter_Position_X,self.hunter_Position_Y,self.fugitive_Position_X,self.fugitive_Position_Y))
246
304
 
305
+ #10回の行動以下で鬼が確保できた時の報酬を定義している。また距離のリストやゲームカウントを初期化している。
306
+
247
307
 
248
308
 
249
309
  elif i == 10 and (0 not in self.lists):
@@ -270,6 +330,8 @@
270
330
 
271
331
  self.lists.append(self.cal_distance(self.hunter_Position_X,self.hunter_Position_Y,self.fugitive_Position_X,self.fugitive_Position_Y))
272
332
 
333
+ #10回の行動以下で鬼が確保出来なかった時の報酬を定義している。また距離のリストやゲームカウントを初期化している。
334
+
273
335
 
274
336
 
275
337
  elif i <= 10 and self.lists[i-1] < self.lists[i]:
@@ -288,6 +350,8 @@
288
350
 
289
351
  print("逃げられてるよ!!!")
290
352
 
353
+ #前回ステップと今回のステップで距離を比較して報酬を定義している。
354
+
291
355
 
292
356
 
293
357
  elif i <= 10 and self.lists[i-1] > self.lists[i]:
@@ -306,6 +370,8 @@
306
370
 
307
371
  print("距離を詰めてるね!!!")
308
372
 
373
+ #前回ステップと今回のステップで距離を比較して報酬を定義している。
374
+
309
375
 
310
376
 
311
377
  elif i <= 10 and self.lists[i-1] == self.lists[i]:
@@ -324,6 +390,8 @@
324
390
 
325
391
  print("距離が変わってないよ!!!")
326
392
 
393
+ #前回ステップと今回のステップで距離を比較して報酬を定義している。
394
+
327
395
 
328
396
 
329
397
  else:
@@ -334,8 +402,26 @@
334
402
 
335
403
  return current_hunter_reward,current_fugitive_reward
336
404
 
405
+
406
+
407
+ #def Linear_function:
408
+
409
+ #Y_intercept_1 = self.hunter_Position_Y - math.sqrt(3)*self.hunter_Position_X
410
+
411
+ #Y_intercept_2 = self.hunter_Position_Y + math.sqrt(3)*self.hunter_Position_X
412
+
413
+ #Y_intercept_3 = self.hunter_Position_Y - (1/math.sqrt(3))*self.hunter_Position_X
414
+
415
+ #Y_intercept_4 = self.hunter_Position_Y + (1/math.sqrt(3))*self.hunter_Position_X
416
+
417
+ #Y = math.sqrt(3)X + b
418
+
337
419
 
338
420
 
421
+ #プログラミングは書いた通りにしか動かない。
422
+
423
+
424
+
339
425
  def reset(self):
340
426
 
341
427
  if self.learn_count == 0:
@@ -346,7 +432,7 @@
346
432
 
347
433
  is_end = False
348
434
 
349
- リセットする条件は学習回数を満了した時のみ。その際に報酬をリセットする。
435
+ #リセットする条件は学習回数を満了した時のみ。その際に報酬をリセットする。
350
436
 
351
437
 
352
438
 

2

誤字

2020/11/16 13:13

投稿

shi.hi
shi.hi

スコア1

test CHANGED
File without changes
test CHANGED
@@ -10,6 +10,8 @@
10
10
 
11
11
  ``` 発生している問題
12
12
 
13
+ ```
14
+
13
15
  ルールは作成済ですが、いざ学習してみると鬼の学習が全然できていない状態です。
14
16
 
15
17
  10万回学習しても何故か上手く行かないです。
@@ -178,7 +180,7 @@
178
180
 
179
181
  print("return値は",np.array(self.observation),hunter_reward,action)
180
182
 
181
- ** return np.array(self.observation),hunter_reward,action,{}**
183
+ **return np.array(self.observation),hunter_reward,action,{}**
182
184
 
183
185
 
184
186
 
@@ -350,12 +352,16 @@
350
352
 
351
353
  ```ここに言語名を入力
352
354
 
355
+ ```
356
+
353
357
  python
354
358
 
355
359
 
356
360
 
357
361
  ``` 試したこと
358
362
 
363
+ ```
364
+
359
365
  gymにあるcartpoleのコードを参考にしてstep内のreturnを復元してみました。
360
366
 
361
367
 

1

誤字

2020/11/15 10:12

投稿

shi.hi
shi.hi

スコア1

test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,4 @@
1
- ### 前提・実現したいこと
1
+ ``` 前提・実現したいこと
2
2
 
3
3
 
4
4
 
@@ -8,7 +8,7 @@
8
8
 
9
9
 
10
10
 
11
- ### 発生している問題・エラーメッセージ
11
+ ``` 発生している問題
12
12
 
13
13
  ルールは作成済ですが、いざ学習してみると鬼の学習が全然できていない状態です。
14
14
 
@@ -18,13 +18,15 @@
18
18
 
19
19
 
20
20
 
21
- エラーメッセージ
21
+ ```エラーメッセージ
22
22
 
23
23
  特になし
24
24
 
25
25
 
26
26
 
27
- ### 該当のソースコード
27
+ ``` 該当のソースコード
28
+
29
+
28
30
 
29
31
  import gym.spaces
30
32
 
@@ -44,8 +46,6 @@
44
46
 
45
47
  class Game(gym.core.Env):
46
48
 
47
- #初期条件や各種変数の初期格納する。
48
-
49
49
  def __init__(self):
50
50
 
51
51
  self.hunter_Position_X=random.randint(0,5)
@@ -54,15 +54,13 @@
54
54
 
55
55
  print("鬼の初期位置は"+str(self.hunter_Position_X),self.hunter_Position_Y)
56
56
 
57
- #selfでグローバル変数化している。鬼のx,y座標をランダムに配置。
58
-
59
57
  self.fugitive_Position_X=random.randint(0,5)
60
58
 
61
59
  self.fugitive_Position_Y=random.randint(0,5)
62
60
 
63
61
  print("逃亡者の初期位置は"+str(self.fugitive_Position_X),self.fugitive_Position_Y)
64
62
 
65
- #selfでグローバル変数化している。逃亡者のx,y座標をランダムに配置。fugitiveは逃亡者という意味。
63
+
66
64
 
67
65
  while self.hunter_Position_X == self.fugitive_Position_X and self.hunter_Position_Y == self.fugitive_Position_Y:
68
66
 
@@ -70,32 +68,24 @@
70
68
 
71
69
  self.hunter_Position_Y=random.randint(0,5)
72
70
 
73
- #print(self.hunter_Position_X,self.hunter_Position_Y)
71
+
74
-
75
- #逃亡者と鬼の位置が完全に一致している場合、鬼の初期位置を再度決める。
76
72
 
77
73
  self.game_count=0
78
74
 
79
- #1ゲームで行動できる上限を設定をしている。今回は10回とする。
75
+
80
76
 
81
77
  self.initial_distance=int(100*math.sqrt((self.hunter_Position_X-self.fugitive_Position_X)**2+(self.hunter_Position_Y-self.fugitive_Position_Y)**2))
82
78
 
83
79
  print("初期の距離は"+str(self.initial_distance))
84
80
 
85
- #鬼と逃亡者の距離を定義する。ただの三平方の定理。自然数で処理するため100倍した。
81
+
86
82
 
87
83
  self.lists = []
88
84
 
89
- #距離を格納するリスト。
90
-
91
85
  self.current_hunter_profit_lists = []
92
86
 
93
- #鬼の報酬を各ステップごとに加える。
94
-
95
87
  self.current_fugitive_profit_lists = []
96
88
 
97
- #逃亡者の報酬を各ステップごとに加える。
98
-
99
89
 
100
90
 
101
91
  self.action_space = gym.spaces.Discrete(4)
@@ -106,22 +96,14 @@
106
96
 
107
97
  self.observation_space = gym.spaces.Box(low, high, dtype=np.int64)
108
98
 
109
- #逃走エリアを定義している。
110
-
111
99
  self.hunter_reward=0
112
100
 
113
101
  self.fugitive_reward=0
114
102
 
115
- #鬼と逃亡者の報酬を0で初期化している。
116
-
117
103
  self.learn_count=0
118
104
 
119
- #学習回数を10000回と制限。
120
-
121
105
  self.lists.append(self.initial_distance)
122
106
 
123
- #開始時の距離を格納する。
124
-
125
107
 
126
108
 
127
109
  def step(self,action):
@@ -152,283 +134,217 @@
152
134
 
153
135
  print("逃亡者の位置は"+str(self.fugitive_Position_X),self.fugitive_Position_Y)
154
136
 
137
+
138
+
139
+ if action == 0 and self.hunter_Position_X == 5:
140
+
141
+ pass
142
+
143
+ if action == 1 and self.hunter_Position_X == 0:
144
+
145
+ pass
146
+
147
+ if action == 2 and self.hunter_Position_Y == 5:
148
+
149
+ pass
150
+
151
+ if action == 3 and self.hunter_Position_Y == 0:
152
+
153
+ pass
154
+
155
+
156
+
157
+ time.sleep(0.01)
158
+
159
+ self.d = self.cal_distance(h_X = self.hunter_Position_X , h_Y = self.hunter_Position_Y , f_X = self.fugitive_Position_X , f_Y = self.fugitive_Position_Y)
160
+
161
+ self.lists.append(self.d)
162
+
163
+ self.observation = (self.hunter_Position_X,self.hunter_Position_Y,self.fugitive_Position_X,self.fugitive_Position_Y)
164
+
165
+
166
+
167
+ hunter_reward,fugitive_reward=self.calc_profit()
168
+
169
+ print("鬼の報酬は"+str(hunter_reward),"逃亡者の報酬は"+str(fugitive_reward))
170
+
171
+ print("鬼の総合報酬は",sum(self.current_hunter_profit_lists),"逃亡者の総合報酬は",sum(self.current_fugitive_profit_lists))
172
+
173
+
174
+
175
+ is_end = self.reset()
176
+
177
+
178
+
179
+ print("return値は",np.array(self.observation),hunter_reward,action)
180
+
181
+ ** return np.array(self.observation),hunter_reward,action,{}**
182
+
183
+
184
+
185
+ def reset_position(self):
186
+
187
+ hunter_Position_X=random.randint(0,5)
188
+
189
+ hunter_Position_Y=random.randint(0,5)
190
+
191
+ fugitive_Position_X=random.randint(0,5)
192
+
193
+ fugitive_Position_Y=random.randint(0,5)
194
+
195
+ while hunter_Position_X == fugitive_Position_X and hunter_Position_Y == fugitive_Position_Y:
196
+
197
+ hunter_Position_X=random.randint(0,5)
198
+
199
+ hunter_Position_Y=random.randint(0,5)
200
+
201
+ print("リセットされました!!!")
202
+
203
+ print()
204
+
205
+ return hunter_Position_X,hunter_Position_Y,fugitive_Position_X,fugitive_Position_Y
206
+
207
+
208
+
209
+ def cal_distance(self , h_X , h_Y ,f_X ,f_Y):
210
+
211
+ distance = int(100*math.sqrt((h_X-f_X)**2 +(h_Y-f_Y)**2))
212
+
213
+ return distance
214
+
215
+
216
+
217
+ def calc_profit(self):
218
+
219
+ i= self.game_count
220
+
221
+ if i <= 10 and self.lists[i] == 0:
222
+
223
+ self.hunter_reward += 1
224
+
225
+ self.fugitive_reward -= 1
226
+
227
+ current_hunter_reward = 1
228
+
229
+ current_fugitive_reward = -1
230
+
231
+ self.current_hunter_profit_lists.append(current_hunter_reward)
232
+
233
+ self.current_fugitive_profit_lists.append(current_fugitive_reward)
234
+
235
+ print("確保成功!!!")
236
+
237
+ self.hunter_Position_X,self.hunter_Position_Y,self.fugitive_Position_X,self.fugitive_Position_Y = self.reset_position()
238
+
239
+ self.game_count = 0
240
+
241
+ self.lists = []
242
+
243
+ self.lists.append(self.cal_distance(self.hunter_Position_X,self.hunter_Position_Y,self.fugitive_Position_X,self.fugitive_Position_Y))
244
+
245
+
246
+
247
+ elif i == 10 and (0 not in self.lists):
248
+
249
+ self.hunter_reward -= 1
250
+
251
+ self.fugitive_reward += 1
252
+
253
+ current_hunter_reward = -1
254
+
255
+ current_fugitive_reward = 1
256
+
257
+ self.current_hunter_profit_lists.append(current_hunter_reward)
258
+
259
+ self.current_fugitive_profit_lists.append(current_fugitive_reward)
260
+
261
+ print("確保失敗!!!")
262
+
263
+ self.hunter_Position_X,self.hunter_Position_Y,self.fugitive_Position_X,self.fugitive_Position_Y = self.reset_position()
264
+
265
+ self.game_count = 0
266
+
267
+ self.lists = []
268
+
269
+ self.lists.append(self.cal_distance(self.hunter_Position_X,self.hunter_Position_Y,self.fugitive_Position_X,self.fugitive_Position_Y))
270
+
271
+
272
+
273
+ elif i <= 10 and self.lists[i-1] < self.lists[i]:
274
+
275
+ self.hunter_reward -= 1
276
+
277
+ self.fugitive_reward += 1
278
+
279
+ current_hunter_reward = -1
280
+
281
+ current_fugitive_reward = 1
282
+
283
+ self.current_hunter_profit_lists.append(current_hunter_reward)
284
+
285
+ self.current_fugitive_profit_lists.append(current_fugitive_reward)
286
+
287
+ print("逃げられてるよ!!!")
288
+
289
+
290
+
291
+ elif i <= 10 and self.lists[i-1] > self.lists[i]:
292
+
293
+ self.hunter_reward += 1
294
+
295
+ self.fugitive_reward -= 1
296
+
297
+ current_hunter_reward = 1
298
+
299
+ current_fugitive_reward = -1
300
+
301
+ self.current_hunter_profit_lists.append(current_hunter_reward)
302
+
303
+ self.current_fugitive_profit_lists.append(current_fugitive_reward)
304
+
305
+ print("距離を詰めてるね!!!")
306
+
307
+
308
+
309
+ elif i <= 10 and self.lists[i-1] == self.lists[i]:
310
+
311
+ self.hunter_reward += 0
312
+
313
+ self.fugitive_reward += 0
314
+
315
+ current_hunter_reward = 0
316
+
317
+ current_fugitive_reward = 0
318
+
319
+ self.current_hunter_profit_lists.append(current_hunter_reward)
320
+
321
+ self.current_fugitive_profit_lists.append(current_fugitive_reward)
322
+
323
+ print("距離が変わってないよ!!!")
324
+
325
+
326
+
327
+ else:
328
+
329
+ pass
330
+
331
+
332
+
333
+ return current_hunter_reward,current_fugitive_reward
334
+
155
335
 
156
336
 
157
- #鬼の行動を4つ設け選択できるようにする。上下左右に移動できる。
158
-
159
-
160
-
161
- if action == 0 and self.hunter_Position_X == 5:
162
-
163
- pass
164
-
165
- if action == 1 and self.hunter_Position_X == 0:
166
-
167
- pass
168
-
169
- if action == 2 and self.hunter_Position_Y == 5:
170
-
171
- pass
172
-
173
- if action == 3 and self.hunter_Position_Y == 0:
174
-
175
- pass
176
-
177
- #例外処理としてエリア外に出る行為は1ターン無駄に消費する事とする。andは&と書くと想定外の動作となった為使用禁止。
178
-
179
- time.sleep(0.01)
180
-
181
- #間隔を0.01秒とする。
182
-
183
- self.d = self.cal_distance(h_X = self.hunter_Position_X , h_Y = self.hunter_Position_Y , f_X = self.fugitive_Position_X , f_Y = self.fugitive_Position_Y)
184
-
185
- self.lists.append(self.d)
186
-
187
- #距離を格納
188
-
189
- self.observation = (self.hunter_Position_X,self.hunter_Position_Y,self.fugitive_Position_X,self.fugitive_Position_Y)
190
-
191
- #鬼と逃亡者の位置を毎回格納する。
192
-
193
-
194
-
195
- hunter_reward,fugitive_reward=self.calc_profit()
196
-
197
- #報酬はcalc_profitcalc_profit関数で計算するのでそちらを参照。
198
-
199
- print("鬼の報酬は"+str(hunter_reward),"逃亡者の報酬は"+str(fugitive_reward))
200
-
201
- print("鬼の総合報酬は",sum(self.current_hunter_profit_lists),"逃亡者の総合報酬は",sum(self.current_fugitive_profit_lists))
202
-
203
-
204
-
205
- is_end = self.reset()
206
-
207
-
208
-
209
- print("return値は",np.array(self.observation),hunter_reward,action)
210
-
211
- ** return np.array(self.observation),hunter_reward,action,{}**
212
-
213
- **#値は4つ必要。学習が良くない時は上記の変数値を変える必要あり。行動を決める要素を入れる。**
214
-
215
-
216
-
217
- #if action == 4:
218
-
219
- #self.fugitive_Position_X += 1
220
-
221
- #if action == 5:
222
-
223
- #self.fugitive_Position_X -= 1
224
-
225
- #if action == 6:
226
-
227
- #self.fugitive_Position_Y += 1
228
-
229
- #if action == 7:
230
-
231
- #self.fugitive_Position_Y -= 1
232
-
233
-
234
-
235
- def reset_position(self):
337
+ def reset(self):
236
-
237
- hunter_Position_X=random.randint(0,5)
338
+
238
-
239
- hunter_Position_Y=random.randint(0,5)
240
-
241
- fugitive_Position_X=random.randint(0,5)
242
-
243
- fugitive_Position_Y=random.randint(0,5)
244
-
245
- while hunter_Position_X == fugitive_Position_X and hunter_Position_Y == fugitive_Position_Y:
246
-
247
- hunter_Position_X=random.randint(0,5)
248
-
249
- hunter_Position_Y=random.randint(0,5)
250
-
251
- print("リセットされました!!!")
252
-
253
- print()
254
-
255
- return hunter_Position_X,hunter_Position_Y,fugitive_Position_X,fugitive_Position_Y
256
-
257
- #返り値を残しておく。
258
-
259
- #1ゲームの終了条件を満たしたときに行う指示を記載。
260
-
261
- #鬼、逃亡者をランダムに配置する。
262
-
263
-
264
-
265
- def cal_distance(self , h_X , h_Y ,f_X ,f_Y):
266
-
267
- distance = int(100*math.sqrt((h_X-f_X)**2 +(h_Y-f_Y)**2))
268
-
269
- return distance
270
-
271
-
272
-
273
- def calc_profit(self):
274
-
275
- i= self.game_count
276
-
277
- if i <= 10 and self.lists[i] == 0:
278
-
279
- self.hunter_reward += 1
280
-
281
- self.fugitive_reward -= 1
282
-
283
- current_hunter_reward = 1
284
-
285
- current_fugitive_reward = -1
286
-
287
- self.current_hunter_profit_lists.append(current_hunter_reward)
288
-
289
- self.current_fugitive_profit_lists.append(current_fugitive_reward)
290
-
291
- print("確保成功!!!")
292
-
293
- self.hunter_Position_X,self.hunter_Position_Y,self.fugitive_Position_X,self.fugitive_Position_Y = self.reset_position()
294
-
295
- self.game_count = 0
339
+ if self.learn_count == 0:
296
-
340
+
297
- self.lists = []
341
+ is_end = True
298
-
299
- self.lists.append(self.cal_distance(self.hunter_Position_X,self.hunter_Position_Y,self.fugitive_Position_X,self.fugitive_Position_Y))
300
-
301
- #10回の行動以下で鬼が確保できた時の報酬を定義している。また距離のリストやゲームカウントを初期化している。
302
-
303
-
304
-
305
- elif i == 10 and (0 not in self.lists):
306
-
307
- self.hunter_reward -= 1
308
-
309
- self.fugitive_reward += 1
310
-
311
- current_hunter_reward = -1
312
-
313
- current_fugitive_reward = 1
314
-
315
- self.current_hunter_profit_lists.append(current_hunter_reward)
316
-
317
- self.current_fugitive_profit_lists.append(current_fugitive_reward)
318
-
319
- print("確保失敗!!!")
320
-
321
- self.hunter_Position_X,self.hunter_Position_Y,self.fugitive_Position_X,self.fugitive_Position_Y = self.reset_position()
322
-
323
- self.game_count = 0
324
-
325
- self.lists = []
326
-
327
- self.lists.append(self.cal_distance(self.hunter_Position_X,self.hunter_Position_Y,self.fugitive_Position_X,self.fugitive_Position_Y))
328
-
329
- #10回の行動以下で鬼が確保出来なかった時の報酬を定義している。また距離のリストやゲームカウントを初期化している。
330
-
331
-
332
-
333
- elif i <= 10 and self.lists[i-1] < self.lists[i]:
334
-
335
- self.hunter_reward -= 1
336
-
337
- self.fugitive_reward += 1
338
-
339
- current_hunter_reward = -1
340
-
341
- current_fugitive_reward = 1
342
-
343
- self.current_hunter_profit_lists.append(current_hunter_reward)
344
-
345
- self.current_fugitive_profit_lists.append(current_fugitive_reward)
346
-
347
- print("逃げられてるよ!!!")
348
-
349
- #前回ステップと今回のステップで距離を比較して報酬を定義している。
350
-
351
-
352
-
353
- elif i <= 10 and self.lists[i-1] > self.lists[i]:
354
-
355
- self.hunter_reward += 1
356
-
357
- self.fugitive_reward -= 1
358
-
359
- current_hunter_reward = 1
360
-
361
- current_fugitive_reward = -1
362
-
363
- self.current_hunter_profit_lists.append(current_hunter_reward)
364
-
365
- self.current_fugitive_profit_lists.append(current_fugitive_reward)
366
-
367
- print("距離を詰めてるね!!!")
368
-
369
- #前回ステップと今回のステップで距離を比較して報酬を定義している。
370
-
371
-
372
-
373
- elif i <= 10 and self.lists[i-1] == self.lists[i]:
374
-
375
- self.hunter_reward += 0
376
-
377
- self.fugitive_reward += 0
378
-
379
- current_hunter_reward = 0
380
-
381
- current_fugitive_reward = 0
382
-
383
- self.current_hunter_profit_lists.append(current_hunter_reward)
384
-
385
- self.current_fugitive_profit_lists.append(current_fugitive_reward)
386
-
387
- print("距離が変わってないよ!!!")
388
-
389
- #前回ステップと今回のステップで距離を比較して報酬を定義している。
390
-
391
-
392
342
 
393
343
  else:
394
344
 
395
- pass
396
-
397
-
398
-
399
- return current_hunter_reward,current_fugitive_reward
400
-
401
-
402
-
403
- #def Linear_function:
404
-
405
- #Y_intercept_1 = self.hunter_Position_Y - math.sqrt(3)*self.hunter_Position_X
406
-
407
- #Y_intercept_2 = self.hunter_Position_Y + math.sqrt(3)*self.hunter_Position_X
408
-
409
- #Y_intercept_3 = self.hunter_Position_Y - (1/math.sqrt(3))*self.hunter_Position_X
410
-
411
- #Y_intercept_4 = self.hunter_Position_Y + (1/math.sqrt(3))*self.hunter_Position_X
412
-
413
- #Y = math.sqrt(3)X + b
414
-
415
-
416
-
417
- #プログラミングは書いた通りにしか動かない。
418
-
419
-
420
-
421
- def reset(self):
422
-
423
- if self.learn_count == 0:
424
-
425
- is_end = True
426
-
427
- else:
428
-
429
345
  is_end = False
430
346
 
431
- #リセットする条件は学習回数を満了した時のみ。その際に報酬をリセットする。
347
+ リセットする条件は学習回数を満了した時のみ。その際に報酬をリセットする。
432
348
 
433
349
 
434
350
 
@@ -438,12 +354,12 @@
438
354
 
439
355
 
440
356
 
441
- ### 試したこと
357
+ ``` 試したこと
442
358
 
443
359
  gymにあるcartpoleのコードを参考にしてstep内のreturnを復元してみました。
444
360
 
445
361
 
446
362
 
447
- ### 補足情報(FW/ツールのバージョンなど)
363
+ ``` 補足情報(FW/ツールのバージョンなど)
448
364
 
449
365
  なし