fcmaes-rust 0.2.0__tar.gz → 0.3.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. {fcmaes_rust-0.2.0 → fcmaes_rust-0.3.0}/Cargo.lock +10 -9
  2. {fcmaes_rust-0.2.0 → fcmaes_rust-0.3.0}/Cargo.toml +3 -3
  3. {fcmaes_rust-0.2.0 → fcmaes_rust-0.3.0}/PKG-INFO +1 -1
  4. fcmaes_rust-0.3.0/crates/fcmaes-core/examples/stop_compatibility.rs +94 -0
  5. {fcmaes_rust-0.2.0 → fcmaes_rust-0.3.0}/crates/fcmaes-core/src/biteopt.rs +48 -2
  6. {fcmaes_rust-0.2.0 → fcmaes_rust-0.3.0}/crates/fcmaes-core/src/cmaes.rs +18 -3
  7. {fcmaes_rust-0.2.0 → fcmaes_rust-0.3.0}/crates/fcmaes-core/src/crfmnes.rs +20 -3
  8. {fcmaes_rust-0.2.0 → fcmaes_rust-0.3.0}/crates/fcmaes-core/src/da.rs +102 -14
  9. {fcmaes_rust-0.2.0 → fcmaes_rust-0.3.0}/crates/fcmaes-core/src/de.rs +61 -6
  10. {fcmaes_rust-0.2.0 → fcmaes_rust-0.3.0}/crates/fcmaes-core/src/lib.rs +1 -1
  11. {fcmaes_rust-0.2.0 → fcmaes_rust-0.3.0}/crates/fcmaes-core/src/pgpe.rs +20 -3
  12. {fcmaes_rust-0.2.0 → fcmaes_rust-0.3.0}/crates/fcmaes-core/src/retry.rs +17 -1
  13. fcmaes_rust-0.3.0/crates/fcmaes-core/tests/cooperative_stop.rs +203 -0
  14. fcmaes_rust-0.3.0/crates/fcmaes-py/python_tests/test_binding_contract.py +427 -0
  15. {fcmaes_rust-0.2.0 → fcmaes_rust-0.3.0}/crates/fcmaes-py/src/acma.rs +57 -24
  16. {fcmaes_rust-0.2.0 → fcmaes_rust-0.3.0}/crates/fcmaes-py/src/biteopt.rs +24 -16
  17. fcmaes_rust-0.3.0/crates/fcmaes-py/src/common.rs +285 -0
  18. {fcmaes_rust-0.2.0 → fcmaes_rust-0.3.0}/crates/fcmaes-py/src/crfmnes.rs +66 -27
  19. fcmaes_rust-0.3.0/crates/fcmaes-py/src/da.rs +78 -0
  20. {fcmaes_rust-0.2.0 → fcmaes_rust-0.3.0}/crates/fcmaes-py/src/de.rs +61 -25
  21. {fcmaes_rust-0.2.0 → fcmaes_rust-0.3.0}/crates/fcmaes-py/src/gtop.rs +5 -6
  22. {fcmaes_rust-0.2.0 → fcmaes_rust-0.3.0}/crates/fcmaes-py/src/mapelites.rs +38 -15
  23. {fcmaes_rust-0.2.0 → fcmaes_rust-0.3.0}/crates/fcmaes-py/src/mode.rs +8 -12
  24. {fcmaes_rust-0.2.0 → fcmaes_rust-0.3.0}/crates/fcmaes-py/src/moretry.rs +50 -25
  25. {fcmaes_rust-0.2.0 → fcmaes_rust-0.3.0}/crates/fcmaes-py/src/pgpe.rs +70 -29
  26. {fcmaes_rust-0.2.0 → fcmaes_rust-0.3.0}/crates/fcmaes-py/src/retry.rs +5 -5
  27. {fcmaes_rust-0.2.0 → fcmaes_rust-0.3.0}/python/fcmaes_rust/_fcmaes_ext.pyi +32 -9
  28. fcmaes_rust-0.2.0/crates/fcmaes-py/src/common.rs +0 -91
  29. fcmaes_rust-0.2.0/crates/fcmaes-py/src/da.rs +0 -64
  30. {fcmaes_rust-0.2.0 → fcmaes_rust-0.3.0}/LICENSE +0 -0
  31. {fcmaes_rust-0.2.0 → fcmaes_rust-0.3.0}/crates/fcmaes-core/Cargo.toml +0 -0
  32. {fcmaes_rust-0.2.0 → fcmaes_rust-0.3.0}/crates/fcmaes-core/LICENSE +0 -0
  33. {fcmaes_rust-0.2.0 → fcmaes_rust-0.3.0}/crates/fcmaes-core/NOTICE.pareto +0 -0
  34. {fcmaes_rust-0.2.0 → fcmaes_rust-0.3.0}/crates/fcmaes-core/PARETO_ARCHIVE.md +0 -0
  35. {fcmaes_rust-0.2.0 → fcmaes_rust-0.3.0}/crates/fcmaes-core/README.md +0 -0
  36. {fcmaes_rust-0.2.0 → fcmaes_rust-0.3.0}/crates/fcmaes-core/examples/pareto_bench.rs +0 -0
  37. {fcmaes_rust-0.2.0 → fcmaes_rust-0.3.0}/crates/fcmaes-core/examples/pareto_mode.rs +0 -0
  38. {fcmaes_rust-0.2.0 → fcmaes_rust-0.3.0}/crates/fcmaes-core/src/fitness.rs +0 -0
  39. {fcmaes_rust-0.2.0 → fcmaes_rust-0.3.0}/crates/fcmaes-core/src/indicators.rs +0 -0
  40. {fcmaes_rust-0.2.0 → fcmaes_rust-0.3.0}/crates/fcmaes-core/src/mapelites.rs +0 -0
  41. {fcmaes_rust-0.2.0 → fcmaes_rust-0.3.0}/crates/fcmaes-core/src/mode.rs +0 -0
  42. {fcmaes_rust-0.2.0 → fcmaes_rust-0.3.0}/crates/fcmaes-core/src/moretry.rs +0 -0
  43. {fcmaes_rust-0.2.0 → fcmaes_rust-0.3.0}/crates/fcmaes-core/src/pareto.rs +0 -0
  44. {fcmaes_rust-0.2.0 → fcmaes_rust-0.3.0}/crates/fcmaes-core/src/pareto_tests.rs +0 -0
  45. {fcmaes_rust-0.2.0 → fcmaes_rust-0.3.0}/crates/fcmaes-core/src/rng.rs +0 -0
  46. {fcmaes_rust-0.2.0 → fcmaes_rust-0.3.0}/crates/fcmaes-core/tests/data/pareto_archive_2d.csv +0 -0
  47. {fcmaes_rust-0.2.0 → fcmaes_rust-0.3.0}/crates/fcmaes-core/tests/pareto_fixture.rs +0 -0
  48. {fcmaes_rust-0.2.0 → fcmaes_rust-0.3.0}/crates/fcmaes-gtop/Cargo.toml +0 -0
  49. {fcmaes_rust-0.2.0 → fcmaes_rust-0.3.0}/crates/fcmaes-gtop/src/lib.rs +0 -0
  50. {fcmaes_rust-0.2.0 → fcmaes_rust-0.3.0}/crates/fcmaes-py/Cargo.toml +0 -0
  51. {fcmaes_rust-0.2.0 → fcmaes_rust-0.3.0}/crates/fcmaes-py/python_tests/test_bindings.py +0 -0
  52. {fcmaes_rust-0.2.0 → fcmaes_rust-0.3.0}/crates/fcmaes-py/python_tests/test_type_stubs.py +0 -0
  53. {fcmaes_rust-0.2.0 → fcmaes_rust-0.3.0}/crates/fcmaes-py/src/lib.rs +0 -0
  54. {fcmaes_rust-0.2.0 → fcmaes_rust-0.3.0}/pyproject.toml +0 -0
  55. {fcmaes_rust-0.2.0 → fcmaes_rust-0.3.0}/python/README.md +0 -0
  56. {fcmaes_rust-0.2.0 → fcmaes_rust-0.3.0}/python/fcmaes_rust/__init__.py +0 -0
  57. {fcmaes_rust-0.2.0 → fcmaes_rust-0.3.0}/python/fcmaes_rust/py.typed +0 -0
