質問するログイン新規登録

質問編集履歴

3

コメントの追加

2020/11/16 13:13

投稿

shi.hi
shi.hi

スコア1

title CHANGED
File without changes
body CHANGED
@@ -23,35 +23,45 @@
23
23
  import random
24
24
 
25
25
  class Game(gym.core.Env):
26
+ #初期条件や各種変数の初期格納する。
26
27
  def __init__(self):
27
28
  self.hunter_Position_X=random.randint(0,5)
28
29
  self.hunter_Position_Y=random.randint(0,5)
29
30
  print("鬼の初期位置は"+str(self.hunter_Position_X),self.hunter_Position_Y)
31
+ #selfでグローバル変数化している。鬼のx,y座標をランダムに配置。
30
32
  self.fugitive_Position_X=random.randint(0,5)
31
33
  self.fugitive_Position_Y=random.randint(0,5)
32
34
  print("逃亡者の初期位置は"+str(self.fugitive_Position_X),self.fugitive_Position_Y)
33
-
35
+ #selfでグローバル変数化している。逃亡者のx,y座標をランダムに配置。fugitiveは逃亡者という意味。
34
36
  while self.hunter_Position_X == self.fugitive_Position_X and self.hunter_Position_Y == self.fugitive_Position_Y:
35
37
  self.hunter_Position_X=random.randint(0,5)
36
38
  self.hunter_Position_Y=random.randint(0,5)
37
-
39
+ #print(self.hunter_Position_X,self.hunter_Position_Y)
40
+ #逃亡者と鬼の位置が完全に一致している場合、鬼の初期位置を再度決める。
38
41
  self.game_count=0
39
-
42
+ #1ゲームで行動できる上限を設定をしている。今回は10回とする。
40
43
  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))
41
44
  print("初期の距離は"+str(self.initial_distance))
42
-
45
+ #鬼と逃亡者の距離を定義する。ただの三平方の定理。自然数で処理するため100倍した。
43
46
  self.lists = []
47
+ #距離を格納するリスト。
44
48
  self.current_hunter_profit_lists = []
49
+ #鬼の報酬を各ステップごとに加える。
45
50
  self.current_fugitive_profit_lists = []
51
+ #逃亡者の報酬を各ステップごとに加える。
46
52
 
47
53
  self.action_space = gym.spaces.Discrete(4)
48
54
  low = np.array([0, 0, 0, 0])
49
55
  high = np.array([5, 5, 5, 5])
50
56
  self.observation_space = gym.spaces.Box(low, high, dtype=np.int64)
57
+ #逃走エリアを定義している。
51
58
  self.hunter_reward=0
52
59
  self.fugitive_reward=0
60
+ #鬼と逃亡者の報酬を0で初期化している。
53
61
  self.learn_count=0
62
+ #学習回数を10000回と制限。
54
63
  self.lists.append(self.initial_distance)
64
+ #開始時の距離を格納する。
55
65
 
56
66
  def step(self,action):
57
67
  self.game_count += 1
@@ -67,7 +77,9 @@
67
77
  self.hunter_Position_Y -= 1
68
78
  print("鬼の位置は"+str(self.hunter_Position_X),self.hunter_Position_Y)
69
79
  print("逃亡者の位置は"+str(self.fugitive_Position_X),self.fugitive_Position_Y)
80
+
70
-
81
+ #鬼の行動を4つ設け選択できるようにする。上下左右に移動できる。
82
+
71
83
  if action == 0 and self.hunter_Position_X == 5:
72
84
  pass
73
85
  if action == 1 and self.hunter_Position_X == 0:
@@ -76,20 +88,34 @@
76
88
  pass
77
89
  if action == 3 and self.hunter_Position_Y == 0:
78
90
  pass
79
-
91
+ #例外処理としてエリア外に出る行為は1ターン無駄に消費する事とする。andは&と書くと想定外の動作となった為使用禁止。
80
92
  time.sleep(0.01)
93
+ #間隔を0.01秒とする。
81
94
  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)
82
95
  self.lists.append(self.d)
96
+ #距離を格納
83
97
  self.observation = (self.hunter_Position_X,self.hunter_Position_Y,self.fugitive_Position_X,self.fugitive_Position_Y)
98
+ #鬼と逃亡者の位置を毎回格納する。
84
99
 
85
100
  hunter_reward,fugitive_reward=self.calc_profit()
