Skip to content

Commit

Permalink
Merge pull request #1186 from YuliaProkopovych/opt-params
Browse files Browse the repository at this point in the history
NOT READY(optimization_tools): Total iterations statistics
  • Loading branch information
Wandalen authored Apr 25, 2024
2 parents e46d69a + 6e220b9 commit b8be5b7
Show file tree
Hide file tree
Showing 11 changed files with 201 additions and 224 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,6 @@ path = "module/test/c"
default-features = true


# [patch.crates-io]
# pathfinder_geometry = { git = "https://github.com/servo/pathfinder.git" }
# pathfinder_simd = { git = "https://github.com/servo/pathfinder.git" }
[patch.crates-io]
pathfinder_geometry = { git = "https://github.com/servo/pathfinder.git" }
pathfinder_simd = { git = "https://github.com/servo/pathfinder.git" }
13 changes: 11 additions & 2 deletions module/move/optimization_tools/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ dynamic_plot = [ "static_plot", "plotters-backend", "piston_window" ]
lp_parse = [ "exmex" ]

[dependencies]
derive_tools = { workspace = true, features = [ "derive_more", "full" ] }
derive_tools = { workspace = true, features = [ "derive_more", "full", "strum" ] }
deterministic_rand = { workspace = true, features = [ "default" ] }
iter_tools = { workspace = true, features = [ "default" ] }
meta_tools = { workspace = true, features = [ "meta_constructors" ] }
Expand All @@ -48,7 +48,16 @@ rand = "0.8.5"
statrs = "0.16.0"
faer = { version = "0.16.0", features = [ "ndarray" ] }
ndarray = "0.15.6"
plotters = { version = "0.3.5" }
plotters = { git = "https://github.com/plotters-rs/plotters.git" }
# plotters = { version = "0.3.5", default-features=false, features = [
# "bitmap_encoder",
# "ttf",
# "area_series",
# "point_series",
# "line_series",
# "full_palette",
# "bitmap_backend",
# ] }
plotters-backend = { version = "0.3.5", optional = true }
piston_window = { version = "0.120.0", optional = true }
exmex = { version = "0.18.0", features = [ "partial" ], optional = true }
Expand Down
2 changes: 1 addition & 1 deletion module/move/optimization_tools/src/hybrid_optimizer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use iter_tools::Itertools;
use std::ops::RangeInclusive;
use rayon::iter::{ ParallelIterator, IndexedParallelIterator};
use deterministic_rand::{ Seed, seq::{ SliceRandom, IteratorRandom } };
use derive_tools::Display;
use derive_tools::exposed::Display;
use optimal_params_search::OptimalProblem;

mod gen_alg;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Implementation of Simulated Annealing for Hybrid Optimizer.