@@ -433,7 +433,7 @@ checksum = "da7c62ceae207dd37ea5b845da6a0696c799f85e97da1ab5b7910be3c1c80223"
433
433
 
434
434
  [[package]]
435
435
  name = "fcmaes-core"
436
- version = "0.2.0"
436
+ version = "0.3.0"
437
437
  dependencies = [
438
438
  "libm",
439
439
  "nalgebra",
@@ -445,7 +445,7 @@ dependencies = [
445
445
 
446
446
  [[package]]
447
447
  name = "fcmaes-examples"
448
- version = "0.2.0"
448
+ version = "0.3.0"
449
449
  dependencies = [
450
450
  "fcmaes-core",
451
451
  "fcmaes-gtop",
@@ -457,11 +457,11 @@ dependencies = [
457
457
 
458
458
  [[package]]
459
459
  name = "fcmaes-gtop"
460
- version = "0.2.0"
460
+ version = "0.3.0"
461
461
 
462
462
  [[package]]
463
463
  name = "fcmaes-job-worker"
464
- version = "0.2.0"
464
+ version = "0.3.0"
465
465
  dependencies = [
466
466
  "fcmaes-core",
467
467
  "fcmaes-native-worker",
@@ -487,7 +487,7 @@ dependencies = [
487
487
 
488
488
  [[package]]
489
489
  name = "fcmaes-py"
490
- version = "0.2.0"
490
+ version = "0.3.0"
491
491
  dependencies = [
492
492
  "fcmaes-core",
493
493
  "fcmaes-gtop",
@@ -1148,8 +1148,8 @@ checksum = "7c87def4c32ab89d880effc9e097653c8da5d6ef28e6b539d313baaacfbafcbe"
1148
1148
 
1149
1149
  [[package]]
1150
1150
  name = "optimization-job-api"
1151
- version = "0.2.0"
1152
- source = "git+https://github.com/dietmarwo/optimization-jobs?rev=43ef3aee7be78274da529f10af5839a16bbf047e#43ef3aee7be78274da529f10af5839a16bbf047e"
1151
+ version = "0.4.1"
1152
+ source = "git+https://github.com/dietmarwo/optimization-jobs?rev=78970a290f63f90a3f3d6ecbfe23f5e1ef24a235#78970a290f63f90a3f3d6ecbfe23f5e1ef24a235"
1153
1153
  dependencies = [
1154
1154
  "fs2",
1155
1155
  "libc",
@@ -1160,8 +1160,8 @@ dependencies = [
1160
1160
 
1161
1161
  [[package]]
1162
1162
  name = "optimization-runner"
1163
- version = "0.2.0"
1164
- source = "git+https://github.com/dietmarwo/optimization-jobs?rev=43ef3aee7be78274da529f10af5839a16bbf047e#43ef3aee7be78274da529f10af5839a16bbf047e"
1163
+ version = "0.4.1"
1164
+ source = "git+https://github.com/dietmarwo/optimization-jobs?rev=78970a290f63f90a3f3d6ecbfe23f5e1ef24a235#78970a290f63f90a3f3d6ecbfe23f5e1ef24a235"
1165
1165
  dependencies = [
1166
1166
  "clap",
1167
1167
  "fs2",
@@ -1169,6 +1169,7 @@ dependencies = [
1169
1169
  "optimization-job-api",
1170
1170
  "serde",
1171
1171
  "serde_json",
1172
+ "sha2",
1172
1173
  "tempfile",
1173
1174
  ]
1174
1175
 
@@ -7,7 +7,7 @@ codegen-units = 1
7
7
  lto = "thin"
8
8
 
9
9
  [workspace.package]
10
- version = "0.2.0"
10
+ version = "0.3.0"
11
11
  edition = "2024"
12
12
  rust-version = "1.88"
13
13
  license = "MIT"
@@ -20,8 +20,8 @@ fcmaes-core = { path = "crates/fcmaes-core" }
20
20
  fcmaes-gtop = { path = "crates/fcmaes-gtop" }
21
21
  # The shared job API (envelope, result contract, worker SDK, runner) lives in
22
22
  # github.com/dietmarwo/optimization-jobs; change the revision there first.
23
- optimization-job-api = { git = "https://github.com/dietmarwo/optimization-jobs", rev = "43ef3aee7be78274da529f10af5839a16bbf047e" }
24
- optimization-runner = { git = "https://github.com/dietmarwo/optimization-jobs", rev = "43ef3aee7be78274da529f10af5839a16bbf047e" }
23
+ optimization-job-api = { git = "https://github.com/dietmarwo/optimization-jobs", rev = "78970a290f63f90a3f3d6ecbfe23f5e1ef24a235" }
24
+ optimization-runner = { git = "https://github.com/dietmarwo/optimization-jobs", rev = "78970a290f63f90a3f3d6ecbfe23f5e1ef24a235" }
25
25
 
26
26
  # The runner and its workers hash their staged executables (tens of MB in
27
27
  # debug builds) on every job start; unoptimized SHA-256 made debug start-up
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: fcmaes-rust
3
- Version: 0.2.0
3
+ Version: 0.3.0
4
4
  Classifier: Development Status :: 3 - Alpha
5
5
  Classifier: Programming Language :: Python :: 3
6
6
  Classifier: Programming Language :: Python :: 3.11
@@ -0,0 +1,94 @@
1
+ //! Fixed-seed compatibility records for the cooperative-stop release.
2
+ use fcmaes_core::*;
3
+ fn main() {
4
+ let objective = |x: &[f64]| x.iter().map(|v| v * v).sum::<f64>();
5
+ for seed in 1..=20 {
6
+ let fit = || Fitness::bounded(4, 1, &[-5.; 4], &[5.; 4]);
7
+ macro_rules! record {
8
+ ($name:expr,$r:expr) => {{
9
+ let r = $r;
10
+ println!(
11
+ "{} {} {:?} {:016x} {} {} {}",
12
+ $name,
13
+ seed,
14
+ r.x,
15
+ r.y.to_bits(),
16
+ r.evaluations,
17
+ r.iterations,
18
+ r.stop
19
+ );
20
+ }};
21
+ }
22
+ let p = BiteParams {
23
+ seed,
24
+ max_evaluations: 1000,
25
+ ..Default::default()
26
+ };
27
+ record!(
28
+ "bite",
29
+ BiteOpt::new(&[-5.; 4], &[5.; 4], None, &p).optimize(&objective)
30
+ );
31
+ record!(
32
+ "deep",
33
+ DeepBiteOpt::new(&[-5.; 4], &[5.; 4], None, &p, 3).optimize(&objective)
34
+ );
35
+ let p = DeParams {
36
+ seed,
37
+ max_evaluations: 1000,
38
+ ..Default::default()
39
+ };
40
+ record!(
41
+ "de",
42
+ De::new(fit(), &[], &[], None, &p).optimize(&objective)
43
+ );
44
+ record!(
45
+ "delayed",
46
+ De::new(fit(), &[], &[], None, &p).optimize_delayed(&objective, 4)
47
+ );
48
+ record!(
49
+ "cmaes",
50
+ Cmaes::new(
51
+ fit(),
52
+ &[0.; 4],
53
+ &[0.3],
54
+ &CmaesParams {
55
+ seed,
56
+ popsize: 8,
57
+ max_evaluations: 1000,
58
+ ..Default::default()
59
+ }
60
+ )
61
+ .optimize(&objective, 1)
62
+ );
63
+ record!(
64
+ "crfmnes",
65
+ Crfmnes::new(
66
+ fit(),
67
+ &[0.; 4],
68
+ 0.3,
69
+ &CrfmnesParams {
70
+ seed,
71
+ popsize: 8,
72
+ max_evaluations: 1000,
73
+ ..Default::default()
74
+ }
75
+ )
76
+ .optimize_batch(|xs| xs.iter().map(|x| objective(x)).collect())
77
+ );
78
+ record!(
79
+ "pgpe",
80
+ Pgpe::new(
81
+ fit(),
82
+ &[0.; 4],
83
+ &[0.3],
84
+ &PgpeParams {
85
+ seed,
86
+ popsize: 8,
87
+ max_evaluations: 1000,
88
+ ..Default::default()
89
+ }
90
+ )
91
+ .optimize_batch(|xs| xs.iter().map(|x| objective(x)).collect())
92
+ );
93
+ }
94
+ }
@@ -116,11 +116,13 @@ enum PopSel {
116
116
  /// Outcome of a BiteOpt run.
117
117
  #[derive(Clone, Debug)]
118
118
  pub struct BiteResult {
119
+ /// The cooperative stop predicate fired; evaluated points are retained.
120
+ pub stopped: bool,
119
121
  /// Best decision vector found.
120
122
  pub x: Vec<f64>,
121
123
  /// Objective value at [`x`](Self::x).
122
124
  pub y: f64,
123
- /// Number of objective evaluations charged to the run.
125
+ /// Objective evaluations actually made; never more than `max_evaluations`.
124
126
  pub evaluations: u64,
125
127
  /// Number of candidates incorporated into the optimizer state.
126
128
  pub iterations: i32,
@@ -2398,12 +2400,34 @@ impl BiteOpt {
2398
2400
  /// # Panics
2399
2401
  ///
2400
2402
  /// Panics if `eval_batch` does not return one cost per row.
2401
- pub fn optimize_batch<F>(&mut self, batch: usize, mut eval_batch: F) -> BiteResult
2403
+ pub fn optimize_batch<F>(&mut self, batch: usize, eval_batch: F) -> BiteResult
2404
+ where
2405
+ F: FnMut(&[Vec<f64>]) -> Vec<f64>,
2406
+ {
2407
+ self.optimize_batch_until(batch, eval_batch, &|| false)
2408
+ }
2409
+
2410
+ /// Batched optimization with a cooperative stop checked before each batch.
2411
+ /// An in-flight batch is incorporated in full, with exact evaluation counts.
2412
+ ///
2413
+ /// # Panics
2414
+ /// Panics if `eval_batch` does not return one cost per row.
2415
+ pub fn optimize_batch_until<F>(
2416
+ &mut self,
2417
+ batch: usize,
2418
+ mut eval_batch: F,
2419
+ stop: &dyn Fn() -> bool,
2420
+ ) -> BiteResult
2402
2421
  where
2403
2422
  F: FnMut(&[Vec<f64>]) -> Vec<f64>,
2404
2423
  {
2405
2424
  let batch = batch.max(1);
2406
2425
  while self.evaluations < self.max_evaluations && self.stop == 0 {
2426
+ if stop() {
2427
+ let mut result = self.result();
2428
+ result.stopped = true;
2429
+ return result;
2430
+ }
2407
2431
  let rows = self.ask(batch);
2408
2432
  if rows.is_empty() {
2409
2433
  break;
@@ -2452,7 +2476,17 @@ impl BiteOpt {
2452
2476
 
2453
2477
  /// Run until the evaluation budget or a stop condition is reached.
2454
2478
  pub fn optimize(&mut self, obj: &impl Objective) -> BiteResult {
2479
+ self.optimize_until(obj, &|| false)
2480
+ }
2481
+
2482
+ /// Like [`Self::optimize`], checking `stop` before every evaluation.
2483
+ pub fn optimize_until(&mut self, obj: &impl Objective, stop: &dyn Fn() -> bool) -> BiteResult {
2455
2484
  while self.evaluations < self.max_evaluations && self.stop == 0 {
2485
+ if stop() {
2486
+ let mut result = self.result();
2487
+ result.stopped = true;
2488
+ return result;
2489
+ }
2456
2490
  self.optimize_step(obj);
2457
2491
  self.iterations += 1;
2458
2492
  self.evaluations += 1;
@@ -2463,6 +2497,7 @@ impl BiteOpt {
2463
2497
 
2464
2498
  fn result(&self) -> BiteResult {
2465
2499
  BiteResult {
2500
+ stopped: false,
2466
2501
  x: self.best_values.clone(),
2467
2502
  y: self.best_cost,
2468
2503
  evaluations: self.evaluations,
@@ -2583,7 +2618,17 @@ impl DeepBiteOpt {
2583
2618
 
2584
2619
  /// One-shot optimization until the (total) evaluation budget or a stop.
2585
2620
  pub fn optimize(&mut self, obj: &impl Objective) -> BiteResult {
2621
+ self.optimize_until(obj, &|| false)
2622
+ }
2623
+
2624
+ /// Deep BiteOpt with a cooperative stop before every evaluation.
2625
+ pub fn optimize_until(&mut self, obj: &impl Objective, stop: &dyn Fn() -> bool) -> BiteResult {
2586
2626
  while self.total_evaluations() < self.max_evaluations && self.stop == 0 {
2627
+ if stop() {
2628
+ let mut result = self.result();
2629
+ result.stopped = true;
2630
+ return result;
2631
+ }
2587
2632
  self.pick_push();
2588
2633
  let opt_idx = self.cur_opt;
2589
2634
  let (sc, push) = self.opts[opt_idx].step_collect(obj, self.m > 1);
@@ -2696,6 +2741,7 @@ impl DeepBiteOpt {
2696
2741
  pub fn result(&self) -> BiteResult {
2697
2742
  let b = self.best();
2698
2743
  BiteResult {
2744
+ stopped: false,
2699
2745
  x: b.best_values.clone(),
2700
2746
  y: b.best_cost,
2701
2747
  evaluations: self.evaluations,
@@ -43,7 +43,7 @@ pub struct AcmaResult {
43
43
  pub x: Vec<f64>,
44
44
  /// Objective value at [`x`](Self::x).
45
45
  pub y: f64,
46
- /// Number of objective evaluations charged to the run.
46
+ /// Objective evaluations actually made (see `max_evaluations`).
47
47
  pub evaluations: u64,
48
48
  /// Number of completed generations.
49
49
  pub iterations: i32,
@@ -58,7 +58,9 @@ pub struct CmaesParams {
58
58
  pub popsize: i32,
59
59
  /// Number of selected parents; non-positive values use half the population.
60
60
  pub mu: i32,
61
- /// Maximum number of objective evaluations.
61
+ /// Evaluation budget, checked before each generation: whole generations
62
+ /// are evaluated, so a run can make up to `popsize - 1` more evaluations.
63
+ /// The result's `evaluations` field reports the actual count.
62
64
  pub max_evaluations: u64,
63
65
  /// Scale applied to numerical termination tolerances.
64
66
  pub accuracy: f64,
@@ -607,9 +609,22 @@ impl Cmaes {
607
609
  /// Run the full generational loop, evaluating each population through
608
610
  /// `obj` with the requested worker count.
609
611
  pub fn optimize(&mut self, obj: &impl Objective, workers: i32) -> AcmaResult {
612
+ self.optimize_until(obj, workers, &|| false)
613
+ }
614
+
615
+ /// Like [`Self::optimize`], stopping before the next population.
616
+ pub fn optimize_until(
617
+ &mut self,
618
+ obj: &impl Objective,
619
+ workers: i32,
620
+ stop: &dyn Fn() -> bool,
621
+ ) -> AcmaResult {
610
622
  self.iterations = 0;
611
623
  self.fitfun.reset_evaluations();
612
624
  while self.fitfun.evaluations() < self.max_evaluations && !self.fitfun.terminate() {
625
+ if stop() {
626
+ break;
627
+ }
613
628
  let xs = self.ask_all();
614
629
  let ys = self
615
630
  .fitfun
@@ -763,7 +778,7 @@ mod tests {
763
778
  .map(|s| run("rosen", rosen as fn(&[f64]) -> f64, 5, -5.0, 5.0, s))
764
779
  .collect();
765
780
  vals.sort_by(|a, b| a.partial_cmp(b).unwrap());
766
- assert!(vals[2] < 1.0, "rosenbrock median too large: {:?}", vals);
781
+ assert!(vals[2] < 1.0, "rosenbrock median too large: {vals:?}");
767
782
  }
768
783
 
769
784
  #[test]
@@ -47,7 +47,7 @@ pub struct CrfmnesResult {
47
47
  pub x: Vec<f64>,
48
48
  /// Objective value at [`x`](Self::x).
49
49
  pub y: f64,
50
- /// Number of objective evaluations charged to the run.
50
+ /// Objective evaluations actually made (see `max_evaluations`).
51
51
  pub evaluations: u64,
52
52
  /// Number of completed generations.
53
53
  pub iterations: i32,
@@ -60,7 +60,9 @@ pub struct CrfmnesResult {
60
60
  pub struct CrfmnesParams {
61
61
  /// Population size; values below two are raised to two.
62
62
  pub popsize: i32,
63
- /// Maximum number of objective evaluations.
63
+ /// Evaluation budget, checked before each generation: whole generations
64
+ /// are evaluated, so a run can make up to `popsize - 1` more evaluations.
65
+ /// The result's `evaluations` field reports the actual count.
64
66
  pub max_evaluations: u64,
65
67
  /// Stop after finding an objective value strictly below this threshold.
66
68
  pub stop_fitness: f64,
@@ -507,13 +509,28 @@ impl Crfmnes {
507
509
 
508
510
  /// Run the generational loop, evaluating each population through
509
511
  /// `eval_batch(decoded_rows) -> ys`.
510
- pub fn optimize_batch<F>(&mut self, mut eval_batch: F) -> CrfmnesResult
512
+ pub fn optimize_batch<F>(&mut self, eval_batch: F) -> CrfmnesResult
513
+ where
514
+ F: FnMut(&[Vec<f64>]) -> Vec<f64>,
515
+ {
516
+ self.optimize_batch_until(eval_batch, &|| false)
517
+ }
518
+
519
+ /// Like [`Self::optimize_batch`], stopping before the next population.
520
+ pub fn optimize_batch_until<F>(
521
+ &mut self,
522
+ mut eval_batch: F,
523
+ stop: &dyn Fn() -> bool,
524
+ ) -> CrfmnesResult
511
525
  where
512
526
  F: FnMut(&[Vec<f64>]) -> Vec<f64>,
513
527
  {
514
528
  self.iterations = 1;
515
529
  self.fitfun.reset_evaluations();
516
530
  while self.fitfun.evaluations() < self.max_evaluations && !self.fitfun.terminate() {
531
+ if stop() {
532
+ break;
533
+ }
517
534
  self.ask();
518
535
  let rows = self.decode_columns(&self.x);
519
536
  let mut ys = eval_batch(&rows);
@@ -40,22 +40,27 @@ use crate::rng::Rng;
40
40
  /// Outcome of a Dual Annealing run.
41
41
  #[derive(Clone, Debug)]
42
42
  pub struct DaResult {
43
- /// Best decoded decision vector found.
43
+ /// Best decoded decision vector found. When no start point was accepted
44
+ /// (`stop == -1`), the best point evaluated, or the last point tried if
45
+ /// every value was NaN or `+inf`.
44
46
  pub x: Vec<f64>,
45
- /// Objective value at [`x`](Self::x).
47
+ /// Objective value at [`x`](Self::x); `f64::MAX` if it was NaN.
46
48
  pub y: f64,
47
- /// Number of objective evaluations charged to the run.
49
+ /// Objective evaluations actually made; never more than
50
+ /// [`DaParams::max_evaluations`].
48
51
  pub evaluations: u64,
49
52
  /// Reserved iteration counter; currently zero.
50
53
  pub iterations: i32,
51
- /// Termination code; `-1` indicates initialization failed.
54
+ /// Termination code; `-1` means no start point with a value below `1e16`
55
+ /// was found within 1,000 attempts or the evaluation budget.
52
56
  pub stop: i32,
53
57
  }
54
58
 
55
59
  /// Tunable inputs for [`optimize_da`].
56
60
  #[derive(Clone, Debug)]
57
61
  pub struct DaParams {
58
- /// Maximum number of global and local objective evaluations combined.
62
+ /// Maximum number of global and local objective evaluations combined,
63
+ /// including the attempts to find a start point; `0` selects 10,000,000.
59
64
  pub max_evaluations: u64,
60
65
  /// Enable the projected limited-memory local-search phase.
61
66
  pub use_local_search: bool,
@@ -307,6 +312,9 @@ impl<'a, O: Objective> Da<'a, O> {
307
312
  loop {
308
313
  self.current_energy = self.value(&self.current_location.clone());
309
314
  if self.current_energy >= BIG_VALUE || self.current_energy.is_nan() {
315
+ if self.max_eval_reached() {
316
+ return;
317
+ }
310
318
  if reinit_counter >= MAX_REINIT_COUNT {
311
319
  self.reinit_failed = true;
312
320
  return;
@@ -480,8 +488,8 @@ impl<'a, O: Objective> Da<'a, O> {
480
488
  for i in (0..kh).rev() {
481
489
  let a = rho[i] * dot(&s_hist[i], &q);
482
490
  alpha[i] = a;
483
- for j in 0..self.dim {
484
- q[j] -= a * y_hist[i][j];
491
+ for (qj, yj) in q.iter_mut().zip(&y_hist[i]) {
492
+ *qj -= a * yj;
485
493
  }
486
494
  }
487
495
  let gamma = if kh > 0 {
@@ -495,8 +503,8 @@ impl<'a, O: Objective> Da<'a, O> {
495
503
  }
496
504
  for i in 0..kh {
497
505
  let beta = rho[i] * dot(&y_hist[i], &q);
498
- for j in 0..self.dim {
499
- q[j] += (alpha[i] - beta) * s_hist[i][j];
506
+ for (qj, sj) in q.iter_mut().zip(&s_hist[i]) {
507
+ *qj += (alpha[i] - beta) * sj;
500
508
  }
501
509
  }
502
510
  let d: Vec<f64> = q.iter().map(|v| -v).collect();
@@ -567,7 +575,7 @@ impl<'a, O: Objective> Da<'a, O> {
567
575
  }
568
576
  if temperature < TEMPERATURE_RESTART {
569
577
  self.reset_energy(&[]);
570
- if self.reinit_failed {
578
+ if self.reinit_failed || self.max_eval_reached() {
571
579
  return;
572
580
  }
573
581
  break;
@@ -592,15 +600,36 @@ impl<'a, O: Objective> Da<'a, O> {
592
600
  self.emin = self.current_energy;
593
601
  self.xmin = self.current_location.clone();
594
602
  self.not_improved_max_idx = 1000;
595
- if !self.reinit_failed {
603
+ if !self.reinit_failed && !self.max_eval_reached() {
596
604
  self.search();
597
605
  }
606
+ let started = !self.xbest.is_empty();
607
+ let (x, y) = if started {
608
+ (self.decode(&self.xbest), self.ebest)
609
+ } else if !self.fit_best_x.is_empty() {
610
+ // No start point below BIG_VALUE: report the best point evaluated.
611
+ let x = self.closest_feasible(&self.fit_best_x);
612
+ (self.decode(&x), self.fit_best_y)
613
+ } else {
614
+ // Every value was NaN or +inf: report the last point tried.
615
+ let x = self.closest_feasible(&self.current_location);
616
+ let y = if self.current_energy.is_nan() {
617
+ f64::MAX
618
+ } else {
619
+ self.current_energy
620
+ };
621
+ (self.decode(&x), y)
622
+ };
598
623
  DaResult {
599
- x: self.decode(&self.xbest),
600
- y: self.ebest,
624
+ x,
625
+ y,
601
626
  evaluations: self.eval_counter,
602
627
  iterations: 0,
603
- stop: if self.reinit_failed { -1 } else { 0 },
628
+ stop: if self.reinit_failed || !started {
629
+ -1
630
+ } else {
631
+ 0
632
+ },
604
633
  }
605
634
  }
606
635
  }
@@ -701,6 +730,65 @@ mod tests {
701
730
  assert!(result.evaluations > 1_000);
702
731
  }
703
732
 
733
+ #[test]
734
+ fn bounded_objective_without_start_point_returns_evaluated_point() {
735
+ // Every value is at or above BIG_VALUE (a large penalty, +inf, NaN):
736
+ // no start point is accepted. The run must not panic, must keep to the
737
+ // budget and must report a point inside the box that was evaluated.
738
+ for (value, expected_y) in [
739
+ (1e300, 1e300),
740
+ (f64::INFINITY, f64::INFINITY),
741
+ (f64::NAN, f64::MAX),
742
+ ] {
743
+ let objective = move |_: &[f64]| value;
744
+ for max_evaluations in [60, 5_000] {
745
+ let result = optimize_da(
746
+ &objective,
747
+ &[0.5, 0.5],
748
+ vec![-1.0, 0.0],
749
+ vec![1.0, 2.0],
750
+ &DaParams {
751
+ max_evaluations,
752
+ use_local_search: true,
753
+ seed: 7,
754
+ runid: 0,
755
+ },
756
+ );
757
+ assert_eq!(result.stop, -1);
758
+ assert_eq!(result.y, expected_y);
759
+ assert!(result.evaluations <= max_evaluations);
760
+ assert_eq!(result.x.len(), 2);
761
+ assert!((-1.0..=1.0).contains(&result.x[0]) && (0.0..=2.0).contains(&result.x[1]));
762
+ }
763
+ }
764
+ }
765
+
766
+ #[test]
767
+ fn budget_bounds_start_point_search() {
768
+ let calls = std::sync::atomic::AtomicU64::new(0);
769
+ let objective = |x: &[f64]| {
770
+ calls.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
771
+ if x[0] > 0.9 { x[0] } else { 1e20 }
772
+ };
773
+ let result = optimize_da(
774
+ &objective,
775
+ &[0.0],
776
+ vec![0.0],
777
+ vec![1.0],
778
+ &DaParams {
779
+ max_evaluations: 200,
780
+ use_local_search: false,
781
+ seed: 3,
782
+ runid: 0,
783
+ },
784
+ );
785
+ assert!(result.evaluations <= 200);
786
+ assert_eq!(
787
+ result.evaluations,
788
+ calls.load(std::sync::atomic::Ordering::Relaxed)
789
+ );
790
+ }
791
+
704
792
  #[test]
705
793
  fn finite_difference_handles_both_box_boundaries() {
706
794
  let objective = sphere as fn(&[f64]) -> f64;