-
Notifications
You must be signed in to change notification settings - Fork 0
/
environment.py
131 lines (94 loc) · 4.35 KB
/
environment.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import numpy as np
import gym
from gym import spaces
from gym.utils import seeding
from particle.ellipsoid import Ellipsoid
# environment for unit cell agent in the packing
class ASC(gym.Env):
metadata = {
'render.modes': ['human', 'rgb_array']
}
def __init__(self, packing, alpha, num_particle):
self.packing = packing
self.particle = Ellipsoid("Jin")
self.num_particle = num_particle
# particle shape parameter
self.particle.S2M = alpha
# Translation and rotation magnitudes:
# In MC simulation, they are adjusted to
# maintain the rate of successful trial moves around 0.3.
# here we initilaize they as 0.3 as well
self.transMod, self.rotMod = 0.3, 0.3
# probability of translation trial move
self.p_trans = 0.5
# acceptance rate of translation and rotation
self.pa_t, self.pa_r = 0.74, 0.98
self.num_step = 0
self.total_step = 0
self.density_old = None
self.density = self.packing.initialize(self.num_particle, self.particle.S2M, self.transMod, self.rotMod, self.p_trans, verb=False)
self.transMod, self.rotMod = 0.15, 0.15
# action space
### transmod and rotmod (*action) reasonable?
self.action_space = spaces.Box(low=0.5, high=2., shape=(3, ), dtype=np.float32)
# observation space
### the shape of particles can vary as initial parameters for different experiments
### to obtain the dependacy of packing density on shape parameter,
### how should it be added in RL?
self.observation_space = spaces.Box(low=-np.inf, high=+np.inf, shape=(3, ), dtype=np.float32)
self.seed()
def seed(self, seed=None):
self.np_random, seed = seeding.np_random(seed)
return [seed]
def step(self, action):
self.density_old = self.density
self.transMod = np.clip(self.transMod*action[0], 0, 0.2)
self.rotMod = np.clip(self.rotMod*action[1], 0, 0.2)
self.p_trans = np.clip(self.p_trans*action[2], 0, 1)
# acceptance rate of translation and rotation
### To do: both two probabilities can be neither too small or too large
### how can this princple be mainfest in RL?
list = self.packing.sim(self.transMod, self.rotMod, self.p_trans, self.total_step)
self.pa_t, self.pa_r = list[0], list[1]
self.num_step += 1
self.total_step += 1
self.density = self.packing.density()
# reward
reward = (self.density - self.density_old) / self.density_old
# observation
### 1-densiy: want to make density close to 1
### but I don't know how to modify two probabilities
obs = np.array([1.-self.density, self.pa_t, self.pa_r])
# done
# delta_density = self.density - self.density_old
if (self.num_step == 300):
done = True
else: done = False
# if (delta_density < 1e-7):
# done = True
# self.packing.scr(1, self.num_step)
# else: done = False
### not clear yet
info = {"packing_fraction":self.density}
# info = {
# # "is_overlap":self.packing.is_overlap,
# # "overlap_potential":self.packing.potential_energy,
# "cell_penalty":self.packing.cell_penalty,
# "packing_fraction":self.packing.fraction
# }
return obs, reward, done, info
def reset(self):
# reset packing
# self.transMod, self.rotMod = 0.3, 0.3
# self.p_trans = 0.5
self.num_step = 0
# self.density_old = None
# self.density = self.packing.initialize(self.num_particle, self.particle.S2M, self.transMod, self.rotMod, self.p_trans, verb=False)
# self.transMod, self.rotMod = 0.15, 0.15
# reset renderer
#self._reset_render()
# record observation
obs = np.array([1.-self.density, self.pa_t, self.pa_r])
return obs
def render(self):
print("packing_fraction {:2f}".format(self.density))