101
+ #報酬はcalc_profitcalc_profit関数で計算するのでそちらを参照。
86
102
  print("鬼の報酬は"+str(hunter_reward),"逃亡者の報酬は"+str(fugitive_reward))
87
103
  print("鬼の総合報酬は",sum(self.current_hunter_profit_lists),"逃亡者の総合報酬は",sum(self.current_fugitive_profit_lists))
88
104
 
89
105
  is_end = self.reset()
90
106
 
91
107
  print("return値は",np.array(self.observation),hunter_reward,action)
92
- **return np.array(self.observation),hunter_reward,action,{}**
108
+ return np.array(self.observation),hunter_reward,action,{}
109
+ #値は4つ必要。学習が良くない時は上記の変数値を変える必要あり。行動を決める要素を入れる。
110
+
111
+ #if action == 4:
112
+ #self.fugitive_Position_X += 1
113
+ #if action == 5:
114
+ #self.fugitive_Position_X -= 1
115
+ #if action == 6:
116
+ #self.fugitive_Position_Y += 1
117
+ #if action == 7:
118
+ #self.fugitive_Position_Y -= 1
93
119
 
94
120
  def reset_position(self):
95
121
  hunter_Position_X=random.randint(0,5)
@@ -102,6 +128,9 @@
102
128
  print("リセットされました!!!")
103
129
  print()
104
130
  return hunter_Position_X,hunter_Position_Y,fugitive_Position_X,fugitive_Position_Y
131
+ #返り値を残しておく。
132
+ #1ゲームの終了条件を満たしたときに行う指示を記載。
133
+ #鬼、逃亡者をランダムに配置する。
105
134
 
106
135
  def cal_distance(self , h_X , h_Y ,f_X ,f_Y):
107
136
  distance = int(100*math.sqrt((h_X-f_X)**2 +(h_Y-f_Y)**2))
@@ -121,6 +150,7 @@
121
150
  self.game_count = 0
122
151
  self.lists = []
123
152
  self.lists.append(self.cal_distance(self.hunter_Position_X,self.hunter_Position_Y,self.fugitive_Position_X,self.fugitive_Position_Y))
153
+ #10回の行動以下で鬼が確保できた時の報酬を定義している。また距離のリストやゲームカウントを初期化している。
124
154
 
125
155
  elif i == 10 and (0 not in self.lists):
126
156
  self.hunter_reward -= 1
@@ -134,6 +164,7 @@
134
164
  self.game_count = 0
135
165
  self.lists = []
136
166
  self.lists.append(self.cal_distance(self.hunter_Position_X,self.hunter_Position_Y,self.fugitive_Position_X,self.fugitive_Position_Y))
167
+ #10回の行動以下で鬼が確保出来なかった時の報酬を定義している。また距離のリストやゲームカウントを初期化している。
137
168
 
138
169
  elif i <= 10 and self.lists[i-1] < self.lists[i]:
139
170
  self.hunter_reward -= 1
@@ -143,6 +174,7 @@
143
174
  self.current_hunter_profit_lists.append(current_hunter_reward)
144
175
  self.current_fugitive_profit_lists.append(current_fugitive_reward)
145
176
  print("逃げられてるよ!!!")
177
+ #前回ステップと今回のステップで距離を比較して報酬を定義している。
146
178
 
147
179
  elif i <= 10 and self.lists[i-1] > self.lists[i]:
148
180
  self.hunter_reward += 1
@@ -152,6 +184,7 @@
152
184
  self.current_hunter_profit_lists.append(current_hunter_reward)
153
185
  self.current_fugitive_profit_lists.append(current_fugitive_reward)
154
186
  print("距離を詰めてるね!!!")
187
+ #前回ステップと今回のステップで距離を比較して報酬を定義している。
155
188
 
156
189
  elif i <= 10 and self.lists[i-1] == self.lists[i]:
157
190
  self.hunter_reward += 0
@@ -161,18 +194,28 @@
161
194
  self.current_hunter_profit_lists.append(current_hunter_reward)
162
195
  self.current_fugitive_profit_lists.append(current_fugitive_reward)
163
196
  print("距離が変わってないよ!!!")
197
+ #前回ステップと今回のステップで距離を比較して報酬を定義している。
164
198
 
165
199
  else:
166
200
  pass
167
201
 
168
202
  return current_hunter_reward,current_fugitive_reward
