converted config to a record type and changed frame step results to something more lucid

This commit is contained in:
Mark Wolters 2024-03-22 12:50:21 -04:00 committed by Jonathan Shook
parent 7d961cb351
commit 7edf6fb85c
5 changed files with 85 additions and 118 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,16 +67,14 @@ public class CMD_findmax extends NBBaseCommand {
1.1d,
SimRateSpec.Verb.restart)))
);
break;
case "threads":
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))))
);
break;
default:
default ->
throw new RuntimeException("Unsupported optimization type: " + findmaxConfig.optimization_type());
}

View File

@ -18,6 +18,7 @@
package io.nosqlbench.scenarios.simframe.optimizers.findmax;
import io.nosqlbench.scenarios.simframe.planning.SimFrame;
import io.nosqlbench.scenarios.simframe.planning.SimFrameAction;
import io.nosqlbench.scenarios.simframe.planning.SimFrameFunctionAnalyzer;
import java.util.Comparator;
@ -31,23 +32,44 @@ public class FindmaxAnalyzer extends SimFrameFunctionAnalyzer<FindmaxFrameFuncti
}
@Override
protected double nextFrame() {
protected FrameResult nextFrame() {
double newValue;
SimFrame<FindmaxFrameParams> last = function.getJournal().last();
SimFrame<FindmaxFrameParams> best = function.getJournal().bestRun();
if (best.index() == last.index()) { // got better consecutively
newValue = last.params().paramValues()[0] + config.step_value();
config.setStep_value(config.step_value() * config.value_incr());
config = new FindmaxConfig(
config.sample_time_ms(),
config.max_value(),
config.base_value(),
config.min_value(),
(config.step_value() * config.value_incr()),
config.value_incr(),
config.sample_incr(),
config.min_settling_ms(),
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 Double.MIN_VALUE;
return new FrameResult(best.params().paramValues()[0], SimFrameAction.stop_run);
} else {
newValue = best.params().paramValues()[0] + config.step_value();
config.setSample_time_ms(config.sample_time_ms() * config.sample_incr());
config.setMin_settling_ms(config.min_settling_ms() * 4);
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
// find next frame with higher rate but lower value, the closest one by rate
@ -59,14 +81,30 @@ public class FindmaxAnalyzer extends SimFrameFunctionAnalyzer<FindmaxFrameFuncti
if ((nextWorseFrameWithHigherRate.params().paramValues()[0] + config.step_value() -
best.params().paramValues()[0] + config.step_value()) > config.step_value()) {
newValue = best.params().paramValues()[0] + config.step_value();
config.setSample_time_ms(config.sample_time_ms() * config.sample_incr());
config.setMin_settling_ms(config.min_settling_ms() * 2);
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() * 2),
config.optimization_type(),
new double[]{newValue}
);
} else {
logger.info("could not divide search space further, stop condition met");
return Double.MIN_VALUE;
return new FrameResult(best.params().paramValues()[0], SimFrameAction.stop_run);
}
}
double[] point = {newValue};
return function.value(point);
return new FrameResult(function.value(point), SimFrameAction.continue_run);
}
@Override
protected FrameResult initialFrame() {
return new FrameResult(function.value(config.initialPoint()), SimFrameAction.continue_run);
}
}

View File

