hope_agents 0.1.0

HOPE Agents: Hierarchical Optimizing Policy Engine for AIngle AI agents
Documentation
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# HOPE Agents

**H**ierarchical **O**ptimizing **P**olicy **E**ngine for AIngle AI agents.

[![Tests](https://img.shields.io/badge/tests-133%20passing-brightgreen)](tests)
[![Documentation](https://img.shields.io/badge/docs-rust-blue)](https://docs.rs/hope_agents)
[![License](https://img.shields.io/badge/license-Apache%202.0-blue)](LICENSE)

## Overview

HOPE Agents is a complete reinforcement learning framework for building autonomous AI agents that can:

- 🧠 **Learn** from experience using Q-Learning, SARSA, and TD algorithms
- 🎯 **Plan** hierarchically with goal decomposition and conflict resolution
- 🔮 **Predict** future states and detect anomalies
- 🤝 **Coordinate** with other agents through message passing and shared memory
- 💾 **Persist** state for checkpointing and transfer learning

## Architecture

```text
┌─────────────────────────────────────────────────────────────┐
│                      HOPE Agent                             │
├─────────────────────────────────────────────────────────────┤
│                                                              │
│  Observation → State → Decision → Action → Learning         │
│                                                              │
│  ┌──────────────┐  ┌──────────────┐  ┌──────────────────┐  │
│  │  Predictive  │  │ Hierarchical │  │    Learning      │  │
│  │    Model     │  │ Goal Solver  │  │     Engine       │  │
│  │              │  │              │  │                  │  │
│  │ • Anomaly    │  │ • Goals      │  │ • Q-Learning     │  │
│  │ • Forecast   │  │ • Planning   │  │ • SARSA          │  │
│  │ • Patterns   │  │ • Conflicts  │  │ • TD Learning    │  │
│  │              │  │              │  │ • Experience     │  │
│  └──────────────┘  └──────────────┘  └──────────────────┘  │
│                                                              │
└─────────────────────────────────────────────────────────────┘
```

## Features

### ✅ Complete (100%)

#### 1. Learning Engine
- **Algorithms**: Q-Learning, SARSA, TD(λ), Expected SARSA
- **Experience Replay**: Prioritized replay buffer for efficient learning
- **Exploration**: Epsilon-greedy and Boltzmann exploration
- **Value Functions**: Tabular and linear function approximation

#### 2. Hierarchical Goal Solver
- **Goal Types**: Achieve, Maintain, Avoid, Explore
- **Decomposition**: Automatic goal breakdown into subgoals
- **Conflict Resolution**: Detect and resolve goal conflicts
- **Priorities**: Support for goal prioritization

#### 3. Predictive Model
- **State Prediction**: Forecast next states given actions
- **Anomaly Detection**: Statistical anomaly detection with z-scores
- **Trajectory Planning**: Multi-step lookahead
- **Transition Model**: Learn state transition dynamics

#### 4. Multi-Agent Coordination
- **Message Passing**: Broadcast and direct messaging
- **Shared Memory**: Global key-value store for coordination
- **Consensus**: Voting-based group decision making
- **Agent Registry**: Dynamic agent registration/unregistration

#### 5. State Persistence
- **Formats**: JSON, Binary, MessagePack
- **Compression**: Optional compression for efficient storage
- **Checkpointing**: Automatic periodic checkpointing
- **Transfer Learning**: Save and load trained agents

## Quick Start

### Simple Reactive Agent

```rust
use hope_agents::{Agent, SimpleAgent, Goal, Observation, Rule, Condition, Action};

// Create a simple reactive agent
let mut agent = SimpleAgent::new("sensor_monitor");

// Add a rule: if temperature > 30, alert
let rule = Rule::new(
    "high_temp",
    Condition::above("temperature", 30.0),
    Action::alert("Temperature too high!"),
);
agent.add_rule(rule);

// Process observations
let obs = Observation::sensor("temperature", 35.0);
agent.observe(obs.clone());
let action = agent.decide();
let result = agent.execute(action.clone());
agent.learn(&obs, &action, &result);
```

### HOPE Agent with Learning

```rust
use hope_agents::{HopeAgent, Observation, Goal, Priority, Outcome, ActionResult};

// Create a HOPE agent with learning, prediction, and hierarchical goals
let mut agent = HopeAgent::with_default_config();

// Set a goal
let goal = Goal::maintain("temperature", 20.0..25.0)
    .with_priority(Priority::High);
agent.set_goal(goal);

// Agent loop with reinforcement learning
for episode in 0..100 {
    let obs = Observation::sensor("temperature", 22.0);
    let action = agent.step(obs.clone());

    // Execute action in environment and get reward
    let reward = 1.0;
    let next_obs = Observation::sensor("temperature", 21.0);
    let result = ActionResult::success(&action.id);

    let outcome = Outcome::new(action, result, reward, next_obs, false);
    agent.learn(outcome);
}

// Check statistics
let stats = agent.get_statistics();
println!("Episodes: {}", stats.episodes_completed);
println!("Success rate: {:.2}%", stats.success_rate * 100.0);
```

### Multi-Agent Coordination

```rust
use hope_agents::{AgentCoordinator, HopeAgent, Message, Observation};
use std::collections::HashMap;

// Create coordinator
let mut coordinator = AgentCoordinator::new();

// Register agents
let agent1 = HopeAgent::with_default_config();
let agent2 = HopeAgent::with_default_config();

let id1 = coordinator.register_agent(agent1);
let id2 = coordinator.register_agent(agent2);

// Broadcast message
coordinator.broadcast(Message::new("update", "System status changed"));

// Step all agents
let mut observations = HashMap::new();
observations.insert(id1, Observation::sensor("temp", 20.0));
observations.insert(id2, Observation::sensor("humidity", 60.0));

let actions = coordinator.step_all(observations);
```

### State Persistence

```rust
use hope_agents::{HopeAgent, AgentPersistence, CheckpointManager};
use std::path::Path;

let mut agent = HopeAgent::with_default_config();

// Train the agent...

// Save agent state
agent.save_to_file(Path::new("agent_state.json")).unwrap();

// Later, load agent state
let loaded_agent = HopeAgent::load_from_file(Path::new("agent_state.json")).unwrap();

// Or use checkpoint manager for automatic checkpointing
let mut manager = CheckpointManager::new(Path::new("checkpoints"), 5)
    .with_interval(1000);

// During training
for step in 0..10000 {
    // ... train agent ...

    if manager.should_checkpoint(step) {
        manager.save_checkpoint(&agent, step).unwrap();
    }
}
```

## Operation Modes

HOPE agents support multiple operation modes:

- **Exploration**: High exploration rate for discovering new strategies
- **Exploitation**: Use learned knowledge for optimal performance
- **GoalDriven**: Balance exploration with goal achievement
- **Adaptive**: Automatically switch modes based on performance

```rust
agent.set_mode(OperationMode::Exploration);  // High exploration
agent.set_mode(OperationMode::Exploitation); // Pure exploitation
agent.set_mode(OperationMode::Adaptive);     // Auto-adjust
```

## Goal Management

### Goal Types

```rust
// Achieve a target value
let goal = Goal::achieve("temperature", 25.0);

// Maintain value in range
let goal = Goal::maintain("humidity", 40.0..60.0);

// Avoid certain values
let goal = Goal::avoid("pressure", 100.0);

// Explore and discover
let goal = Goal::explore("new_area");
```

### Hierarchical Goals

Goals are automatically decomposed into subgoals:

```rust
let parent = Goal::achieve("optimize_system", 1.0)
    .with_priority(Priority::High);

let goal_id = agent.set_goal(parent);

// Automatically creates subgoals for different aspects
let active_goals = agent.active_goals();
```

## Consensus and Coordination

Agents can make group decisions through voting:

```rust
let mut coordinator = AgentCoordinator::new();

// Create proposal
let proposal_id = coordinator.create_proposal(
    "new_policy",
    "Should we adopt the new temperature policy?"
);

// Agents vote
// ... (voting happens through message passing) ...

// Check consensus
match coordinator.get_consensus(&proposal_id) {
    Some(ConsensusResult::Decided { approved, votes_for, votes_against, .. }) => {
        println!("Decision: {}", if approved { "Approved" } else { "Rejected" });
        println!("Votes: {} for, {} against", votes_for, votes_against);
    }
    _ => println!("Voting in progress..."),
}
```

## Configuration

### IoT Mode

Optimized for resource-constrained devices:

```rust
use hope_agents::AgentConfig;

let config = AgentConfig::iot_mode();
let agent = SimpleAgent::with_config("iot_agent", config);

// Features:
// - Limited memory (128KB)
// - Disabled learning
// - Reduced buffer sizes
```

### Custom Configuration

```rust
use hope_agents::{HopeConfig, LearningConfig, PredictiveConfig, LearningAlgorithm};

let config = HopeConfig {
    learning: LearningConfig {
        learning_rate: 0.1,
        discount_factor: 0.95,
        algorithm: LearningAlgorithm::QLearning,
        epsilon: 0.2,
        ..Default::default()
    },
    predictive: PredictiveConfig {
        history_size: 500,
        ..Default::default()
    },
    anomaly_sensitivity: 0.8,
    auto_decompose_goals: true,
    ..Default::default()
};

let agent = HopeAgent::new(config);
```

## Performance

HOPE Agents is designed for high performance:

- **1000+ steps/second** on modern hardware
- **Efficient memory usage** with configurable limits
- **Incremental learning** with no batch processing required
- **Lock-free coordination** for multi-agent scenarios

### Benchmarks

Run benchmarks with:

```bash
cargo bench
```

## Testing

Comprehensive test suite with 133 tests covering:

- Unit tests for all components
- Integration tests for complete workflows
- Multi-agent coordination scenarios
- Persistence roundtrip tests
- Performance tests

Run tests:

```bash
# All tests
cargo test

# Specific module
cargo test coordination
cargo test persistence

# Integration tests
cargo test --test integration_test

# With output
cargo test -- --nocapture
```

## Examples

See the `tests/integration_test.rs` file for comprehensive examples demonstrating:

- Simple agent workflows
- Learning cycles with multiple episodes
- Multi-agent coordination
- Consensus mechanisms
- State persistence and checkpointing
- Anomaly detection
- Operation mode switching

## API Documentation

Full API documentation is available at [docs.rs/hope_agents](https://docs.rs/hope_agents).

Generate local documentation:

```bash
cargo doc --open
```

## Integration with AIngle

HOPE Agents integrates seamlessly with the AIngle network:

```rust
// Observe network events
let obs = Observation::network_event("node_joined", node_id);

// Execute actions on the network
let action = Action::send_message("Hello, network!");

// Store data in AIngle
let action = Action::store_data(serde_json::to_string(&data).unwrap());
```

## Roadmap

Future enhancements (beyond 100%):

- [ ] Deep Q-Networks (DQN) support
- [ ] Policy gradient methods (PPO, A3C)
- [ ] Multi-objective optimization
- [ ] Distributed training
- [ ] Web assembly support
- [ ] Python bindings

## Contributing

Contributions are welcome! Please see CONTRIBUTING.md for guidelines.

## License

Licensed under the Apache License, Version 2.0. See LICENSE for details.

## Citation

If you use HOPE Agents in your research, please cite:

```bibtex
@software{hope_agents,
  title = {HOPE Agents: Hierarchical Optimizing Policy Engine},
  author = {Apilium Technologies},
  year = {2025},
  url = {https://github.com/ApiliumCode/aingle}
}
```

## Support

- Documentation: https://docs.rs/hope_agents
- Issues: https://github.com/ApiliumCode/aingle/issues

---

**Status**: ✅ **100% Complete**

All core features implemented, tested, and documented.