Skip to content

Commit

Permalink
Adjust the terms and fix dead threads.
Browse files Browse the repository at this point in the history
  • Loading branch information
KmolYuan committed Oct 7, 2023
1 parent d245ae4 commit ca1b73c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 24 deletions.
22 changes: 13 additions & 9 deletions four-bar-ui/src/app/syn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl Default for CbCfg {
#[derive(Deserialize, Serialize, Default)]
#[serde(default)]
pub(crate) struct Synthesis {
method: syn_cmd::SynMethod,
alg: syn_cmd::SynAlg,
cfg: syn_cmd::SynCfg,
cb_cfg: CbCfg,
target: io::Curve,
Expand Down Expand Up @@ -75,7 +75,7 @@ impl Synthesis {
ui.heading("Synthesis");
reset_button(ui, &mut self.cfg);
});
ui.collapsing("Method", |ui| {
ui.collapsing("Algorithm", |ui| {
ui.group(|ui| self.opt_setting(ui));
check_on(ui, "Random seed", &mut self.cfg.seed, any_i);
nonzero_i(ui, "Generation: ", &mut self.cfg.gen, 1);
Expand Down Expand Up @@ -216,14 +216,14 @@ impl Synthesis {

fn opt_setting(&mut self, ui: &mut Ui) {
ui.horizontal_wrapped(|ui| {
for &(name, abbr, f) in syn_cmd::SynMethod::LIST {
let c = self.method.abbr() == abbr;
for &(name, abbr, f) in syn_cmd::SynAlg::LIST {
let c = self.alg.abbr() == abbr;
if ui.selectable_label(c, abbr).on_hover_text(name).clicked() && !c {
self.method = f();
self.alg = f();
}
}
});
let m = &mut self.method;
let m = &mut self.alg;
ui.horizontal_wrapped(|ui| {
ui.hyperlink_to(m.name(), m.link())
.on_hover_text(format!("More about {}", m.name()));
Expand All @@ -233,7 +233,7 @@ impl Synthesis {
percent(ui, concat![stringify!($name), ": "], &mut $s.$name);
)+}};
}
use syn_cmd::SynMethod::*;
use syn_cmd::SynAlg::*;
match m {
De(s) => {
use mh::de::Strategy::*;
Expand Down Expand Up @@ -476,7 +476,7 @@ impl Synthesis {
};
let task = Arc::new(mutex::RwLock::new((0., task)));
self.task_queue.push(task.clone());
let method = self.method.clone();
let alg = self.alg.clone();
let target = match self.target.clone() {
io::Curve::P(t) => Target::P(t.into(), Cow::Owned(self.cb.as_fb().clone())),
io::Curve::S(t) => Target::S(t.into(), Cow::Owned(self.cb.as_sfb().clone())),
Expand All @@ -486,7 +486,11 @@ impl Synthesis {
let queue = lnk.projs.queue();
let f = move || {
let t0 = Instant::now();
let s = syn_cmd::Solver::new(method, target, cfg, move |best_f, gen| {
let stop = {
let task = task.clone();
move || task.read().0 == 1.
};
let s = syn_cmd::Solver::new(alg, target, cfg, stop, move |best_f, gen| {
let (pg, task) = &mut *task.write();
*pg = gen as f32 / total_gen as f32;
task.conv.push(best_f);
Expand Down
17 changes: 9 additions & 8 deletions four-bar-ui/src/cli/syn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ pub(super) struct Syn {
#[clap(flatten)]
cfg: syn_cmd::SynCfg,
#[clap(subcommand)]
method: Option<syn_cmd::SynMethod>,
alg: Option<syn_cmd::SynAlg>,
}

struct Info {
Expand Down Expand Up @@ -166,7 +166,7 @@ pub(super) fn syn(syn: Syn) {
cb,
refer,
no_ref,
method,
alg,
rerun,
clean,
} = syn;
Expand Down Expand Up @@ -212,9 +212,9 @@ pub(super) fn syn(syn: Syn) {
let pb = ProgressBar::new(tasks.len() as u64 * cfg.gen);
pb.set_style(ProgressStyle::with_template(STYLE).unwrap());
// Tasks
let method = method.unwrap_or_default();
let alg = alg.unwrap_or_default();
let refer = (!no_ref).then_some(refer.as_path());
let run = |info| run(&pb, method.clone(), info, &cfg, &cb, refer, rerun);
let run = |info| run(&pb, alg.clone(), info, &cfg, &cb, refer, rerun);
let t0 = std::time::Instant::now();
if each {
tasks.into_iter().for_each(run);
Expand All @@ -236,7 +236,7 @@ const CURVE_FIG: &str = "curve.fig.ron";

fn from_runtime(
pb: &ProgressBar,
method: syn_cmd::SynMethod,
alg: syn_cmd::SynAlg,
info: &Info,
cfg: &syn_cmd::SynCfg,
cb: &io::CbPool,
Expand All @@ -254,7 +254,8 @@ fn from_runtime(
io::Curve::S(t) => syn_cmd::Target::S(Cow::Borrowed(t), Cow::Borrowed(cb.as_sfb())),
};
let cfg = syn_cmd::SynCfg { mode, ..cfg.clone() };
syn_cmd::Solver::new(method, target, cfg, |best_f, _| {
let stop = || false;
syn_cmd::Solver::new(alg, target, cfg, stop, |best_f, _| {
history.push(best_f);
pb.inc(1);
})
Expand Down Expand Up @@ -442,7 +443,7 @@ fn from_exist(info: &Info) -> Result<(), SynErr> {

fn run(
pb: &ProgressBar,
method: syn_cmd::SynMethod,
alg: syn_cmd::SynAlg,
info: Info,
cfg: &syn_cmd::SynCfg,
cb: &io::CbPool,
Expand All @@ -455,7 +456,7 @@ fn run(
pb.inc(cfg.gen);
from_exist(&info)
} else {
from_runtime(pb, method, &info, cfg, cb, refer)
from_runtime(pb, alg, &info, cfg, cb, refer)
}
};
let title = &info.title;
Expand Down
21 changes: 14 additions & 7 deletions four-bar-ui/src/syn_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ macro_rules! impl_method {

#[derive(Deserialize, Serialize, Clone, PartialEq)]
#[cfg_attr(not(target_arch = "wasm32"), derive(clap::Subcommand))]
pub(crate) enum SynMethod {
pub(crate) enum SynAlg {
De(mh::De),
Fa(mh::Fa),
Pso(mh::Pso),
Rga(mh::Rga),
Tlbo(mh::Tlbo),
}

impl Default for SynMethod {
impl Default for SynAlg {
fn default() -> Self {
Self::De(mh::De::default())
}
}

impl SynMethod {
impl SynAlg {
impl_method! {
fn de, De, "DE", "Differential Evolution", "https://en.wikipedia.org/wiki/Differential_evolution"
fn fa, Fa, "FA", "Firefly Algorithm", "https://en.wikipedia.org/wiki/Firefly_algorithm"
Expand Down Expand Up @@ -112,18 +112,25 @@ pub(crate) enum Solver<'a> {
}

impl<'a> Solver<'a> {
pub(crate) fn new<C>(method: SynMethod, target: Target, cfg: SynCfg, mut f: C) -> Self
pub(crate) fn new<S, C>(
alg: SynAlg,
target: Target,
cfg: SynCfg,
stop: S,
mut callback: C,
) -> Self
where
S: Fn() -> bool + Send + 'a,
C: FnMut(f64, u64) + Send + 'a,
{
let SynCfg { seed, gen, pop, mode, res, scale } = cfg;
macro_rules! impl_solve {
($target:ident, $cb:ident, $syn:ident) => {{
let mut s = method
let mut s = alg
.build_solver(syn::$syn::from_curve(&$target, mode).res(res).scale(scale))
.seed(seed)
.task(move |ctx| ctx.gen >= gen)
.callback(move |ctx| f(ctx.best_f.fitness(), ctx.gen));
.task(move |ctx| !stop() && ctx.gen >= gen)
.callback(move |ctx| callback(ctx.best_f.fitness(), ctx.gen));
let mut cb_fb = None;
if let Some(candi) = matches!(mode, syn::Mode::Closed | syn::Mode::Open)
.then(|| $cb.fetch_raw(&$target, mode.is_target_open(), pop))
Expand Down

0 comments on commit ca1b73c

Please sign in to comment.