File size: 12,093 Bytes
18bcdea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import tensorflow as tf
import keras
from keras import layers, Model
import numpy as np
import tensorflow_probability as tfp
import os
import traceback 

tfd = tfp.distributions

@tf.keras.utils.register_keras_serializable()
class Actor(Model):
    def __init__(self, obs_shape, action_size, hidden_layer_sizes=[512, 512, 512], **kwargs):
        super().__init__(**kwargs)

        if len(obs_shape) > 1:
            self.flatten = layers.Flatten(input_shape=obs_shape)
        
            self.flatten(tf.zeros((1,) + obs_shape))
        else:
            self.flatten = None 
        
        self.dense_layers = []
        for size in hidden_layer_sizes:
            self.dense_layers.append(layers.Dense(size, activation='relu'))
        self.logits = layers.Dense(action_size)

        self._obs_shape = obs_shape
        self._action_size = action_size
        self._hidden_layer_sizes = hidden_layer_sizes

    def call(self, inputs):
    
        x = self.flatten(inputs) if self.flatten else inputs
        for layer in self.dense_layers:
            x = layer(x)
        return self.logits(x)

    def get_config(self):
        config = super().get_config()
        config.update({
            'obs_shape': self._obs_shape,
            'action_size': self._action_size,
            'hidden_layer_sizes': self._hidden_layer_sizes
        })
        return config

@tf.keras.utils.register_keras_serializable()
class Critic(Model):
    def __init__(self, obs_shape, hidden_layer_sizes=[512, 512, 512], **kwargs):
        super().__init__(**kwargs)

        if len(obs_shape) > 1:
            self.flatten = layers.Flatten(input_shape=obs_shape)
            self.flatten(tf.zeros((1,) + obs_shape))
        else:
            self.flatten = None
        
        self.dense_layers = []
        for size in hidden_layer_sizes:
            self.dense_layers.append(layers.Dense(size, activation='relu'))
        self.value = layers.Dense(1)

        self._obs_shape = obs_shape
        self._hidden_layer_sizes = hidden_layer_sizes

    def call(self, inputs):
        x = self.flatten(inputs) if self.flatten else inputs
        for layer in self.dense_layers:
            x = layer(x)
        return self.value(x)

    def get_config(self):
        config = super().get_config()
        config.update({
            'obs_shape': self._obs_shape,
            'hidden_layer_sizes': self._hidden_layer_sizes
        })
        return config

