All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.engineblock.extensions.optimizers.MultivariateObjectScript Maven / Gradle / Ivy

package io.engineblock.extensions.optimizers;

import jdk.nashorn.api.scripting.ScriptObjectMirror;
import org.apache.commons.math3.analysis.MultivariateFunction;
import org.slf4j.Logger;

import java.security.InvalidParameterException;
import java.util.HashMap;
import java.util.Map;

public class MultivariateObjectScript implements MultivariateFunction {
    private final ScriptObjectMirror script;
    private final MVParams params;
    private Logger logger;

    public MultivariateObjectScript(Logger logger, MVParams params, ScriptObjectMirror script) {
        this.logger = logger;
        this.script = script;
        this.params = params;
    }

    @Override
    public double value(double[] doubles) {
        if (doubles.length != params.size()) {
            throw new InvalidParameterException("Expected " + params.size() + " doubles, not " + doubles.length);
        }

        Map map = new HashMap<>();

        for (int i = 0; i < doubles.length; i++) {
            map.put(params.get(i).name, doubles[i]);
        }

        Object object = ScriptObjectMirror.wrap(map, null);
        Object result = null;
        result = this.script.call(script,object);
        return Double.valueOf(result.toString());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy