-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrf_cartpole_swingup_gemma2
More file actions
154 lines (105 loc) · 3.92 KB
/
Copy pathrf_cartpole_swingup_gemma2
File metadata and controls
154 lines (105 loc) · 3.92 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
Generation 0
import numpy as np
def reward(action, obs):
"""
Calculates the reward for a given action and observation in the CartPole environment.
Args:
action (ndarray): A 1D array representing the force applied to the cart (-15 to 15).
obs (ndarray): A 1D array representing the observation of the environment [cart_position, pole_angle, cart_velocity, pole_angular_velocity].
Returns:
float: The reward value.
"""
cart_pos = obs[0]
pole_angle = obs[1]
pole_vel = obs[3]
# Reward for keeping the pole upright
rew = -10 * np.abs((pole_angle - np.pi))
# Penalize large cart movements
rew -= 0.1 * np.abs(cart_pos)
# Encourage stillness by penalizing excess force used
rew -= 0.01 * np.abs(action)
return rew
Generation 1
import numpy as np
def reward(action, obs):
"""
Calculates the reward for a given action and observation in the CartPole environment.
Args:
action (ndarray): A 1D array representing the force applied to the cart (-15 to 15).
obs (ndarray): A 1D array representing the observation of the environment [cart_position, pole_angle, cart_velocity, pole_angular_velocity].
Returns:
float: The reward value.
"""
cart_pos = obs[0]
pole_angle = obs[1]
pole_vel = obs[3]
# Reward for keeping the pole upright
rew = -10 * np.abs((pole_angle - np.pi))
# Penalize large cart movements
rew -= 0.1 * np.abs(cart_pos)
# Encourage stillness by penalizing excess force used
rew -= 0.01 * np.abs(action)
return rew
Generation 2
import numpy as np
def reward(action, obs):
"""
Calculates the reward for a given action and observation in the CartPole environment.
Args:
action (ndarray): A 1D array representing the force applied to the cart (-15 to 15).
obs (ndarray): A 1D array representing the observation of the environment [cart_position, pole_angle, cart_velocity, pole_angular_velocity].
Returns:
float: The reward value.
"""
cart_pos = obs[0]
pole_angle = obs[1]
pole_vel = obs[3]
# Reward for keeping the pole upright
rew = -10 * np.abs((pole_angle - np.pi))
# Penalize large cart movements
rew -= 0.1 * np.abs(cart_pos)
# Encourage stillness by penalizing excess force used
rew -= 0.01 * np.abs(action)
return rew
Generation 3
import numpy as np
def reward(action, obs):
"""
Calculates the reward for a given action and observation in the CartPole environment.
Args:
action (ndarray): A 1D array representing the force applied to the cart (-15 to 15).
obs (ndarray): A 1D array representing the observation of the environment [cart_position, pole_angle, cart_velocity, pole_angular_velocity].
Returns:
float: The reward value.
"""
cart_pos = obs[0]
pole_angle = obs[1]
pole_vel = obs[3]
# Reward for keeping the pole upright
rew = -10 * np.abs((pole_angle - np.pi))
# Penalize large cart movements
rew -= 0.1 * np.abs(cart_pos)
# Encourage stillness by penalizing excess force used
rew -= 0.01 * np.abs(action)
return rew
Generation 4
import numpy as np
def reward(action, obs):
"""
Calculates the reward for a given action and observation in the CartPole environment.
Args:
action (ndarray): A 1D array representing the force applied to the cart (-15 to 15).
obs (ndarray): A 1D array representing the observation of the environment [cart_position, pole_angle, cart_velocity, pole_angular_velocity].
Returns:
float: The reward value.
"""
cart_pos = obs[0]
pole_angle = obs[1]
pole_vel = obs[3]
# Reward for keeping the pole upright
rew = -10 * np.abs((pole_angle - np.pi))
# Penalize large cart movements
rew -= 0.1 * np.abs(cart_pos)
# Encourage stillness by penalizing excess force used
rew -= 0.01 * np.abs(action)
return rew