203
+
204
+ #def Linear_function:
205
+ #Y_intercept_1 = self.hunter_Position_Y - math.sqrt(3)*self.hunter_Position_X
206
+ #Y_intercept_2 = self.hunter_Position_Y + math.sqrt(3)*self.hunter_Position_X
207
+ #Y_intercept_3 = self.hunter_Position_Y - (1/math.sqrt(3))*self.hunter_Position_X
208
+ #Y_intercept_4 = self.hunter_Position_Y + (1/math.sqrt(3))*self.hunter_Position_X
209
+ #Y = math.sqrt(3)X + b
169
210
 
211
+ #プログラミングは書いた通りにしか動かない。
212
+
170
213
  def reset(self):
171
214
  if self.learn_count == 0:
172
215
  is_end = True
173
216
  else:
174
217
  is_end = False
175
- リセットする条件は学習回数を満了した時のみ。その際に報酬をリセットする。
218
+ #リセットする条件は学習回数を満了した時のみ。その際に報酬をリセットする。
176
219
 
177
220
  ```ここに言語名を入力
178
221
  ```

2

誤字

2020/11/16 13:13

投稿

shi.hi
shi.hi

スコア1

title CHANGED
File without changes
body CHANGED
@@ -4,6 +4,7 @@
4
4
  まず鬼のみ動かせる状態としています。
5
5
 
6
6
  ``` 発生している問題
7
+ ```
7
8
  ルールは作成済ですが、いざ学習してみると鬼の学習が全然できていない状態です。
8
9
  10万回学習しても何故か上手く行かないです。
9
10
  被疑個所としてreturn np.array(self.observation),hunter_reward,action,{}だと考えていますが、他の要因があるか分からないのでご教示いただけないでしょうか。
@@ -88,7 +89,7 @@
88
89
  is_end = self.reset()
89
90
 
90
91
  print("return値は",np.array(self.observation),hunter_reward,action)
91
- ** return np.array(self.observation),hunter_reward,action,{}**
92
+ **return np.array(self.observation),hunter_reward,action,{}**
92
93
 
93
94
  def reset_position(self):
94
95
  hunter_Position_X=random.randint(0,5)
@@ -174,9 +175,11 @@
174
175
  リセットする条件は学習回数を満了した時のみ。その際に報酬をリセットする。
175
176
 
176
177
  ```ここに言語名を入力
178
+ ```
177
179
  python
178
180
 
179
181
  ``` 試したこと
182
+ ```
180
183
  gymにあるcartpoleのコードを参考にしてstep内のreturnを復元してみました。
181
184
 
182
185
  ``` 補足情報(FW/ツールのバージョンなど)

1

誤字

2020/11/15 10:12

投稿

shi.hi
shi.hi

スコア1

title CHANGED
File without changes
body CHANGED
@@ -1,17 +1,18 @@
1
- ### 前提・実現したいこと
1
+ ``` 前提・実現したいこと
2
2
 
3
3
  open ai gymを使用して鬼ごっこの強化学習を作成しています。
4
4
  まず鬼のみ動かせる状態としています。
5
5
 
6
- ### 発生している問題・エラーメッセージ
6
+ ``` 発生している問題
7
7
  ルールは作成済ですが、いざ学習してみると鬼の学習が全然できていない状態です。
8
8
  10万回学習しても何故か上手く行かないです。
9
9
  被疑個所としてreturn np.array(self.observation),hunter_reward,action,{}だと考えていますが、他の要因があるか分からないのでご教示いただけないでしょうか。
10
10
 
11
- エラーメッセージ
11
+ ```エラーメッセージ
12
12
  特になし
13
13
 
