Skip to content

Commit 1add5ad

Browse files
committed
Fix features cloning
1 parent 5f97995 commit 1add5ad

1 file changed

Lines changed: 27 additions & 26 deletions

File tree

src/darwin/exploration.rs

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
use anyhow::{anyhow, Result};
2+
use dashmap::DashMap;
3+
use rand::prelude::*;
24
use std::collections::{HashMap, HashSet};
35
use std::sync::Arc;
4-
use tracing::{info, warn, error, debug};
5-
use uuid::Uuid;
6-
use dashmap::DashMap;
76
use tokio::sync::RwLock;
8-
use rand::prelude::*;
9-
10-
7+
use tracing::{debug, error, info, warn};
8+
use uuid::Uuid;
119

1210
use crate::core::metrics::MetricsCollector;
1311
use crate::darwin::self_improvement::Modification;
@@ -20,7 +18,7 @@ pub struct ExplorationStrategy {
2018

2119
/// Archive of previously explored solutions
2220
archive: DashMap<String, ArchiveEntry>,
23-
21+
2422
/// Current exploration parameters
2523
parameters: RwLock<ExplorationParameters>,
2624

@@ -320,8 +318,10 @@ impl ExplorationStrategy {
320318
}
321319

322320
/// Tournament selection for choosing parents
323-
async fn tournament_selection(&self, archive: &DashMap<String, ArchiveEntry>) -> Option<ArchiveEntry> {
324-
321+
async fn tournament_selection(
322+
&self,
323+
archive: &DashMap<String, ArchiveEntry>,
324+
) -> Option<ArchiveEntry> {
325325
let params = self.parameters.read().await;
326326
let mut rng = rand::thread_rng();
327327

@@ -330,7 +330,6 @@ impl ExplorationStrategy {
330330
return archive.iter().next().map(|e| e.value().clone());
331331
}
332332

333-
334333
let entries: Vec<ArchiveEntry> = archive.iter().map(|e| e.value().clone()).collect();
335334

336335
// Select tournament_size random entries
@@ -344,14 +343,12 @@ impl ExplorationStrategy {
344343
}
345344

346345
// Find the best entry in the tournament
347-
tournament
348-
.into_iter()
349-
.max_by(|a, b| {
350-
// Compare based on sum of metrics (higher is better)
351-
let a_score: f32 = a.metrics.values().sum();
352-
let b_score: f32 = b.metrics.values().sum();
353-
a_score.partial_cmp(&b_score).unwrap()
354-
})
346+
tournament.into_iter().max_by(|a, b| {
347+
// Compare based on sum of metrics (higher is better)
348+
let a_score: f32 = a.metrics.values().sum();
349+
let b_score: f32 = b.metrics.values().sum();
350+
a_score.partial_cmp(&b_score).unwrap()
351+
})
355352
}
356353

357354
/// Add a validated modification to the archive
@@ -364,6 +361,8 @@ impl ExplorationStrategy {
364361

365362
// Generate feature descriptors for quality-diversity
366363
let features = self.extract_features(&modification, &metrics);
364+
// Clone features so we can use them after moving into the archive entry
365+
let features_clone = features.clone();
367366

368367
// Precompute fields used before moving values
369368
let mod_id = modification.id;
@@ -381,15 +380,15 @@ impl ExplorationStrategy {
381380
let key = entry.modification.id.to_string();
382381

383382
self.archive.insert(key, entry);
384-
383+
385384
// Add to novelty archive
386385
let novelty_point = NoveltyPoint {
387386
id: mod_id,
388-
features,
387+
features: features_clone,
389388
score,
390389
added_at: chrono::Utc::now(),
391390
};
392-
391+
393392
{
394393
let mut novelty_archive = self.novelty_archive.write().await;
395394
novelty_archive.push(novelty_point);
@@ -409,8 +408,10 @@ impl ExplorationStrategy {
409408
}
410409

411410
// Update metrics
412-
self.metrics.set_gauge("darwin.exploration.archive_size", self.archive.len() as u64).await;
413-
411+
self.metrics
412+
.set_gauge("darwin.exploration.archive_size", self.archive.len() as u64)
413+
.await;
414+
414415
Ok(())
415416
}
416417

@@ -494,12 +495,13 @@ impl ExplorationStrategy {
494495

495496
/// Trim the archive to maintain diversity
496497
async fn trim_archive(&self) -> Result<()> {
497-
498498
// In a real implementation, this would use quality-diversity
499499
// algorithms to maintain a diverse set of high-quality solutions
500500

501501
// For now, we'll just keep the newest entries
502-
let mut entries: Vec<(String, ArchiveEntry)> = self.archive.iter()
502+
let mut entries: Vec<(String, ArchiveEntry)> = self
503+
.archive
504+
.iter()
503505
.map(|e| (e.key().clone(), e.value().clone()))
504506
.collect();
505507
entries.sort_by(|a, b| b.1.added_at.cmp(&a.1.added_at));
@@ -635,4 +637,3 @@ pub struct ArchiveStats {
635637
pub feature_coverage: f32,
636638
pub top_scores: Vec<(Uuid, f32)>,
637639
}
638-

0 commit comments

Comments
 (0)