added min_frames and contra_base_value params

This commit is contained in:
Mark Wolters 2024-04-17 09:56:55 -04:00
parent 2ce59e8899
commit 3fab415c6d
3 changed files with 29 additions and 26 deletions

View File

@ -57,7 +57,7 @@ public class CMD_findmax extends NBBaseCommand {
FindmaxConfig findmaxConfig = new FindmaxConfig(params);
switch(findmaxConfig.optimization_type()) {
case "rate" ->
case "rate" -> {
model.add("rate",
findmaxConfig.min_value(), // min
findmaxConfig.base_value(), // initial
@ -67,13 +67,24 @@ public class CMD_findmax extends NBBaseCommand {
1.1d,
SimRateSpec.Verb.restart)))
);
case "threads" ->
if (findmaxConfig.contra_base_value() != 0) {
flywheel.onEvent(ParamChange.of(new SetThreads((int) (findmaxConfig.contra_base_value()))));
}
}
case "threads" -> {
model.add("threads",
findmaxConfig.min_value(), // min
findmaxConfig.base_value(), // initial
findmaxConfig.max_value(), // max
threads -> flywheel.onEvent(ParamChange.of(new SetThreads((int) (threads))))
);
if (findmaxConfig.contra_base_value() != 0) {
flywheel.onEvent(ParamChange.of(new CycleRateSpec(
findmaxConfig.contra_base_value(),
1.1d,
SimRateSpec.Verb.restart)));
}
}
default ->
throw new RuntimeException("Unsupported optimization type: " + findmaxConfig.optimization_type());
}

View File

@ -31,6 +31,12 @@ public class FindmaxAnalyzer extends SimFrameFunctionAnalyzer<FindmaxFrameFuncti
super(function, config);
}
protected boolean shouldStop(SimFrame<FindmaxFrameParams> compFrame) {
return function.getJournal().frames().size() >= config.min_frames() &&
((compFrame.params().paramValues()[0] + config.step_value()) -
(function.getJournal().bestRun().params().paramValues()[0] + config.step_value())) <= config.step_value();
}
@Override
protected FrameResult nextFrame() {
double newValue;
@ -42,54 +48,36 @@ public class FindmaxAnalyzer extends SimFrameFunctionAnalyzer<FindmaxFrameFuncti
config.sample_time_ms(),
config.max_value(),
config.base_value(),
config.contra_base_value(),
config.min_value(),
(config.step_value() * config.value_incr()),
config.value_incr(),
config.sample_incr(),
config.min_settling_ms(),
config.min_frames(),
config.optimization_type(),
new double[]{newValue}
);
} else if (best.index() == last.index() - 1) {
// got worse consecutively, this may be collapsed out since the general case below covers it (test first)
if (((last.params().paramValues()[0] + config.step_value()) -
(best.params().paramValues()[0] + config.step_value())) <= config.step_value()) {
logger.info("could not divide search space further, stop condition met");
return new FrameResult(best.params().paramValues()[0], SimFrameAction.stop_run);
} else {
newValue = best.params().paramValues()[0] + config.step_value();
config = new FindmaxConfig(
(config.sample_time_ms() * config.sample_incr()),
config.max_value(),
config.base_value(),
config.min_value(),
config.step_value(),
config.value_incr(),
config.sample_incr(),
(config.min_settling_ms() * 4),
config.optimization_type(),
new double[]{newValue}
);
}
} else { // any other case
} else {
// find next frame with higher rate but lower value, the closest one by rate
SimFrame<FindmaxFrameParams> nextWorseFrameWithHigherRate = function.getJournal().frames().stream()
.filter(f -> f.value() < best.value())
.filter(f -> f.params().paramValues()[0] + config.step_value() > (best.params().paramValues()[0] + config.step_value()))
.min(Comparator.comparingDouble(f -> f.params().paramValues()[0] + config.step_value()))
.orElseThrow(() -> new RuntimeException("inconsistent samples"));
if ((nextWorseFrameWithHigherRate.params().paramValues()[0] + config.step_value() -
best.params().paramValues()[0] + config.step_value()) > config.step_value()) {
if (!shouldStop(nextWorseFrameWithHigherRate)) {
newValue = best.params().paramValues()[0] + config.step_value();
config = new FindmaxConfig(
(config.sample_time_ms() * config.sample_incr()),
config.max_value(),
config.base_value(),
config.contra_base_value(),
config.min_value(),
config.step_value(),
config.value_incr(),
config.sample_incr(),
(config.min_settling_ms() * 2),
config.min_frames(),
config.optimization_type(),
new double[]{newValue}
);

View File

@ -22,11 +22,13 @@ public record FindmaxConfig (
double sample_time_ms,
double max_value,
double base_value,
double contra_base_value,
double min_value,
double step_value,
double value_incr,
double sample_incr,
long min_settling_ms,
long min_frames,
String optimization_type,
double[] initial_point
) {
@ -39,11 +41,13 @@ public record FindmaxConfig (
params.maybeGet("sample_time_ms").map(Double::parseDouble).orElse(4000d),
params.maybeGet("max_value").map(Double::parseDouble).orElse(10000d),
params.maybeGet("base_value").map(Double::parseDouble).orElse(10d),
params.maybeGet("contra_base_value").map(Double::parseDouble).orElse(0d),
params.maybeGet("min_value").map(Double::parseDouble).orElse(0d),
params.maybeGet("step_value").map(Double::parseDouble).orElse(100d),
params.maybeGet("value_incr").map(Double::parseDouble).orElse(2d),
params.maybeGet("sample_incr").map(Double::parseDouble).orElse(1.2d),
params.maybeGet("min_settling_ms").map(Long::parseLong).orElse(4000L),
params.maybeGet("min_frames").map(Long::parseLong).orElse(5L),
params.maybeGet("optimization_type").orElse("rate"),
new double[]{params.maybeGet("base_value").map(Double::parseDouble).orElse(10d)}
);