Artificial-Intelligence-Computer-Vision/step_function_model_executables
0
1import numpy as np2 3 4class 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_path9 self.state_space = state_space10 self.num_actions = 311 12 # Initialize mock model weights for demonstration13 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 state26 Returns: array of Q-values for each action27 """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 = state34 x = np.tanh(x @ self.model_weights["layer1"]) # Hidden layer 135 x = np.tanh(x @ self.model_weights["layer2"]) # Hidden layer 236 q_values = x @ self.model_weights["output"] # Output layer37 38 return q_values39 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 environment52 Returns: (reward, next_state, done)53 """54 # Mock environment step55 reward = np.random.randn()56 next_state = np.random.randn(self.state_space)57 done = False58 return reward, next_state, done59 60 def predict_action_state(self, state):61 """62 Predict next action and state63 Returns: (next_state, action)64 """65 # Uncomment to use model predictions instead of random66 # 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, action70 71 72class 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_steps78 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_state84 85 print("=" * 80)86 print("Running Prediction Demonstration")87 print("=" * 80)88 89 for i in range(self.num_steps):90 # Get predictions91 state_reshaped = state.reshape(-1, *state.shape)92 q_values = self.predict(state_reshaped)[0]93 94 # Get next state and action95 next_state, action = self.predict_action_state(state)96 97 # Display results98 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 values102 103 # Update state for next iteration104 state = next_state105 106 print("\n" + "=" * 80)107 108 109# Example usage110if __name__ == "__main__":111 # Create and run the display system112 display_system = DisplayActionsState(state_space=10, num_steps=10)113 