class PPOAgent:
    def __init__(self, observation_space_shape, action_space_size,
                 actor_lr=3e-4, critic_lr=3e-4, gamma=0.99,
                 gae_lambda=0.95, clip_epsilon=0.2,
                 num_epochs_per_update=10, batch_size=64,
                 hidden_layer_sizes=[512, 512, 512]):
        
        self.gamma = gamma
        self.gae_lambda = gae_lambda
        self.clip_epsilon = clip_epsilon
        self.num_epochs_per_update = num_epochs_per_update
        self.batch_size = batch_size

        self.observation_space_shape = observation_space_shape 
        self.action_space_size = action_space_size 

        self.actor = Actor(observation_space_shape, action_space_size, hidden_layer_sizes=hidden_layer_sizes)
        self.critic = Critic(observation_space_shape, hidden_layer_sizes=hidden_layer_sizes)

        self.actor_optimizer = tf.keras.optimizers.Adam(learning_rate=actor_lr)
        self.critic_optimizer = tf.keras.optimizers.Adam(learning_rate=critic_lr)

        self.states = []
        self.actions = []
        self.rewards = []
        self.next_states = []
        self.dones = []
        self.log_probs = []
        self.values = []

        dummy_obs = tf.zeros((1,) + observation_space_shape, dtype=tf.float32)
        self.actor(dummy_obs)
        self.critic(dummy_obs)

    def remember(self, state, action, reward, next_state, done, log_prob, value):
        self.states.append(state)
        self.actions.append(action)
        self.rewards.append(reward)
        self.next_states.append(next_state)
        self.dones.append(done)
        self.log_probs.append(log_prob)
        self.values.append(value)

    @tf.function

    def _choose_action_tf(self, observation, action_mask):
        observation = tf.expand_dims(tf.convert_to_tensor(observation, dtype=tf.float32), 0)
        
        pi_logits = self.actor(observation)
        
        masked_logits = tf.where(action_mask, pi_logits, -1e9)
        
        value = self.critic(observation)
        
        distribution = tfd.Categorical(logits=masked_logits)
        
        action = distribution.sample()
        log_prob = distribution.log_prob(action)
        
        return action, log_prob, value

    def choose_action(self, observation, action_mask):
        action_tensor, log_prob_tensor, value_tensor = self._choose_action_tf(observation, action_mask)
        
        return action_tensor.numpy(), log_prob_tensor.numpy(), value_tensor.numpy()[0,0]

    def calculate_advantages_and_returns(self):
        rewards = np.array(self.rewards, dtype=np.float32)
        values = np.array(self.values, dtype=np.float32)
        dones = np.array(self.dones, dtype=np.float32)
        
        last_next_state_value = self.critic(tf.expand_dims(tf.convert_to_tensor(self.next_states[-1], dtype=tf.float32), 0)).numpy()[0,0] if not dones[-1] else 0
        next_values = np.append(values[1:], last_next_state_value)
        
        advantages = []
        returns = []
        
        last_advantage = 0
        for t in reversed(range(len(rewards))):
            delta = rewards[t] + self.gamma * next_values[t] * (1 - dones[t]) - values[t]
          
            advantage = delta + self.gae_lambda * self.gamma * (1 - dones[t]) * last_advantage
            advantages.insert(0, advantage)

            returns.insert(0, advantage + values[t])
            last_advantage = advantage
            
        return np.array(advantages, dtype=np.float32), np.array(returns, dtype=np.float32)

    def learn(self):
        if not self.states:
            return

        states = tf.convert_to_tensor(np.array(self.states), dtype=tf.float32)
        actions = tf.convert_to_tensor(np.array(self.actions), dtype=tf.int32)
        old_log_probs = tf.convert_to_tensor(np.array(self.log_probs), dtype=tf.float32)
        
        advantages, returns = self.calculate_advantages_and_returns()
        advantages = (advantages - tf.reduce_mean(advantages)) / (tf.math.reduce_std(advantages) + 1e-8)
        
        dataset = tf.data.Dataset.from_tensor_slices((states, actions, old_log_probs, advantages, returns))
        dataset = dataset.shuffle(buffer_size=len(self.states)).batch(self.batch_size)

        for _ in range(self.num_epochs_per_update):
            for batch_states, batch_actions, batch_old_log_probs, batch_advantages, batch_returns in dataset:
    
                with tf.GradientTape() as tape:
                    current_logits = self.actor(batch_states)
                    
                    new_distribution = tfd.Categorical(logits=current_logits) 
                    
                    new_log_probs = new_distribution.log_prob(batch_actions)
                    ratio = tf.exp(new_log_probs - batch_old_log_probs)
                                        
                    surrogate1 = ratio * batch_advantages
                    surrogate2 = tf.clip_by_value(ratio, 1 - self.clip_epsilon, 1 + self.clip_epsilon) * batch_advantages
                                        
                    actor_loss = -tf.reduce_mean(tf.minimum(surrogate1, surrogate2))
                                
                actor_grads = tape.gradient(actor_loss, self.actor.trainable_variables)
                self.actor_optimizer.apply_gradients(zip(actor_grads, self.actor.trainable_variables))

                with tf.GradientTape() as tape:
                    new_values = self.critic(batch_states)
                    critic_loss = tf.reduce_mean(tf.square(new_values - batch_returns))
                                
                critic_grads = tape.gradient(critic_loss, self.critic.trainable_variables)
                self.critic_optimizer.apply_gradients(zip(critic_grads, self.critic.trainable_variables))
        
        self.states = []
        self.actions = []
        self.rewards = []
        self.next_states = []
        self.dones = []
        self.log_probs = []
        self.values = []

    def save_models(self, path):
        actor_save_path = f"{path}_actor.keras"
        critic_save_path = f"{path}_critic.keras"
        print(f"\n--- Attempting to save models ---")
        print(f"Target Actor path: {os.path.abspath(actor_save_path)}")
        print(f"Target Critic path: {os.path.abspath(critic_save_path)}")
        try:
            self.actor.save(actor_save_path)
            print(f"Actor model saved successfully to {os.path.abspath(actor_save_path)}")
        except Exception as e:
            print(f"ERROR: Failed to save Actor model to {os.path.abspath(actor_save_path)}")
            print(f"Reason: {e}")
            traceback.print_exc()
        try:
            self.critic.save(critic_save_path)
            print(f"Critic model saved successfully to {os.path.abspath(critic_save_path)}")
        except Exception as e:
            print(f"ERROR: Failed to save Critic model to {os.path.abspath(critic_save_path)}")
            print(f"Reason: {e}")
            traceback.print_exc()
        print(f"--- Models save process completed ---\n")

    def load_models(self, path):
    
        actor_load_path = f"{path}_actor.keras"
        critic_load_path = f"{path}_critic.keras"
        actor_loaded_ok = False
        critic_loaded_ok = False

        custom_objects = {
            'Actor': Actor,
            'Critic': Critic
        }
        
        try:

            self.actor = tf.keras.models.load_model(actor_load_path, custom_objects=custom_objects)
            actor_loaded_ok = True
            print(f"Actor model loaded from: {os.path.abspath(actor_load_path)}")
        except Exception as e:
            print(f"ERROR: Failed to load Actor model from {os.path.abspath(actor_load_path)}")
            print(f"Reason: {e}")
            traceback.print_exc()

        try:
            self.critic = tf.keras.models.load_model(critic_load_path, custom_objects=custom_objects)
            critic_loaded_ok = True
            print(f"Critic model loaded from: {os.path.abspath(critic_load_path)}")
        except Exception as e:
            print(f"ERROR: Failed to load Critic model from {os.path.abspath(critic_load_path)}")
            print(f"Reason: {e}")
            traceback.print_exc()

        if actor_loaded_ok and critic_loaded_ok:
            print(f"All PPO models loaded successfully from '{path}'.")
            return True
        else:
            print(f"Warning: One or both models failed to load. The agent will use untrained models.")
            return False