- hive_mind_enhanced.py - Main simulation (Phase 1)
- genetic_optimization.py - Weight optimization (Phase 2 Bonus)
- AIAssignment2.py - Your original implementation
Hero must navigate grid, retrieve the Gem, and reach the Exit while evading coordinated Monster swarm.
Hero (Blue)
- HP: 100
- Ammo: 5
- Attack Range: 3 cells (ranged)
- Vision: 5 cells radius
- Actions: Move, Fire, Interact
Monsters (Red)
- Count: 3
- Attack Range: 1 cell (melee)
- Damage: 20 HP
- Vision: 5 cells radius
- Behavior: Hive mind with shared perception
- Grid: 10x10
- Fog of War: Only visible within hero's vision radius
- Walls: Obstacles blocking movement
- Gem: Must collect before exiting
- Exit: Victory condition
Algorithm:
def alpha_beta_search(state):
for each possible action:
v = min_value(next_state, alpha, beta, depth)
if v > best_score:
best_score, best_action = v, action
alpha = max(alpha, v)
return best_actionEvaluation Function E(s):
E(s) = w1 * objective_distance
+ w2 * threat_proximity
+ w3 * resource_value
+ w4 * loop_penalty
Where:
- Objective: Distance to Gem (if not collected) or Exit (if collected)
- Threat: Proximity to nearest monster
- Resource: HP and Ammo values
- Loop: Penalty for revisiting positions
Key Features:
- Depth-limited search (depth = 3)
- Alpha-beta pruning for efficiency
- Considers Move, Fire, and Interact actions
- Tracks nodes explored and branches pruned
Shared Perception:
- If ANY monster sees the hero, ALL monsters know hero location
- Breaks fog of war for the swarm
- Implements true cooperative behavior
Utility Function:
U(move) = w1 * pursuit(move)
+ w2 * separation(move)
+ w3 * aggression(move)
Where:
- Pursuit: -distance_to_hero (closer = higher)
- Separation: Penalties for clustering with other monsters
- Aggression: Bonus for entering attack range (1 cell)
Default Weights: [1.5, 0.8, 2.0]
Behaviors:
- Pursuit: Minimize distance to hero (or last known position)
- Separation: Avoid clustering (penalties for adjacent monsters)
- Aggression: Bonus for moving into melee range
Objective: Optimize utility weights to minimize turns needed to eliminate hero
Algorithm:
1. Initialize random population of weight sets
2. For each generation:
a. Evaluate fitness (avg turns to eliminate hero)
b. Select parents (tournament selection)
c. Crossover (single-point)
d. Mutate (gaussian noise)
e. Apply elitism (keep best 2)
3. Return best weights found
Parameters:
- Population size: 20
- Generations: 25
- Mutation rate: 0.15
- Weight bounds: [0.1-5.0, 0.1-3.0, 0.1-5.0]
Evolution Tracking:
- Best fitness per generation
- Average fitness per generation
- Final comparison: original vs optimized weights
python hive_mind_enhanced.pyExpected Output:
- Live matplotlib visualization with fog of war
- Turn-by-turn console output
- Hero actions and monster movements
- Alpha-beta pruning statistics
- Victory/defeat message
python genetic_optimization.pyExpected Output:
- Generation-by-generation evolution
- Best/average fitness tracking
- Final optimized weights
- Performance comparison graph (ga_evolution.png)
- Before/after testing results
Note: Takes 5-10 minutes to complete
- Nodes Explored: Total game states evaluated
- Branches Pruned: States skipped due to pruning
- Efficiency: Higher pruning = faster decisions
- Best Fitness: Minimum turns to eliminate hero
- Average Fitness: Population average performance
- Convergence: Decreasing fitness over generations
- Grid-based environment (10x10)
- Hero with Minimax implementation
- Monster swarm with cooperative behavior
- Shared perception (hive mind)
- Fog of war visualization
- Resource management (HP, Ammo)
- Combat mechanics (ranged vs melee)
- Loop prevention
- Matplotlib visualization
- Distance-based utility function
- Threat assessment
- Separation behavior
- Genetic Algorithm implementation
- Weight optimization
- Fitness evaluation
- Population evolution
- Crossover and mutation
- Evolution tracking
- Performance comparison
Make Hero Stronger:
HERO_MAX_HP = 150 # More survivability
HERO_START_AMMO = 10 # More firepower
HERO_ATTACK_RANGE = 4 # Better rangeMake Monsters Harder:
MONSTER_ATTACK_DMG = 30 # Higher damage
monsters = [(5,5), (6,6), (7,2), (4,4)] # More monstersAdjust AI Depth:
hero.depth_limit = 4 # Deeper search (slower but smarter)Manual Weight Testing:
swarm = MonsterSwarm()
swarm.weights = [2.0, 0.5, 3.0] # [Pursuit, Separation, Aggression]GA Parameters:
ga = GeneticAlgorithm(
population_size=30, # Larger population
generations=50, # More evolution
mutation_rate=0.2 # Higher mutation
)GameState:
- Grid management
- Entity positions
- Legal move generation
- Visibility/fog of war
- Shared perception updates
HeroAgent:
- Minimax search
- Alpha-beta pruning
- Evaluation function
- Action simulation
- Terminal state checking
MonsterSwarm:
- Utility calculation
- Move selection
- Cooperative behavior
- Weight-based decision making
GeneticAlgorithm:
- Population management
- Fitness evaluation
- Selection operators
- Crossover and mutation
- Evolution tracking
Simulation freezes:
- Reduce hero depth_limit to 2
- Reduce monster count
- Check for infinite loops in evaluation function
GA too slow:
- Reduce population_size to 15
- Reduce generations to 20
- Reduce num_trials in fitness function to 3
Hero always loses:
- Increase HERO_MAX_HP
- Decrease MONSTER_ATTACK_DMG
- Increase HERO_START_AMMO
Monsters not coordinating:
- Check swarm_knows_hero_pos is updating
- Verify is_visible() function
- Test different weight combinations
Hero Victory:
- Turns: 15-30
- Requires: Good positioning, ammo management
- Strategy: Avoid combat, prioritize objectives
Hero Defeat:
- Turns: 10-20
- Causes: Cornered by swarm, low HP, out of ammo
Original Weights: [1.5, 0.8, 2.0]
- Avg turns to eliminate: 25-35
Optimized Weights: ~[2.5, 0.4, 3.5] (example)
- Avg turns to eliminate: 18-25
- Improvement: ~20-30%
Search Algorithms:
- Explain minimax recursion (max/min alternating)
- Show alpha-beta pruning with example
- Quantify pruning efficiency (% branches cut)
Evaluation Function:
- Write E(s) formula with actual weights
- Justify each component (why valuable?)
- Show example calculations for different states
Data Representation:
- Tensor shape: (batch, height, width, channels)
- Example: (1, 10, 10, 8) for 8 feature maps
- Channels: Hero, Monsters, Gem, Exit, Walls, HP, Ammo, Fog
Network Architecture:
- Input: Game state tensor
- Shared backbone: Conv layers
- Value Head: Predicts state value (replaces E(s))
- Policy Head: Predicts action probabilities
Learning Paradigms:
- Supervised: Train on expert games (minimax decisions)
- Reinforcement: Self-play with rewards (+1 win, -1 loss)
- Credit Assignment: How to attribute long-term outcomes to actions
Interpretability:
- Heuristic: Can trace why each weight matters
- Neural: Black box, requires attention/saliency maps
Adaptability:
- Heuristic: Manual reweighting for rule changes
- Neural: Automatic retraining on new data
- Minimax: Russell & Norvig, AIMA Ch. 5
- Alpha-Beta Pruning: Knuth & Moore (1975)
- Genetic Algorithms: Goldberg (1989)
- AlphaZero: Silver et al. (2018)
- Utility-based Agents: AIMA Ch. 16
- Visual Clarity - Clear text-based visualization
- Turn Counter - Track game progress
- Detection Status - Show when hero is spotted
- Pruning Metrics - Performance monitoring
- Evolution Graphs - GA convergence visualization
- Weight Comparison - Before/after analysis
- Status Bar - Real-time game state info
540A
- Run both files and collect screenshots
- Record metrics (nodes explored, turns, etc.)
- Analyze GA results - did it converge?
- Compare weights - what changed and why?
- Test edge cases - what happens with extreme weights?
- Document findings in your report
Good luck with your project!