14
- ### 該当のソースコード
14
+ ``` 該当のソースコード
15
+
15
16
  import gym.spaces
16
17
  import numpy as np
17
18
  import pandas
@@ -21,45 +22,35 @@
21
22
  import random
22
23
 
23
24
  class Game(gym.core.Env):
24
- #初期条件や各種変数の初期格納する。
25
25
  def __init__(self):
26
26
  self.hunter_Position_X=random.randint(0,5)
27
27
  self.hunter_Position_Y=random.randint(0,5)
28
28
  print("鬼の初期位置は"+str(self.hunter_Position_X),self.hunter_Position_Y)
29
- #selfでグローバル変数化している。鬼のx,y座標をランダムに配置。
30
29
  self.fugitive_Position_X=random.randint(0,5)
31
30
  self.fugitive_Position_Y=random.randint(0,5)
32
31
  print("逃亡者の初期位置は"+str(self.fugitive_Position_X),self.fugitive_Position_Y)
33
- #selfでグローバル変数化している。逃亡者のx,y座標をランダムに配置。fugitiveは逃亡者という意味。
32
+
34
33
  while self.hunter_Position_X == self.fugitive_Position_X and self.hunter_Position_Y == self.fugitive_Position_Y:
35
34
  self.hunter_Position_X=random.randint(0,5)
36
35
  self.hunter_Position_Y=random.randint(0,5)
37
- #print(self.hunter_Position_X,self.hunter_Position_Y)
36
+
38
- #逃亡者と鬼の位置が完全に一致している場合、鬼の初期位置を再度決める。
39
37
  self.game_count=0
40
- #1ゲームで行動できる上限を設定をしている。今回は10回とする。
38
+
41
39
  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))
42
40
  print("初期の距離は"+str(self.initial_distance))
43
- #鬼と逃亡者の距離を定義する。ただの三平方の定理。自然数で処理するため100倍した。
41
+
44
42
  self.lists = []
45
- #距離を格納するリスト。
46
43
  self.current_hunter_profit_lists = []
47
- #鬼の報酬を各ステップごとに加える。
48
44
  self.current_fugitive_profit_lists = []
49
- #逃亡者の報酬を各ステップごとに加える。
50
45
 
51
46
  self.action_space = gym.spaces.Discrete(4)
52
47
  low = np.array([0, 0, 0, 0])
53
48
  high = np.array([5, 5, 5, 5])
54
49
  self.observation_space = gym.spaces.Box(low, high, dtype=np.int64)
55
- #逃走エリアを定義している。
56
50
  self.hunter_reward=0
57
51
  self.fugitive_reward=0
58
- #鬼と逃亡者の報酬を0で初期化している。
59
52
  self.learn_count=0
60
- #学習回数を10000回と制限。
61
53
  self.lists.append(self.initial_distance)
62
- #開始時の距離を格納する。
63
54
 
64
55
  def step(self,action):
65
56
  self.game_count += 1
@@ -75,9 +66,7 @@
75
66
  self.hunter_Position_Y -= 1
76
67
  print("鬼の位置は"+str(self.hunter_Position_X),self.hunter_Position_Y)
77
68
  print("逃亡者の位置は"+str(self.fugitive_Position_X),self.fugitive_Position_Y)
78
-
79
- #鬼の行動を4つ設け選択できるようにする。上下左右に移動できる。
69
+
80
-
81
70
  if action == 0 and self.hunter_Position_X == 5:
82
71
  pass
83
72
  if action == 1 and self.hunter_Position_X == 0:
@@ -86,17 +75,13 @@
86
75
  pass
87
76
  if action == 3 and self.hunter_Position_Y == 0:
88
77
  pass
89
- #例外処理としてエリア外に出る行為は1ターン無駄に消費する事とする。andは&と書くと想定外の動作となった為使用禁止。
78
+
90
79
  time.sleep(0.01)
91
- #間隔を0.01秒とする。
92
80
  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)
93
81
  self.lists.append(self.d)
94
- #距離を格納
95
82
  self.observation = (self.hunter_Position_X,self.hunter_Position_Y,self.fugitive_Position_X,self.fugitive_Position_Y)
96
- #鬼と逃亡者の位置を毎回格納する。
97
83
 
98
84
  hunter_reward,fugitive_reward=self.calc_profit()
99
- #報酬はcalc_profitcalc_profit関数で計算するのでそちらを参照。
100
85
  print("鬼の報酬は"+str(hunter_reward),"逃亡者の報酬は"+str(fugitive_reward))
101
86
  print("鬼の総合報酬は",sum(self.current_hunter_profit_lists),"逃亡者の総合報酬は",sum(self.current_fugitive_profit_lists))
102
87
 
@@ -104,16 +89,6 @@
104
89
 
105
90
  print("return値は",np.array(self.observation),hunter_reward,action)
106
91
  ** return np.array(self.observation),hunter_reward,action,{}**
107
- **#値は4つ必要。学習が良くない時は上記の変数値を変える必要あり。行動を決める要素を入れる。**
108
-
109
- #if action == 4:
110
- #self.fugitive_Position_X += 1
111
- #if action == 5:
112
- #self.fugitive_Position_X -= 1
113
- #if action == 6:
114
- #self.fugitive_Position_Y += 1
115
- #if action == 7:
116
- #self.fugitive_Position_Y -= 1
117
92
 
118
93
  def reset_position(self):
119
94
  hunter_Position_X=random.randint(0,5)
@@ -126,9 +101,6 @@
126
101
  print("リセットされました!!!")
127
102
  print()
128
103
  return hunter_Position_X,hunter_Position_Y,fugitive_Position_X,fugitive_Position_Y
129
- #返り値を残しておく。
130
- #1ゲームの終了条件を満たしたときに行う指示を記載。
131
- #鬼、逃亡者をランダムに配置する。
132
104
 
133
105
  def cal_distance(self , h_X , h_Y ,f_X ,f_Y):
134
106
  distance = int(100*math.sqrt((h_X-f_X)**2 +(h_Y-f_Y)**2))
@@ -148,7 +120,6 @@
148
120
  self.game_count = 0
149
121
  self.lists = []
150
122
  self.lists.append(self.cal_distance(self.hunter_Position_X,self.hunter_Position_Y,self.fugitive_Position_X,self.fugitive_Position_Y))
151
- #10回の行動以下で鬼が確保できた時の報酬を定義している。また距離のリストやゲームカウントを初期化している。
152
123
 
153
124
  elif i == 10 and (0 not in self.lists):
154
125
  self.hunter_reward -= 1
@@ -162,7 +133,6 @@
162
133
  self.game_count = 0
163
134
  self.lists = []
164
135
  self.lists.append(self.cal_distance(self.hunter_Position_X,self.hunter_Position_Y,self.fugitive_Position_X,self.fugitive_Position_Y))
165
- #10回の行動以下で鬼が確保出来なかった時の報酬を定義している。また距離のリストやゲームカウントを初期化している。
166
136
 
167
137
  elif i <= 10 and self.lists[i-1] < self.lists[i]:
168
138
  self.hunter_reward -= 1
@@ -172,7 +142,6 @@
172
142
  self.current_hunter_profit_lists.append(current_hunter_reward)
173
143
  self.current_fugitive_profit_lists.append(current_fugitive_reward)
174
144
  print("逃げられてるよ!!!")
175
- #前回ステップと今回のステップで距離を比較して報酬を定義している。
176
145
 
177
146
  elif i <= 10 and self.lists[i-1] > self.lists[i]:
178
147
  self.hunter_reward += 1
@@ -182,7 +151,6 @@
182
151
  self.current_hunter_profit_lists.append(current_hunter_reward)
183
152
  self.current_fugitive_profit_lists.append(current_fugitive_reward)
184
153
  print("距離を詰めてるね!!!")
185
- #前回ステップと今回のステップで距離を比較して報酬を定義している。
186
154
 
187
155
  elif i <= 10 and self.lists[i-1] == self.lists[i]:
188
156
  self.hunter_reward += 0
@@ -192,34 +160,24 @@
192
160
  self.current_hunter_profit_lists.append(current_hunter_reward)
193
161
  self.current_fugitive_profit_lists.append(current_fugitive_reward)
194
162
  print("距離が変わってないよ!!!")
195
- #前回ステップと今回のステップで距離を比較して報酬を定義している。
196
163
 
197
164
  else:
198
165
  pass
199
166
 
200
167
  return current_hunter_reward,current_fugitive_reward
201
-
202
- #def Linear_function:
203
- #Y_intercept_1 = self.hunter_Position_Y - math.sqrt(3)*self.hunter_Position_X
204
- #Y_intercept_2 = self.hunter_Position_Y + math.sqrt(3)*self.hunter_Position_X
205
- #Y_intercept_3 = self.hunter_Position_Y - (1/math.sqrt(3))*self.hunter_Position_X
206
- #Y_intercept_4 = self.hunter_Position_Y + (1/math.sqrt(3))*self.hunter_Position_X
207
- #Y = math.sqrt(3)X + b
208
168
 
209
- #プログラミングは書いた通りにしか動かない。
210
-
211
169
  def reset(self):
212
170
  if self.learn_count == 0:
213
171
  is_end = True
214
172
  else:
215
173
  is_end = False
216
- #リセットする条件は学習回数を満了した時のみ。その際に報酬をリセットする。
174
+ リセットする条件は学習回数を満了した時のみ。その際に報酬をリセットする。
217
175
 
218
176
  ```ここに言語名を入力
219
177
  python
220
178
 
221
- ### 試したこと
179
+ ``` 試したこと
222
180
  gymにあるcartpoleのコードを参考にしてstep内のreturnを復元してみました。
223
181
 
224
- ### 補足情報(FW/ツールのバージョンなど)
182
+ ``` 補足情報(FW/ツールのバージョンなど)
225
183
  なし