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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
use super::Error;
use super::events::{
GenotypeEvaluatedEvent, GenotypeGenerated, OptimizationRequestedEvent, RequestCompletedEvent,
RequestTerminatedEvent,
};
use crate::models::{
Breeder, Crossover, Distribution, Encodeable, Fitness, FitnessGoal, Genotype, Mutagen, Request,
RequestConclusion, Schedule, ScheduleDecision, Selector, Terminated,
};
use crate::repositories::chainable::{Chain, FromTx, ToTx};
use crate::repositories::{genotypes, morphologies, requests};
use crate::services::lock;
use crate::services::optimization::models::{Terminator, TypeErasedEvaluator};
use fx_event_bus::Publisher;
use serde::Serialize;
use std::collections::{HashMap, HashSet};
use tracing::instrument;
use uuid::Uuid;
/// Genetic algorithm optimization service that manages the entire optimization lifecycle.
/// Handles request creation, population generation, genotype evaluation, and breeding.
pub struct Service {
pub(super) locking: lock::Service,
pub(super) requests: requests::Repository,
pub(super) morphologies: morphologies::Repository,
pub(super) genotypes: genotypes::Repository,
pub(super) evaluators: HashMap<i32, Box<dyn TypeErasedEvaluator + 'static>>,
pub(super) max_deduplication_attempts: i32,
}
impl Service {
/// Creates a new service builder with the required dependencies.
#[instrument(level = "debug", skip_all)]
pub(crate) fn builder(
locking: lock::Service,
requests: requests::Repository,
morphologies: morphologies::Repository,
genotypes: genotypes::Repository,
) -> super::ServiceBuilder {
super::ServiceBuilder {
locking,
requests,
morphologies,
genotypes,
evaluators: HashMap::new(),
max_deduplication_attempts: 5, // Default value
}
}
/// Creates a new genetic algorithm optimization request.
///
/// This is the main entry point for starting genetic algorithm optimization.
/// It configures and initiates an evolutionary process that will attempt
/// to find optimal solutions for your problem type.
///
/// # Parameters
///
/// * `type_name` - Human-readable name of the type being optimized (from `Encodeable::NAME`)
/// * `type_hash` - Unique hash identifying the type structure (from `Encodeable::HASH`)
/// * `goal` - Optimization objective and stopping criteria
/// * `schedule` - Controls generation timing and population lifecycle
/// * `selector` - Parent selection strategy for breeding operations
/// * `mutagen` - Mutation behavior including temperature and rate schedules
/// * `crossover` - Genetic recombination method for combining parents
/// * `distribution` - Initial population generation strategy
///
/// # Optimization Goal
///
/// The `goal` parameter defines when optimization should stop:
/// - `FitnessGoal::maximize(0.95)` - Stop when fitness reaches 95% or higher
/// - `FitnessGoal::minimize(0.1)` - Stop when fitness drops to 10% or lower
///
/// # Population Management
///
/// The `schedule` controls generation timing and resource allocation:
/// - `Schedule::generational(100, 50)` - 100 generations, 50 individuals each
/// - `Schedule::rolling(5000, 100, 10)` - 5000 total evaluations, 100 max population, breed 10 at a time
///
/// # Parent Selection
///
/// The `selector` determines how parents are chosen for breeding:
/// - `Selector::tournament(3, 100)` - Tournament selection (size 3) from 100 candidates
/// - `Selector::roulette(100)` - Fitness-proportionate selection from 100 candidates
///
/// # Mutation Strategy
///
/// The `mutagen` combines temperature (step size) and mutation rate (frequency):
/// - `Mutagen::constant(0.5, 0.3)` - Fixed 50% temperature, 30% mutation rate
/// - Adaptive strategies use `Temperature::linear()` and `MutationRate::exponential()`
///
/// # Genetic Recombination
///
/// The `crossover` method combines genetic material from parents:
/// - `Crossover::uniform(0.5)` - Each gene has 50% chance from first parent
/// - `Crossover::single_point()` - Cut genome at random point, swap tails
///
/// # Initial Population
///
/// The `distribution` strategy affects initial diversity and convergence:
/// - `Distribution::latin_hypercube(50)` - Structured sampling for better coverage
/// - `Distribution::random(50)` - Pure random sampling
///
/// # Common Configurations
///
/// ## Quick Convergence (Time-Constrained)
/// ```rust,no_run
/// use fx_durable_ga::models::*;
/// # use fx_durable_ga::optimization::Service;
/// # #[derive(Debug)] struct MyType;
/// # impl Encodeable for MyType {
/// # const NAME: &'static str = "MyType";
/// # type Phenotype = MyType;
/// # fn morphology() -> Vec<GeneBounds> { vec![] }
/// # fn encode(&self) -> Vec<i64> { vec![] }
/// # fn decode(_: &[i64]) -> Self::Phenotype { MyType }
/// # }
/// # async fn example(service: &Service) -> Result<(), Box<dyn std::error::Error>> {
///
/// service.new_optimization_request(
/// MyType::NAME,
/// MyType::HASH,
/// FitnessGoal::maximize(0.90)?, // Modest target
/// Schedule::generational(20, 30), // Small, fast generations
/// Selector::tournament(5, 50)?, // Strong selection pressure
/// Mutagen::new(
/// Temperature::exponential(0.8, 0.1, 1.2, 3)?, // Rapid cooling
/// MutationRate::exponential(0.6, 0.05, 1.1, 2)?
/// ),
/// Crossover::uniform(0.6)?, // High recombination
/// Distribution::latin_hypercube(30),
/// None::<()>,
/// ).await?;
/// # Ok(())
/// # }
/// ```
///
/// ## Balanced Search (General Purpose)
/// ```rust,no_run
/// use fx_durable_ga::models::*;
/// # use fx_durable_ga::optimization::Service;
/// # #[derive(Debug)] struct MyType;
/// # impl Encodeable for MyType {
/// # const NAME: &'static str = "MyType";
/// # type Phenotype = MyType;
/// # fn morphology() -> Vec<GeneBounds> { vec![] }
/// # fn encode(&self) -> Vec<i64> { vec![] }
/// # fn decode(_: &[i64]) -> Self::Phenotype { MyType }
/// # }
/// # async fn example(service: &Service) -> Result<(), Box<dyn std::error::Error>> {
///
/// service.new_optimization_request(
/// MyType::NAME,
/// MyType::HASH,
/// FitnessGoal::maximize(0.95)?, // High-quality target
/// Schedule::generational(50, 50), // Moderate generations
/// Selector::tournament(3, 100)?, // Balanced selection
/// Mutagen::new(
/// Temperature::linear(0.7, 0.2, 1.0)?, // Gradual cooling
/// MutationRate::linear(0.4, 0.1, 1.0)?
/// ),
/// Crossover::uniform(0.5)?, // Balanced recombination
/// Distribution::latin_hypercube(50),
/// None::<()>,
/// ).await?;
/// # Ok(())
/// # }
/// ```
///
/// ## Thorough Exploration (Complex Problems)
/// ```rust,no_run
/// use fx_durable_ga::models::*;
/// # use fx_durable_ga::optimization::Service;
/// # #[derive(Debug)] struct MyType;
/// # impl Encodeable for MyType {
/// # const NAME: &'static str = "MyType";
/// # type Phenotype = MyType;
/// # fn morphology() -> Vec<GeneBounds> { vec![] }
/// # fn encode(&self) -> Vec<i64> { vec![] }
/// # fn decode(_: &[i64]) -> Self::Phenotype { MyType }
/// # }
/// # async fn example(service: &Service) -> Result<(), Box<dyn std::error::Error>> {
///
/// service.new_optimization_request(
/// MyType::NAME,
/// MyType::HASH,
/// FitnessGoal::maximize(0.99)?, // High precision target
/// Schedule::rolling(10000, 200, 20), // Large budget
/// Selector::tournament(2, 150)?, // Low selection pressure
/// Mutagen::constant(0.6, 0.4)?, // Sustained exploration
/// Crossover::single_point(), // Conservative recombination
/// Distribution::latin_hypercube(100), // High initial diversity
/// None::<()>,
/// ).await?;
/// # Ok(())
/// # }
/// ```
///
/// # Parameter Interactions
///
/// Key relationships to consider:
/// - **High selection pressure + Low mutation**: Fast convergence, may get stuck
/// - **Low selection pressure + High mutation**: Slow convergence, good exploration
/// - **Large populations + Tournament selection**: Better genetic diversity
/// - **Small populations + Roulette selection**: Risk of premature convergence
/// - **Exponential decay + Generational schedule**: Classic simulated annealing
/// - **Linear decay + Rolling schedule**: Steady refinement over time
///
/// # Returns
///
/// Returns `Ok(())` if the optimization request was successfully created and scheduled.
/// The actual optimization runs asynchronously via the event/job system.
///
/// # Errors
///
/// - Returns error if the specified type hasn't been registered with an evaluator
/// - Returns error if database operations fail
/// - Parameter validation errors are caught at construction time (e.g., `FitnessGoal::maximize(1.5)` fails)
///
/// # Example Usage
///
/// ```rust,no_run
/// use fx_durable_ga::models::*;
/// # use fx_durable_ga::optimization::Service;
///
/// // Define your problem type
/// #[derive(Debug)]
/// struct Point { x: f64, y: f64 }
///
/// impl Encodeable for Point {
/// const NAME: &'static str = "Point";
/// type Phenotype = Point;
/// fn morphology() -> Vec<GeneBounds> {
/// vec![
/// GeneBounds::decimal(0.0, 10.0, 1000, 3).unwrap(),
/// GeneBounds::decimal(0.0, 10.0, 1000, 3).unwrap(),
/// ]
/// }
/// fn encode(&self) -> Vec<i64> {
/// let bounds = Self::morphology();
/// vec![
/// bounds[0].from_sample(self.x / 10.0),
/// bounds[1].from_sample(self.y / 10.0),
/// ]
/// }
/// fn decode(genes: &[i64]) -> Self::Phenotype {
/// let bounds = Self::morphology();
/// Point {
/// x: bounds[0].decode_f64(genes[0]),
/// y: bounds[1].decode_f64(genes[1]),
/// }
/// }
/// }
///
/// # async fn example(service: &Service) -> Result<(), Box<dyn std::error::Error>> {
/// // Start optimization
/// service.new_optimization_request(
/// Point::NAME,
/// Point::HASH,
/// FitnessGoal::maximize(0.95)?,
/// Schedule::generational(50, 50),
/// Selector::tournament(3, 100)?,
/// Mutagen::constant(0.5, 0.3)?,
/// Crossover::uniform(0.5)?,
/// Distribution::latin_hypercube(50),
/// None::<()>,
/// ).await?;
/// # Ok(())
/// # }
/// ```
#[instrument(level = "debug", skip(self, data), fields(type_name = type_name, type_hash = type_hash, goal = ?goal, mutagen = ?mutagen, crossover = ?crossover))]
pub async fn new_optimization_request(
&self,
type_name: &str,
type_hash: i32,
goal: FitnessGoal,
schedule: Schedule,
selector: Selector,
mutagen: Mutagen,
crossover: Crossover,
distribution: Distribution,
data: Option<impl Serialize + Send + Sync>,
) -> Result<Uuid, Error> {
tracing::info!("Optimization request received");
let request = self
.requests
.chain(|mut tx_requests| {
Box::pin(async move {
// Create a new optimization request with the repository
let request = tx_requests
.new_request(Request::new(
type_name,
type_hash,
goal,
selector,
schedule,
mutagen,
crossover,
distribution,
data,
)?)
.await?;
// Instantiate a publisher
let mut publisher = fx_event_bus::Publisher::from_tx(tx_requests);
// Publish an event within the same transaction
publisher
.publish(OptimizationRequestedEvent::new(request.id))
.await?;
Ok((publisher, request))
})
})
.await?;
Ok(request.id)
}
/// Generates the initial population of genotypes for an optimization request.
/// Creates genotypes based on the request's distribution and publishes generation events.
#[instrument(level = "debug", skip(self), fields(request_id = %request_id))]
pub(crate) async fn generate_initial_population(&self, request_id: Uuid) -> Result<(), Error> {
tracing::info!("Generating initial population");
// Get the optimization request
let request = self.requests.get_request(request_id).await.map_err(|e| {
tracing::error!(
"Failed to fetch request in generate_initial_population with ID {}: {:?}",
request_id,
e
);
e
})?;
// Get the morphology of the type under optimization
let morphology = self.morphologies.get_morphology(request.type_hash).await?;
// Create an inital distribution of genomes
let genomes = request.distribution.distribute(&morphology);
let mut genotypes = Vec::with_capacity(genomes.len());
let mut events = Vec::with_capacity(genomes.len());
for genome in genomes {
let genotype =
Genotype::new(&request.type_name, request.type_hash, genome, request.id, 1);
let event = GenotypeGenerated::new(request.id, genotype.id());
genotypes.push(genotype);
events.push(event);
}
self.genotypes
.chain(|mut tx_genotypes| {
Box::pin(async move {
// Create the initial generation atomically (prevents race conditions)
let inserted_genotypes = tx_genotypes.new_genotypes(genotypes).await?;
// Only proceed if genotypes were actually inserted (no race condition)
if !inserted_genotypes.is_empty() {
// Instantiate a publisher
let mut publisher = fx_event_bus::Publisher::from_tx(tx_genotypes);
// Publish one event for each generated phenotype
publisher.publish_many(&events).await?;
Ok((publisher, request))
} else {
// Race condition occurred - another worker created the generation
// Create a dummy publisher to satisfy the return type
let publisher = fx_event_bus::Publisher::from_tx(tx_genotypes);
Ok((publisher, request))
}
})
})
.await?;
Ok(())
}
/// Evaluates a genotype's fitness using the appropriate evaluator.
/// Records the fitness result and publishes an evaluation event.
#[instrument(level = "debug", skip(self), fields(request_id = %request_id, genotype_id = %genotype_id))]
pub(crate) async fn evaluate_genotype(
&self,
request_id: Uuid,
genotype_id: Uuid,
) -> Result<(), Error> {
// Get the genotype from the database
let genotype = self
.genotypes
.get_genotype(&genotype_id)
.await
.map_err(|e| {
tracing::error!("Failed to fetch genotype with ID {}: {:?}", genotype_id, e);
e
})?;
// Get the request from the database
let request = self.requests.get_request(request_id).await?;
// Get the evaluator to use for this type
let evaluator =
self.evaluators
.get(&genotype.type_hash())
.ok_or(Error::UnknownTypeError {
type_hash: genotype.type_hash(),
type_name: genotype.type_name().to_string(),
})?;
// Call the evaluation function. This is a long running function!
let terminator: Box<dyn Terminated> =
Box::new(Terminator::new(self.requests.clone(), request_id));
let fitness = evaluator.fitness(&genotype, &request, &terminator).await?;
self.genotypes
.chain(|mut tx_genotypes| {
Box::pin(async move {
// Record the fitness of the genotype
tx_genotypes
.record_fitness(&Fitness::new(genotype_id, fitness))
.await?;
// Instantiate a publisher
let mut publisher = fx_event_bus::Publisher::from_tx(tx_genotypes);
// Publish an event
publisher
.publish(GenotypeEvaluatedEvent::new(request_id, genotype_id))
.await?;
Ok((publisher, ()))
})
})
.await?;
Ok(())
}
#[instrument(level = "debug", skip(self, request), fields(request_id = %request.id, num_offspring = num_offspring, next_generation_id = next_generation_id))]
async fn breed_genotypes(
&self,
request: &Request,
num_offspring: usize,
next_generation_id: i32,
) -> Result<(), Error> {
// Early return if generation already exists (prevents duplicate work)
let exists = self
.genotypes
.check_if_generation_exists(request.id, next_generation_id)
.await?;
if exists {
return Ok(());
}
tracing::info!("Breeding genotypes");
// Get candidates with fitness from populations repository
let candidates_with_fitness = self
.genotypes
.search_genotypes(
&genotypes::Filter::default()
.with_request_id(request.id)
.with_fitness(true)
.with_order_random(),
request.selector.sample_size(),
)
.await?;
// Get morphology for mutation bounds
let morphology = self.morphologies.get_morphology(request.type_hash).await?;
// Get current population to calculate optimization progress
let population = self.genotypes.get_population(&request.id).await?;
let best_fitness = *request
.goal
.best_fitness(&population.min_fitness, &population.max_fitness);
// Iterative breeding with deduplication to avoid creating duplicate genomes
let mut final_genotypes = Vec::with_capacity(num_offspring);
let mut generated_hashes = HashSet::new();
let mut deduplication_attempts = 0;
while final_genotypes.len() < num_offspring
&& deduplication_attempts < self.max_deduplication_attempts
{
let needed = num_offspring - final_genotypes.len();
// Re-select parents for this iteration (borrowed refs)
let parent_pairs = request
.selector
.select_parents(needed, &candidates_with_fitness, &request.goal)
.map_err(|e| Error::SelectionError(e))?;
// Breed using Breeder (before async operations)
let batch_genotypes = {
let mut rng = rand::rng();
Breeder::breed_batch(
&request,
&morphology,
&parent_pairs,
next_generation_id,
request.goal.calculate_progress(best_fitness),
&mut rng,
)
};
// Collect hashes from this batch
let batch_hashes: Vec<i64> = batch_genotypes.iter().map(|g| g.genome_hash()).collect();
// Check database for intersections
let intersecting_hashes = self
.genotypes
.get_intersection(request.id, &batch_hashes)
.await?
.into_iter()
.collect::<HashSet<i64>>();
// Filter out duplicates (database + already generated)
let mut unique_count = 0;
for genotype in batch_genotypes {
if !intersecting_hashes.contains(&genotype.genome_hash())
&& !generated_hashes.contains(&genotype.genome_hash())
{
generated_hashes.insert(genotype.genome_hash());
final_genotypes.push(genotype);
unique_count += 1;
}
}
if unique_count < num_offspring {
// Inform of duplicate generation. This is an indication that request params may require tuning.
tracing::info!(
"Breeding generated {} non-unique genomes during attempt {}",
num_offspring - unique_count,
deduplication_attempts
);
}
// Update deduplication_attempts counter
if unique_count == 0 {
deduplication_attempts += 1;
} else {
deduplication_attempts = 0;
}
}
// Check if we exhausted max_deduplication_attempts progress iterations
if deduplication_attempts >= self.max_deduplication_attempts {
tracing::warn!(
"Breeding exhausted max_deduplication_attempts ({}) for request {}. Consider tuning selection parameters or increasing diversity.",
self.max_deduplication_attempts,
request.id
);
}
// If we couldn't generate enough unique genotypes, log a warning
if final_genotypes.len() < num_offspring {
tracing::warn!(
"Could only generate {} unique genotypes out of {} requested for request {}",
final_genotypes.len(),
num_offspring,
request.id
);
}
// Update database (only if we have any genotypes)
if !final_genotypes.is_empty() {
// Create events for final genotypes
let events: Vec<GenotypeGenerated> = final_genotypes
.iter()
.map(|genotype| GenotypeGenerated::new(request.id, genotype.id()))
.collect();
self.genotypes
.chain(|mut tx_genotypes| {
Box::pin(async move {
let inserted_genotypes =
tx_genotypes.new_genotypes(final_genotypes).await?;
// Only proceed if genotypes were actually inserted (no race condition)
if !inserted_genotypes.is_empty() {
let mut publisher = fx_event_bus::Publisher::from_tx(tx_genotypes);
publisher.publish_many(&events).await?;
Ok((publisher, ()))
} else {
// Race condition occurred - another worker created the generation
let publisher = fx_event_bus::Publisher::from_tx(tx_genotypes);
Ok((publisher, ()))
}
})
})
.await?;
}
Ok(())
}
/// Maintains the population by checking completion status and scheduling breeding or termination.
/// Called after each genotype evaluation to determine the next optimization step.
#[instrument(level = "debug", skip(self), fields(request_id = %request_id))]
pub(crate) async fn maintain_population(&self, request_id: Uuid) -> Result<(), Error> {
let key = format!("maintain_population_{}", request_id);
self.locking
.lock_while(&key, || async {
// Check if request is already concluded - skip if so
if let Some(_) = self.requests.get_request_conclusion(&request_id).await? {
tracing::debug!("Request already concluded, skipping maintenance");
return Ok(());
}
// Get the request
let request = self.requests.get_request(request_id).await.map_err(|e| {
tracing::error!("Failed to fetch request with ID {}: {:?}", request_id, e);
e
})?;
// Get the population
let population = self.genotypes.get_population(&request.id).await?;
let Some(best_fitness) = *request
.goal
.best_fitness(&population.min_fitness, &population.max_fitness)
else {
return Ok(());
};
if request.is_completed(best_fitness) {
// Publish RequestCompleted::new(request_id)
self.genotypes
.chain(|tx_genotypes| {
Box::pin(async move {
// Create publisher
let mut publisher = fx_event_bus::Publisher::from_tx(tx_genotypes);
// Publish completion event
publisher
.publish(RequestCompletedEvent::new(request.id))
.await?;
Ok((publisher, ()))
})
})
.await?;
return Ok(());
}
match request.schedule.should_breed(&population) {
ScheduleDecision::Wait => Ok(()), // Do nothing
ScheduleDecision::Terminate => self.publish_terminated(request.id).await,
ScheduleDecision::Breed {
num_offspring,
next_generation_id,
} => {
self.breed_genotypes(&request, num_offspring, next_generation_id)
.await
}
}
})
.await?
}
/// Records the conclusion of an optimization request to prevent duplicate processing.
/// Uses locking to ensure atomicity when multiple workers might conclude the same request.
#[instrument(level = "debug", skip(self), fields(request_id = %request_conclusion.request_id, concluded_at = %request_conclusion.concluded_at, concluded_with = ?request_conclusion.concluded_with))]
pub(crate) async fn conclude_request(
&self,
request_conclusion: RequestConclusion,
) -> Result<(), Error> {
let key = format!("conclude_request_{}", request_conclusion.request_id);
self.locking
.lock_while(&key, || async {
if let Some(_) = self
.requests
.get_request_conclusion(&request_conclusion.request_id)
.await?
{
return Ok::<(), super::Error>(());
}
tracing::info!("Concluding request");
self.requests
.new_request_conclusion(&request_conclusion)
.await?;
Ok(())
})
.await?
}
/// Publishes a termination event for an optimization request.
#[instrument(level = "debug", skip(self), fields(request_id = %request_id))]
pub(crate) async fn publish_terminated(&self, request_id: Uuid) -> Result<(), Error> {
self.genotypes
.chain(|tx_genotypes| {
Box::pin(async move {
let mut publisher = Publisher::new(tx_genotypes.tx());
let ret = publisher
.publish(RequestTerminatedEvent::new(request_id))
.await?;
Ok((publisher, ret))
})
})
.await?;
Ok(())
}
/// Gets the best genotype for a request based on fitness.
/// Returns the genotype with the best (min or max) fitness depending on the goal.
#[instrument(level = "debug", skip(self), fields(request_id = %request_id))]
pub async fn get_best_genotype(
&self,
request_id: Uuid,
) -> Result<Option<(Genotype, f64)>, Error> {
let request = self.requests.get_request(request_id).await?;
// Determine sort order based on goal
let filter = match request.goal {
FitnessGoal::Minimize { .. } => genotypes::Filter::default()
.with_request_id(request_id)
.with_fitness(true)
.with_order_fitness_asc(),
FitnessGoal::Maximize { .. } => genotypes::Filter::default()
.with_request_id(request_id)
.with_fitness(true)
.with_order_fitness_desc(),
};
let results = self.genotypes.search_genotypes(&filter, 1).await?;
// Extract the first result if it exists and has fitness
let best = results
.into_iter()
.next()
.and_then(|(genotype, fitness_opt)| fitness_opt.map(|fitness| (genotype, fitness)));
Ok(best)
}
/// Checks if an optimization request has concluded.
/// Returns true if the request has been completed or terminated.
#[instrument(level = "debug", skip(self), fields(request_id = %request_id))]
pub async fn is_request_concluded(&self, request_id: Uuid) -> Result<bool, Error> {
Ok(self
.requests
.get_request_conclusion(&request_id)
.await?
.is_some())
}
/// Get a morphology by its type name
#[instrument(level = "debug", skip(self), fields(type_name))]
pub async fn search_genotypes(
&self,
filter: &genotypes::Filter,
limit: i64,
) -> Result<Vec<(Genotype, Option<f64>)>, Error> {
let genotypes = self.genotypes.search_genotypes(filter, limit).await?;
Ok(genotypes)
}
/// Get a phenotype by its type name
#[instrument(level = "debug", skip(self), fields(type_name))]
pub async fn get_phenotype<T: Encodeable>(
&self,
genotype_id: &Uuid,
) -> Result<T::Phenotype, Error> {
let genotype = self.genotypes.get_genotype(genotype_id).await?;
if genotype.type_hash() != T::HASH {
return Err(Error::UnknownPhenotype {
type_name: genotype.type_name().to_string(),
type_hash: genotype.type_hash(),
});
}
Ok(T::decode(&genotype.genome()))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::bootstrap;
use crate::models::{
Crossover, Distribution, Encodeable, Evaluator, FitnessGoal, GeneBounds, Mutagen,
MutationRate, Request, Schedule, Selector, Temperature, Terminated,
};
use futures::future::BoxFuture;
async fn create_test_service(pool: sqlx::PgPool) -> Service {
let builder = bootstrap::bootstrap(pool).await.unwrap();
builder.build()
}
#[sqlx::test(migrations = false)]
async fn test_new_optimization_request_happy_path(pool: sqlx::PgPool) -> anyhow::Result<()> {
crate::migrations::run_default_migrations(&pool).await?;
// Create service
let service = create_test_service(pool.clone()).await;
// Create valid request parameters
let type_name = "TestType";
let type_hash = 123;
let goal = FitnessGoal::maximize(0.95)?;
let schedule = Schedule::generational(100, 10);
let selector = Selector::tournament(3, 20).expect("is valid");
let mutagen = Mutagen::new(Temperature::constant(0.5)?, MutationRate::constant(0.3)?);
let crossover = Crossover::uniform(0.5)?;
let distribution = Distribution::random(50);
// Call the method
let result = service
.new_optimization_request(
type_name,
type_hash,
goal.clone(),
schedule.clone(),
selector.clone(),
mutagen.clone(),
crossover.clone(),
distribution.clone(),
None::<()>,
)
.await;
// Verify success
assert!(result.is_ok(), "new_optimization_request should succeed");
// Verify request was stored in database
let request_count = sqlx::query_scalar!(
"SELECT COUNT(*) FROM fx_durable_ga.requests WHERE type_name = $1 AND type_hash = $2",
type_name,
type_hash
)
.fetch_one(&pool)
.await?
.unwrap_or(0);
assert_eq!(
request_count, 1,
"Exactly one request should be stored in database"
);
// Verify an OptimizationRequestedEvent was published
let event_count: i64 = sqlx::query_scalar::<_, i64>(
"SELECT COUNT(*) FROM fx_event_bus.events_unacknowledged WHERE name = $1",
)
.bind("OptimizationRequested")
.fetch_one(&pool)
.await?;
assert_eq!(
event_count, 1,
"Exactly one OptimizationRequestedEvent should be published"
);
Ok(())
}
#[sqlx::test(migrations = false)]
async fn test_generate_initial_population_happy_path(pool: sqlx::PgPool) -> anyhow::Result<()> {
crate::migrations::run_default_migrations(&pool).await?;
// Create service
let service = create_test_service(pool.clone()).await;
// Create a test morphology first
let type_name = "TestType";
let type_hash = 456;
let morphology = crate::models::Morphology::new(
type_name,
type_hash,
vec![
crate::models::GeneBounds::integer(0, 9, 10).unwrap(), // 10 steps
crate::models::GeneBounds::integer(0, 4, 5).unwrap(), // 5 steps
],
);
// Insert morphology into database
service.morphologies.new_morphology(morphology).await?;
// Create a request
let goal = FitnessGoal::maximize(0.95)?;
let schedule = Schedule::generational(100, 10);
let selector = Selector::tournament(3, 20).expect("is valid");
let mutagen = Mutagen::new(Temperature::constant(0.5)?, MutationRate::constant(0.3)?);
let crossover = Crossover::uniform(0.5)?;
let distribution = Distribution::random(10); // Small population for testing
let request = crate::models::Request::new(
type_name,
type_hash,
goal,
selector,
schedule,
mutagen,
crossover,
distribution,
None::<()>,
)?;
let request_id = request.id;
// Insert request into database using chainable pattern
service
.requests
.chain(|mut tx_requests| {
Box::pin(async move {
let request = tx_requests.new_request(request).await?;
Ok((tx_requests, request))
})
})
.await?;
// Call the method under test
let result = service.generate_initial_population(request_id).await;
// Verify success
assert!(result.is_ok(), "generate_initial_population should succeed");
// Verify genotypes were created in database
let genotype_count = sqlx::query_scalar!(
"SELECT COUNT(*) FROM fx_durable_ga.genotypes WHERE request_id = $1 AND generation_id = 1",
request_id
)
.fetch_one(&pool)
.await?
.unwrap_or(0);
assert_eq!(
genotype_count, 10,
"Exactly 10 genotypes should be created for generation 1"
);
// Verify GenotypeGenerated events were published
let event_count: i64 = sqlx::query_scalar::<_, i64>(
"SELECT COUNT(*) FROM fx_event_bus.events_unacknowledged WHERE name = $1",
)
.bind("GenotypeGenerated")
.fetch_one(&pool)
.await?;
assert_eq!(
event_count, 10,
"Exactly 10 GenotypeGenerated events should be published"
);
Ok(())
}
#[sqlx::test(migrations = false)]
async fn test_evaluate_genotype_happy_path(pool: sqlx::PgPool) -> anyhow::Result<()> {
crate::migrations::run_default_migrations(&pool).await?;
// Define a simple test type
#[derive(Debug)]
struct TestType;
impl Encodeable for TestType {
const NAME: &'static str = "TestType";
type Phenotype = (i64, i64);
fn morphology() -> Vec<GeneBounds> {
vec![
GeneBounds::integer(0, 9, 10).unwrap(),
GeneBounds::integer(0, 4, 5).unwrap(),
]
}
fn encode(&self) -> Vec<i64> {
vec![5, 2] // Not used in test
}
fn decode(genes: &[i64]) -> Self::Phenotype {
(genes[0], genes[1])
}
}
// Simple evaluator that returns constant fitness
struct TestEvaluator;
impl Evaluator<(i64, i64)> for TestEvaluator {
fn fitness<'a>(
&self,
_genotype_id: uuid::Uuid,
_phenotype: (i64, i64),
_request: &'a Request,
_terminated: &'a Box<dyn Terminated>,
) -> BoxFuture<'a, Result<f64, anyhow::Error>> {
Box::pin(async move { Ok(0.75) })
}
}
// Create service and register evaluator
let service = bootstrap::bootstrap(pool.clone())
.await?
.register::<TestType, _>(TestEvaluator)
.await?
.build();
// Create a test request
let goal = FitnessGoal::maximize(0.95)?;
let schedule = Schedule::generational(100, 10);
let selector = Selector::tournament(3, 20).expect("is valid");
let mutagen = Mutagen::new(Temperature::constant(0.5)?, MutationRate::constant(0.3)?);
let crossover = Crossover::uniform(0.5)?;
let distribution = Distribution::random(1);
let request = crate::models::Request::new(
TestType::NAME,
TestType::HASH,
goal,
selector,
schedule,
mutagen,
crossover,
distribution,
None::<()>,
)?;
let request_id = request.id;
// Insert request into database
service
.requests
.chain(|mut tx_requests| {
Box::pin(async move {
let request = tx_requests.new_request(request).await?;
Ok((tx_requests, request))
})
})
.await?;
// Create a genotype manually
let genotype = crate::models::Genotype::new(
TestType::NAME,
TestType::HASH,
vec![7, 3], // Test genome
request_id,
1, // generation_id
);
let genotype_id = genotype.id();
// Insert genotype into database
service
.genotypes
.chain(|mut tx_genotypes| {
Box::pin(async move {
let genotypes = tx_genotypes.new_genotypes(vec![genotype]).await?;
Ok((tx_genotypes, genotypes))
})
})
.await?;
// Call the method under test
let result = service.evaluate_genotype(request_id, genotype_id).await;
// Verify success
assert!(result.is_ok(), "evaluate_genotype should succeed");
// Verify fitness was recorded in database
let fitness = sqlx::query_scalar!(
"SELECT fitness FROM fx_durable_ga.fitness WHERE genotype_id = $1",
genotype_id
)
.fetch_one(&pool)
.await?;
assert_eq!(fitness, 0.75, "Fitness should be recorded as 0.75");
// Verify GenotypeEvaluatedEvent was published
let event_count: i64 = sqlx::query_scalar::<_, i64>(
"SELECT COUNT(*) FROM fx_event_bus.events_unacknowledged WHERE name = $1",
)
.bind("GenotypeEvaluated")
.fetch_one(&pool)
.await?;
assert_eq!(
event_count, 1,
"Exactly one GenotypeEvaluatedEvent should be published"
);
Ok(())
}
#[sqlx::test(migrations = false)]
async fn test_maintain_population_request_completion(pool: sqlx::PgPool) -> anyhow::Result<()> {
crate::migrations::run_default_migrations(&pool).await?;
// Create service
let service = create_test_service(pool.clone()).await;
// Create morphology
let type_name = "TestType";
let type_hash = 789;
let morphology = crate::models::Morphology::new(
type_name,
type_hash,
vec![
crate::models::GeneBounds::integer(0, 9, 10).unwrap(),
crate::models::GeneBounds::integer(0, 4, 5).unwrap(),
],
);
service.morphologies.new_morphology(morphology).await?;
// Create request with LOW fitness goal (0.5) so it's easy to exceed
let goal = FitnessGoal::maximize(0.5)?;
let schedule = Schedule::generational(100, 10);
let selector = Selector::tournament(3, 20).expect("is valid");
let mutagen = Mutagen::new(Temperature::constant(0.5)?, MutationRate::constant(0.3)?);
let crossover = Crossover::uniform(0.5)?;
let distribution = Distribution::random(5);
let request = crate::models::Request::new(
type_name,
type_hash,
goal,
selector,
schedule,
mutagen,
crossover,
distribution,
None::<()>,
)?;
let request_id = request.id;
// Insert request
service
.requests
.chain(|mut tx_requests| {
Box::pin(async move {
let request = tx_requests.new_request(request).await?;
Ok((tx_requests, request))
})
})
.await?;
// Create genotype with fitness that EXCEEDS the goal (0.8 > 0.5)
let genotype =
crate::models::Genotype::new(type_name, type_hash, vec![5, 2], request_id, 1);
let genotype_id = genotype.id();
// Insert genotype and fitness
service
.genotypes
.chain(|mut tx_genotypes| {
Box::pin(async move {
// Insert genotype
tx_genotypes.new_genotypes(vec![genotype]).await?;
// Insert fitness that exceeds goal
let fitness = crate::models::Fitness::new(genotype_id, 0.8);
tx_genotypes.record_fitness(&fitness).await?;
Ok((tx_genotypes, ()))
})
})
.await?;
// Call the method under test
let result = service.maintain_population(request_id).await;
// Verify success
assert!(result.is_ok(), "maintain_population should succeed");
// Verify RequestCompletedEvent was published
let event_count: i64 = sqlx::query_scalar::<_, i64>(
"SELECT COUNT(*) FROM fx_event_bus.events_unacknowledged WHERE name = $1",
)
.bind("RequestCompleted")
.fetch_one(&pool)
.await?;
assert_eq!(
event_count, 1,
"Exactly one RequestCompletedEvent should be published"
);
Ok(())
}
#[sqlx::test(migrations = false)]
async fn test_get_phenotype(pool: sqlx::PgPool) -> anyhow::Result<()> {
crate::migrations::run_default_migrations(&pool).await?;
#[derive(Debug, PartialEq)]
struct TestPoint {
x: i64,
y: i64,
}
impl Encodeable for TestPoint {
const NAME: &'static str = "TestPoint";
type Phenotype = TestPoint;
fn morphology() -> Vec<GeneBounds> {
vec![
GeneBounds::integer(0, 9, 10).unwrap(),
GeneBounds::integer(0, 4, 5).unwrap(),
]
}
fn encode(&self) -> Vec<i64> {
vec![self.x, self.y]
}
fn decode(genes: &[i64]) -> Self::Phenotype {
TestPoint {
x: genes[0],
y: genes[1],
}
}
}
let service = create_test_service(pool.clone()).await;
let goal = FitnessGoal::maximize(0.95)?;
let schedule = Schedule::generational(100, 10);
let selector = Selector::tournament(3, 20).expect("is valid");
let mutagen = Mutagen::new(Temperature::constant(0.5)?, MutationRate::constant(0.3)?);
let crossover = Crossover::uniform(0.5)?;
let distribution = Distribution::random(10);
let request = crate::models::Request::new(
TestPoint::NAME,
TestPoint::HASH,
goal,
selector,
schedule,
mutagen,
crossover,
distribution,
None::<()>,
)?;
let request_id = request.id;
service
.requests
.chain(|mut tx_requests| {
Box::pin(async move {
let request = tx_requests.new_request(request).await?;
Ok((tx_requests, request))
})
})
.await?;
let genotype = crate::models::Genotype::new(
TestPoint::NAME,
TestPoint::HASH,
vec![7, 3],
request_id,
1,
);
let genotype_id = genotype.id();
service
.genotypes
.chain(|mut tx_genotypes| {
Box::pin(async move {
tx_genotypes.new_genotypes(vec![genotype]).await?;
Ok((tx_genotypes, ()))
})
})
.await?;
let result: TestPoint = service.get_phenotype::<TestPoint>(&genotype_id).await?;
assert_eq!(result.x, 7);
assert_eq!(result.y, 3);
Ok(())
}
}