@ -17,105 +17,35 @@
package io.nosqlbench.scenarios.simframe.optimizers.findmax;
import io.nosqlbench.engine.core.lifecycle.scenario.container.NBCommandParams;
import io.nosqlbench.scenarios.simframe.planning.SimFrameConfig;
public class FindmaxConfig extends SimFrameConfig {
private double sample_time_ms;
private double max_value;
private double base_value;
private double min_value;
private double step_value;
private double value_incr;
private double sample_incr;
private long min_settling_ms;
private String optimization_type;
public double sample_time_ms() {
return sample_time_ms;
}
public double max_value() {
return max_value;
}
public double base_value() {
return base_value;
}
@Override
public record FindmaxConfig (
double sample_time_ms,
double max_value,
double base_value,
double min_value,
double step_value,
double value_incr,
double sample_incr,
long min_settling_ms,
String optimization_type,
double[] initial_point
) {
public double[] initialPoint() {
return new double[]{base_value};
}
public double min_value() {
return min_value;
}
public double step_value() {
return step_value;
}
public double value_incr() {
return value_incr;
}
public double sample_incr() {
return sample_incr;
}
public long min_settling_ms() {
return min_settling_ms;
}
public String optimization_type() {
return optimization_type;
}
public void setSample_time_ms(double sample_time_ms) {
this.sample_time_ms = sample_time_ms;
}
public void setMax_value(double max_value) {
this.max_value = max_value;
}
public void setBase_value(double base_value) {
this.base_value = base_value;
}
public void setStep_value(double step_value) {
this.step_value = step_value;
}
public void setMin_value(double min_value) {
this.min_value = min_value;
}
public void setSample_incr(double sample_incr) {
this.sample_incr = sample_incr;
}
public void setValue_incr(double value_incr) {
this.value_incr = value_incr;
}
public void setMin_settling_ms(long min_settling_ms) {
this.min_settling_ms = min_settling_ms;
}
public void setOptimization_type(String optimization_type) {
this.optimization_type = optimization_type;
}
public FindmaxConfig(NBCommandParams params) {
setSample_time_ms(params.maybeGet("sample_time_ms").map(Double::parseDouble).orElse(4000d));
setMax_value(params.maybeGet("max_value").map(Double::parseDouble).orElse(10000d));
setBase_value(params.maybeGet("base_value").map(Double::parseDouble).orElse(10d));
setMin_value(params.maybeGet("min_value").map(Double::parseDouble).orElse(0d));
setStep_value(params.maybeGet("step_value").map(Double::parseDouble).orElse(100d));
setValue_incr(params.maybeGet("value_incr").map(Double::parseDouble).orElse(2d));
setSample_incr(params.maybeGet("sample_incr").map(Double::parseDouble).orElse(1.2d));
setMin_settling_ms(params.maybeGet("min_settling_ms").map(Long::parseLong).orElse(4000L));
setOptimization_type(params.maybeGet("optimization_type").orElse("rate"));
this(
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("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("optimization_type").orElse("rate"),
new double[]{params.maybeGet("base_value").map(Double::parseDouble).orElse(10d)}
);
}
}

View File

@ -17,8 +17,7 @@
package io.nosqlbench.scenarios.simframe.planning;
public abstract class SimFrameConfig {
public abstract double base_value();
public abstract double[] initialPoint();
public enum SimFrameAction {
continue_run,
stop_run
}

View File

@ -19,23 +19,25 @@ package io.nosqlbench.scenarios.simframe.planning;
import io.nosqlbench.engine.core.lifecycle.scenario.container.InvokableResult;
public abstract class SimFrameFunctionAnalyzer<A extends SimFrameFunction<? extends InvokableResult>, C extends SimFrameConfig> {
public abstract class SimFrameFunctionAnalyzer<A extends SimFrameFunction<? extends InvokableResult>, C extends Record> {
protected final A function;
protected final C config;
protected C config;
protected SimFrameFunctionAnalyzer(A function, C config) {
this.function = function;
this.config = config;
}
public record FrameResult(double value, SimFrameAction action) {}
public SimFrame<? extends InvokableResult> analyze() {
double[] initialPoint = config.initialPoint();
double result = function.value(initialPoint);
while (result != Double.MIN_VALUE) {
FrameResult result = initialFrame();
while (result.action() == SimFrameAction.continue_run) {
result = nextFrame();
}
return function.getJournal().bestRun();
}
protected abstract double nextFrame();
protected abstract FrameResult nextFrame();
protected abstract FrameResult initialFrame();
}