use derive_tools::{ FromInner, InnerFrom, Display };
use derive_tools::{ FromInner, InnerFrom, exposed::Display };
/// Represents temperature of SA process.
#[ derive( Default, Debug, Display, Clone, Copy, PartialEq, PartialOrd, FromInner, InnerFrom ) ]
pub struct Temperature( f64 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl Default for OptimalParamsConfig
{
improvement_threshold : 0.005,
max_no_improvement_steps : 10,
max_iterations : 100,
max_iterations : 50,
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@

use std::
{
collections::HashMap,
fs::{ File, OpenOptions },
ops::{ Bound, RangeBounds },
sync::{ Arc, Mutex },
collections::HashMap, fs::{ File, OpenOptions }, ops::{ Bound, RangeBounds }, sync::{ Arc, Mutex }
};
use deterministic_rand::{ Hrng, Seed, Rng };
use iter_tools::Itertools;
Expand Down Expand Up @@ -65,7 +62,9 @@ impl Constraints
#[ derive( Debug, Clone ) ]
pub struct Stats
{

pub number_of_iterations : usize,
pub number_of_starting_points : usize,
pub resumed_after_stale : usize,
pub starting_point : Point,
pub differences : Vec< Vec< f64 > >,
pub positive_change : Vec< usize >,
Expand All @@ -79,6 +78,9 @@ impl Stats
let dimensions = starting_point.coords.len();
Self
{
number_of_iterations : 0,
number_of_starting_points : 1,
resumed_after_stale : 0,
starting_point,
differences : vec![ Vec::new(); dimensions ],
positive_change : vec![ 0; dimensions ],
Expand Down Expand Up @@ -488,6 +490,7 @@ where R : RangeBounds< f64 > + Sync,
let results = points.into_par_iter().map( | point |
{
let mut stats = Stats::new( point.clone() );
stats.number_of_starting_points = points_number;
let x0 = point.clone();
let dimensions = x0.coords.len();
let mut prev_best = self.evaluate_point( &x0, &mut stats );
Expand All @@ -509,19 +512,22 @@ where R : RangeBounds< f64 > + Sync,

if self.max_iterations <= iterations
{
return Result::< Solution, Error >::Ok ( Solution
stats.number_of_iterations = iterations;
return Result::< Solution, Error >::Ok ( Solution
{
point : res[ 0 ].0.clone(),
objective : res[ 0 ].1,
reason : TerminationReason::MaxIterations,
stats : Some( stats ),
} )
}

iterations += 1;


if best.1 < prev_best - self.improvement_threshold
{
if steps_with_no_improv > 0
{
stats.resumed_after_stale += 1;
}
steps_with_no_improv = 0;
prev_best = best.1;
}
Expand All @@ -532,7 +538,8 @@ where R : RangeBounds< f64 > + Sync,

if steps_with_no_improv >= self.max_no_improvement_steps
{
return Ok ( Solution
stats.number_of_iterations = iterations;
return Ok ( Solution
{
point : res[ 0 ].0.clone(),
objective : res[ 0 ].1,
Expand All @@ -541,6 +548,8 @@ where R : RangeBounds< f64 > + Sync,
} )
}

iterations += 1;

//centroid
let mut x0_center = vec![ 0.0; dimensions ];
for ( point, _ ) in res.iter().take( res.len() - 1 )
Expand Down Expand Up @@ -569,7 +578,6 @@ where R : RangeBounds< f64 > + Sync,
let prev_point = res.pop().unwrap().0;
stats.record_positive_change( &prev_point, &x_ref );
res.push( ( x_ref, reflection_score ) );
// log::info!("reflection");
continue;
}

Expand All @@ -591,7 +599,6 @@ where R : RangeBounds< f64 > + Sync,
let prev_point = res.pop().unwrap().0;
stats.record_positive_change( &prev_point, &x_exp );
res.push( ( x_exp, expansion_score ) );
// log::info!("expansion");
continue;

}
Expand All @@ -600,7 +607,6 @@ where R : RangeBounds< f64 > + Sync,
let prev_point = res.pop().unwrap().0;
stats.record_positive_change( &prev_point, &x_ref );
res.push( ( x_ref, reflection_score ) );
// log::info!("expansion");
continue;
}
}
Expand All @@ -620,7 +626,6 @@ where R : RangeBounds< f64 > + Sync,
let prev_point = res.pop().unwrap().0;
stats.record_positive_change( &prev_point, &x_con );
res.push( ( x_con, contraction_score ) );
// log::info!("contraction");
continue;
}

Expand All @@ -639,7 +644,6 @@ where R : RangeBounds< f64 > + Sync,
let score = self.evaluate_point( &x_shrink, &mut stats );
new_res.push( ( x_shrink, score ) );
}
// log::info!("shrink");
res = new_res;
}
} ).collect::< Vec< _ > >();
Expand Down Expand Up @@ -828,7 +832,7 @@ pub struct Solution
}

/// Reasons for termination of optimization process.
#[ derive( Debug, Clone ) ]
#[ derive( Debug, Clone, derive_tools::Display ) ]
pub enum TerminationReason
{
/// Reached limit of total iterations.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Contains CellVal structure that corresponds to single digit on Sudoku field.
//!

use derive_tools::Display;
use derive_tools::exposed::Display;

/// Represents the value of a cell in Sudoku. It can have a value from 1 to 9 or 0 if the cell is not assigned.
#[ derive( Default, Debug, Display, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Hash ) ]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::collections::HashSet;
use crate::hybrid_optimizer::*;
use crate::problems::sudoku::*;

use derive_tools::{ FromInner, InnerFrom, Display };
use derive_tools::{ FromInner, InnerFrom, exposed::Display };
use deterministic_rand::{ Hrng, Rng, seq::SliceRandom };
use iter_tools::Itertools;

Expand Down
Loading

0 comments on commit b8be5b7

Please sign in to comment.