Upload 2 files
Browse files- bellande_step.py +112 -0
- bellande_step_model.h5 +3 -0
bellande_step.py
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
class LocalPredictionModel:
|
| 5 |
+
"""Simplified local version of the prediction model"""
|
| 6 |
+
|
| 7 |
+
def __init__(self, model_path="bellande_step_model.h5", state_space=10):
|
| 8 |
+
self.model_path = model_path
|
| 9 |
+
self.state_space = state_space
|
| 10 |
+
self.num_actions = 3
|
| 11 |
+
|
| 12 |
+
# Initialize mock model weights for demonstration
|
| 13 |
+
self.model_weights = self._initialize_mock_weights()
|
| 14 |
+
|
| 15 |
+
def _initialize_mock_weights(self):
|
| 16 |
+
"""Initialize random weights to simulate a trained model"""
|
| 17 |
+
return {
|
| 18 |
+
"layer1": np.random.randn(self.state_space, 64) * 0.1,
|
| 19 |
+
"layer2": np.random.randn(64, 32) * 0.1,
|
| 20 |
+
"output": np.random.randn(32, self.num_actions) * 0.1,
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
def predict(self, state):
|
| 24 |
+
"""
|
| 25 |
+
Predict Q-values for given state
|
| 26 |
+
Returns: array of Q-values for each action
|
| 27 |
+
"""
|
| 28 |
+
# Ensure state is 2D: (batch_size, state_space)
|
| 29 |
+
if state.ndim == 1:
|
| 30 |
+
state = state.reshape(1, -1)
|
| 31 |
+
|
| 32 |
+
# Simple feedforward prediction (mock DQN)
|
| 33 |
+
x = state
|
| 34 |
+
x = np.tanh(x @ self.model_weights["layer1"]) # Hidden layer 1
|
| 35 |
+
x = np.tanh(x @ self.model_weights["layer2"]) # Hidden layer 2
|
| 36 |
+
q_values = x @ self.model_weights["output"] # Output layer
|
| 37 |
+
|
| 38 |
+
return q_values
|
| 39 |
+
|
| 40 |
+
def get_q_values(self, state):
|
| 41 |
+
"""Get the best action based on Q-values"""
|
| 42 |
+
q_values = self.predict(state)
|
| 43 |
+
return np.argmax(q_values[0])
|
| 44 |
+
|
| 45 |
+
def reset(self):
|
| 46 |
+
"""Reset environment to initial state"""
|
| 47 |
+
return np.random.randn(self.state_space)
|
| 48 |
+
|
| 49 |
+
def step(self, action):
|
| 50 |
+
"""
|
| 51 |
+
Take a step in the environment
|
| 52 |
+
Returns: (reward, next_state, done)
|
| 53 |
+
"""
|
| 54 |
+
# Mock environment step
|
| 55 |
+
reward = np.random.randn()
|
| 56 |
+
next_state = np.random.randn(self.state_space)
|
| 57 |
+
done = False
|
| 58 |
+
return reward, next_state, done
|
| 59 |
+
|
| 60 |
+
def predict_action_state(self, state):
|
| 61 |
+
"""
|
| 62 |
+
Predict next action and state
|
| 63 |
+
Returns: (next_state, action)
|
| 64 |
+
"""
|
| 65 |
+
# Uncomment to use model predictions instead of random
|
| 66 |
+
# action = self.get_q_values(state)
|
| 67 |
+
action = np.random.randint(0, self.num_actions)
|
| 68 |
+
_, next_state, _ = self.step(action)
|
| 69 |
+
return next_state, action
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
class DisplayActionsState(LocalPredictionModel):
|
| 73 |
+
"""Display actions and states for visualization"""
|
| 74 |
+
|
| 75 |
+
def __init__(self, state_space=10, num_steps=10):
|
| 76 |
+
super().__init__(state_space=state_space)
|
| 77 |
+
self.num_steps = num_steps
|
| 78 |
+
self.starting_state = self.reset()
|
| 79 |
+
self.run_demonstration()
|
| 80 |
+
|
| 81 |
+
def run_demonstration(self):
|
| 82 |
+
"""Run and print predictions for demonstration"""
|
| 83 |
+
state = self.starting_state
|
| 84 |
+
|
| 85 |
+
print("=" * 80)
|
| 86 |
+
print("Running Prediction Demonstration")
|
| 87 |
+
print("=" * 80)
|
| 88 |
+
|
| 89 |
+
for i in range(self.num_steps):
|
| 90 |
+
# Get predictions
|
| 91 |
+
state_reshaped = state.reshape(-1, *state.shape)
|
| 92 |
+
q_values = self.predict(state_reshaped)[0]
|
| 93 |
+
|
| 94 |
+
# Get next state and action
|
| 95 |
+
next_state, action = self.predict_action_state(state)
|
| 96 |
+
|
| 97 |
+
# Display results
|
| 98 |
+
print(f"\nStep {i + 1}:")
|
| 99 |
+
print(f" Q-values: {q_values}")
|
| 100 |
+
print(f" Action: {action}")
|
| 101 |
+
print(f" Next State: {next_state[:5]}...") # Show first 5 values
|
| 102 |
+
|
| 103 |
+
# Update state for next iteration
|
| 104 |
+
state = next_state
|
| 105 |
+
|
| 106 |
+
print("\n" + "=" * 80)
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
# Example usage
|
| 110 |
+
if __name__ == "__main__":
|
| 111 |
+
# Create and run the display system
|
| 112 |
+
display_system = DisplayActionsState(state_space=10, num_steps=10)
|
bellande_step_model.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:e5614030d7bda8bca8f5b923d2f197dd0a9e5e6496e1fcc971c295fe3e0c4132
|
| 3 |
+
size 155001
|