finished generification

This commit is contained in:
Mark Wolters 2024-03-21 12:36:22 -04:00 committed by Jonathan Shook
parent 08bd1ca029
commit 7d961cb351
8 changed files with 78 additions and 27 deletions

View File

@ -20,6 +20,7 @@ import io.nosqlbench.engine.api.activityapi.core.Activity;
import io.nosqlbench.engine.api.activityapi.simrate.CycleRateSpec;
import io.nosqlbench.engine.api.activityapi.simrate.SimRateSpec;
import io.nosqlbench.engine.core.lifecycle.scenario.container.ContainerActivitiesController;
import io.nosqlbench.engine.core.lifecycle.scenario.container.InvokableResult;
import io.nosqlbench.engine.core.lifecycle.scenario.container.NBBufferedContainer;
import io.nosqlbench.engine.core.lifecycle.scenario.container.NBCommandParams;
import io.nosqlbench.engine.core.lifecycle.scenario.execution.NBBaseCommand;
@ -31,6 +32,7 @@ import io.nosqlbench.scenarios.simframe.capture.SimFrameJournal;
import io.nosqlbench.scenarios.simframe.capture.SimFrameValueData;
import io.nosqlbench.scenarios.simframe.optimizers.CMD_optimize;
import io.nosqlbench.scenarios.simframe.planning.SimFrame;
import io.nosqlbench.scenarios.simframe.planning.SimFrameFunctionAnalyzer;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@ -80,8 +82,8 @@ public class CMD_findmax extends NBBaseCommand {
SimFrameCapture capture = new SimFrameValueData(flywheel);
FindmaxFrameFunction frameFunction = new FindmaxFrameFunction(controller, findmaxConfig, flywheel, capture, journal, model);
FindmaxAnalyzer analyzer = new FindmaxAnalyzer(frameFunction, findmaxConfig);
SimFrame<FindmaxFrameParams> best = analyzer.analyze();
SimFrameFunctionAnalyzer<FindmaxFrameFunction,FindmaxConfig> analyzer = new FindmaxAnalyzer(frameFunction, findmaxConfig);
SimFrame<? extends InvokableResult> best = analyzer.analyze();
stdout.println("Best Run:\n" + best);
return best.params();
}

View File

@ -24,27 +24,17 @@ import java.util.Comparator;
import static io.nosqlbench.virtdata.core.bindings.VirtDataLibrary.logger;
public class FindmaxAnalyzer implements SimFrameFunctionAnalyzer {
private final FindmaxFrameFunction function;
private final FindmaxConfig config;
public class FindmaxAnalyzer extends SimFrameFunctionAnalyzer<FindmaxFrameFunction,FindmaxConfig> {
public FindmaxAnalyzer(FindmaxFrameFunction function, FindmaxConfig config) {
this.function = function;
this.config = config;
}
public SimFrame<FindmaxFrameParams> analyze() {
double[] initialPoint = {config.base_value()};
double result = function.value(initialPoint);
while (result != 0.0d) {
result = nextStep();
}
return function.journal().bestRun();
super(function, config);
}
public double nextStep() {
@Override
protected double nextFrame() {
double newValue;
SimFrame<FindmaxFrameParams> last = function.journal().last();
SimFrame<FindmaxFrameParams> best = function.journal().bestRun();
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());
@ -53,7 +43,7 @@ public class FindmaxAnalyzer implements SimFrameFunctionAnalyzer {
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 0;
return Double.MIN_VALUE;
} else {
newValue = best.params().paramValues()[0] + config.step_value();
config.setSample_time_ms(config.sample_time_ms() * config.sample_incr());
@ -61,7 +51,7 @@ public class FindmaxAnalyzer implements SimFrameFunctionAnalyzer {
}
} else { // any other case
// find next frame with higher rate but lower value, the closest one by rate
SimFrame<FindmaxFrameParams> nextWorseFrameWithHigherRate = function.journal().frames().stream()
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()))
@ -73,7 +63,7 @@ public class FindmaxAnalyzer implements SimFrameFunctionAnalyzer {
config.setMin_settling_ms(config.min_settling_ms() * 2);
} else {
logger.info("could not divide search space further, stop condition met");
return 0.0d;
return Double.MIN_VALUE;
}
}
double[] point = {newValue};

View File

@ -17,8 +17,9 @@
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 {
public class FindmaxConfig extends SimFrameConfig {
private double sample_time_ms;
private double max_value;
private double base_value;
@ -41,6 +42,11 @@ public class FindmaxConfig {
return base_value;
}
@Override
public double[] initialPoint() {
return new double[]{base_value};
}
public double min_value() {
return min_value;
}

View File

@ -23,7 +23,7 @@ import io.nosqlbench.scenarios.simframe.capture.SimFrameCapture;
import io.nosqlbench.scenarios.simframe.capture.SimFrameJournal;
import io.nosqlbench.scenarios.simframe.planning.SimFrameFunction;
public class FindmaxFrameFunction implements SimFrameFunction {
public class FindmaxFrameFunction implements SimFrameFunction<FindmaxFrameParams> {
private final Activity flywheel;
private final SimFrameCapture capture;
@ -69,7 +69,8 @@ public class FindmaxFrameFunction implements SimFrameFunction {
return journal.last().value();
}
public SimFrameJournal<FindmaxFrameParams> journal() {
@Override
public SimFrameJournal<FindmaxFrameParams> getJournal() {
return journal;
}
}

View File

@ -23,7 +23,7 @@ import io.nosqlbench.scenarios.simframe.capture.SimFrameCapture;
import io.nosqlbench.scenarios.simframe.capture.SimFrameJournal;
import io.nosqlbench.scenarios.simframe.planning.SimFrameFunction;
public class OptimoFrameFunction implements SimFrameFunction {
public class OptimoFrameFunction implements SimFrameFunction<OptimoFrameParams> {
private final Activity flywheel;
private final SimFrameCapture capture;
@ -65,4 +65,9 @@ public class OptimoFrameFunction implements SimFrameFunction {
}
return journal.last().value();
}
@Override
public SimFrameJournal<OptimoFrameParams> getJournal() {
return journal;
}
}

View File

@ -0,0 +1,24 @@
/*
* Copyright (c) 2024 nosqlbench
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package io.nosqlbench.scenarios.simframe.planning;
public abstract class SimFrameConfig {
public abstract double base_value();
public abstract double[] initialPoint();
}

View File

@ -16,10 +16,13 @@
package io.nosqlbench.scenarios.simframe.planning;
import io.nosqlbench.engine.core.lifecycle.scenario.container.InvokableResult;
import io.nosqlbench.scenarios.simframe.capture.SimFrameJournal;
import org.apache.commons.math4.legacy.analysis.MultivariateFunction;
public interface SimFrameFunction extends MultivariateFunction {
public interface SimFrameFunction<P extends InvokableResult> extends MultivariateFunction {
@Override
double value(double[] point);
SimFrameJournal<P> getJournal();
}

View File

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