Search in sources :

Example 6 with OptimizationSpec

use of cbit.vcell.opt.OptimizationSpec in project vcell by virtualcell.

the class CurveFitting method solveSpatial.

public static OptimizationResultSet solveSpatial(Simulation sim, Parameter[] parameters, SpatialReferenceData refData, File dataDir, FieldDataIdentifierSpec[] fieldDataIDSs) throws ExpressionException, OptimizationException, IOException {
    // choose optimization solver, currently we have Powell and CFSQP
    PowellOptimizationSolver optService = new PowellOptimizationSolver();
    OptimizationSpec optSpec = new OptimizationSpec();
    // send to optimization service
    optSpec.setObjectiveFunction(new PdeObjectiveFunction(sim.getMathDescription(), refData, dataDir, fieldDataIDSs));
    double[] parameterValues = new double[parameters.length];
    for (int i = 0; i < parameters.length; i++) {
        parameterValues[i] = parameters[i].getInitialGuess();
        System.out.println("initial " + parameters[i].getName() + " = " + parameterValues[i] + ";\tLB = " + parameters[i].getLowerBound() + ";\tUB = " + parameters[i].getUpperBound());
    }
    // get the initial guess to send it to the f() function. ....
    for (int i = 0; i < parameters.length; i++) {
        optSpec.addParameter(parameters[i]);
    }
    // Parameters in OptimizationSolverSpec are solver type and objective function change tolerance.
    OptimizationSolverSpec optSolverSpec = new OptimizationSolverSpec(OptimizationSolverSpec.SOLVERTYPE_POWELL, 0.000001);
    OptSolverCallbacks optSolverCallbacks = new DefaultOptSolverCallbacks();
    OptimizationResultSet optResultSet = null;
    optResultSet = optService.solve(optSpec, optSolverSpec, optSolverCallbacks);
    OptSolverResultSet optSolverResultSet = optResultSet.getOptSolverResultSet();
    String[] paramNames = optSolverResultSet.getParameterNames();
    double[] paramValues = optSolverResultSet.getBestEstimates();
    for (int i = 0; i < paramNames.length; i++) {
        System.out.println("finally:   " + paramNames[i] + " = " + paramValues[i]);
    }
    return optResultSet;
}
Also used : PowellOptimizationSolver(cbit.vcell.opt.solvers.PowellOptimizationSolver) DefaultOptSolverCallbacks(org.vcell.optimization.DefaultOptSolverCallbacks) OptSolverCallbacks(org.vcell.optimization.OptSolverCallbacks) OptimizationResultSet(cbit.vcell.opt.OptimizationResultSet) PdeObjectiveFunction(cbit.vcell.opt.PdeObjectiveFunction) DefaultOptSolverCallbacks(org.vcell.optimization.DefaultOptSolverCallbacks) OptimizationSolverSpec(cbit.vcell.opt.OptimizationSolverSpec) OptimizationSpec(cbit.vcell.opt.OptimizationSpec) OptSolverResultSet(cbit.vcell.opt.OptSolverResultSet)

Example 7 with OptimizationSpec

use of cbit.vcell.opt.OptimizationSpec in project vcell by virtualcell.

the class CurveFitting method solve.

public static OptimizationResultSet solve(ExplicitFitObjectiveFunction.ExpressionDataPair[] expDataPairs, Parameter[] parameters, double[] time, double[][] data, String[] colNames, Weights weights) throws ExpressionException, OptimizationException, IOException {
    // choose optimization solver, currently we have Powell and CFSQP
    PowellOptimizationSolver optService = new PowellOptimizationSolver();
    OptimizationSpec optSpec = new OptimizationSpec();
    // create simple reference data, columns: t + dataColumns
    double[][] realData = new double[1 + data.length][time.length];
    for (// add time column
    int i = 0; // add time column
    i < time.length; // add time column
    i++) {
        realData[0][i] = time[i];
    }
    for (// add each data column to realData
    int i = 0; // add each data column to realData
    i < data.length; // add each data column to realData
    i++) {
        for (int j = 0; j < time.length; j++) {
            realData[1 + i][j] = data[i][j];
        }
    }
    SimpleReferenceData refData = new SimpleReferenceData(colNames, weights, realData);
    // send to optimization service
    optSpec.setObjectiveFunction(new ExplicitFitObjectiveFunction(expDataPairs, refData));
    double[] parameterValues = new double[parameters.length];
    for (int i = 0; i < parameters.length; i++) {
        parameterValues[i] = parameters[i].getInitialGuess();
        System.out.println("initial " + parameters[i].getName() + " = " + parameterValues[i] + ";\tLB = " + parameters[i].getLowerBound() + ";\tUB = " + parameters[i].getUpperBound());
    }
    // get the initial guess to send it to the f() function. ....
    for (int i = 0; i < parameters.length; i++) {
        optSpec.addParameter(parameters[i]);
    }
    // Parameters in OptimizationSolverSpec are solver type and objective function change tolerance.
    OptimizationSolverSpec optSolverSpec = new OptimizationSolverSpec(OptimizationSolverSpec.SOLVERTYPE_POWELL, 0.000001);
    OptSolverCallbacks optSolverCallbacks = new DefaultOptSolverCallbacks();
    OptimizationResultSet optResultSet = null;
    optResultSet = optService.solve(optSpec, optSolverSpec, optSolverCallbacks);
    return optResultSet;
}
Also used : PowellOptimizationSolver(cbit.vcell.opt.solvers.PowellOptimizationSolver) DefaultOptSolverCallbacks(org.vcell.optimization.DefaultOptSolverCallbacks) OptSolverCallbacks(org.vcell.optimization.OptSolverCallbacks) OptimizationResultSet(cbit.vcell.opt.OptimizationResultSet) ExplicitFitObjectiveFunction(cbit.vcell.opt.ExplicitFitObjectiveFunction) DefaultOptSolverCallbacks(org.vcell.optimization.DefaultOptSolverCallbacks) OptimizationSolverSpec(cbit.vcell.opt.OptimizationSolverSpec) OptimizationSpec(cbit.vcell.opt.OptimizationSpec) SimpleReferenceData(cbit.vcell.opt.SimpleReferenceData)

Example 8 with OptimizationSpec

use of cbit.vcell.opt.OptimizationSpec in project vcell by virtualcell.

the class CurveFitting method solve.

public static OptimizationResultSet solve(ExplicitFitObjectiveFunction.ExpressionDataPair[] expDataPairs, Parameter[] parameters, double[] time, double[][] data, String[] colNames, Weights weights) throws ExpressionException, OptimizationException, IOException {
    // choose optimization solver, currently we have Powell
    PowellOptimizationSolver optService = new PowellOptimizationSolver();
    OptimizationSpec optSpec = new OptimizationSpec();
    // create simple reference data, columns: t + dataColumns
    double[][] realData = new double[1 + data.length][time.length];
    for (// add time column
    int i = 0; // add time column
    i < time.length; // add time column
    i++) {
        realData[0][i] = time[i];
    }
    for (// add each data column to realData
    int i = 0; // add each data column to realData
    i < data.length; // add each data column to realData
    i++) {
        for (int j = 0; j < time.length; j++) {
            realData[1 + i][j] = data[i][j];
        }
    }
    SimpleReferenceData refData = new SimpleReferenceData(colNames, weights, realData);
    // send to optimization service
    optSpec.setObjectiveFunction(new ExplicitFitObjectiveFunction(expDataPairs, refData));
    double[] parameterValues = new double[parameters.length];
    for (int i = 0; i < parameters.length; i++) {
        parameterValues[i] = parameters[i].getInitialGuess();
        System.out.println("initial " + parameters[i].getName() + " = " + parameterValues[i] + ";\tLB = " + parameters[i].getLowerBound() + ";\tUB = " + parameters[i].getUpperBound());
    }
    // get the initial guess to send it to the f() function. ....
    for (int i = 0; i < parameters.length; i++) {
        optSpec.addParameter(parameters[i]);
    }
    // Parameters in OptimizationSolverSpec are solver type and objective function change tolerance.
    OptimizationSolverSpec optSolverSpec = new OptimizationSolverSpec(OptimizationSolverSpec.SOLVERTYPE_POWELL, 0.000001);
    OptSolverCallbacks optSolverCallbacks = new DefaultOptSolverCallbacks();
    OptimizationResultSet optResultSet = null;
    optResultSet = optService.solve(optSpec, optSolverSpec, optSolverCallbacks);
    return optResultSet;
}
Also used : PowellOptimizationSolver(cbit.vcell.opt.solvers.PowellOptimizationSolver) DefaultOptSolverCallbacks(org.vcell.optimization.DefaultOptSolverCallbacks) OptSolverCallbacks(org.vcell.optimization.OptSolverCallbacks) OptimizationResultSet(cbit.vcell.opt.OptimizationResultSet) ExplicitFitObjectiveFunction(cbit.vcell.opt.ExplicitFitObjectiveFunction) DefaultOptSolverCallbacks(org.vcell.optimization.DefaultOptSolverCallbacks) OptimizationSolverSpec(cbit.vcell.opt.OptimizationSolverSpec) OptimizationSpec(cbit.vcell.opt.OptimizationSpec) SimpleReferenceData(cbit.vcell.opt.SimpleReferenceData)

Example 9 with OptimizationSpec

use of cbit.vcell.opt.OptimizationSpec in project vcell by virtualcell.

the class ParameterEstimationRunTaskPanel method saveSolutionAsNewSimulation.

private void saveSolutionAsNewSimulation() {
    try {
        OptimizationSpec optSpec = parameterEstimationTask.getModelOptimizationMapping().getOptimizationSpec();
        if (optSpec == null) {
            throw new RuntimeException("optimization not yet performed");
        }
        // 
        // add new simulation to the Application (other bookkeeping required?)
        // 
        String newSimName = DialogUtils.showInputDialog0(this, "Enter name for new simulation", "Fitted");
        newSimName = (newSimName == null ? null : newSimName.trim());
        if (newSimName == null || newSimName.length() == 0) {
            throw new Exception("New Simulation name cannot be 0 characters");
        }
        SimulationContext simContext = parameterEstimationTask.getModelOptimizationSpec().getSimulationContext();
        MathMappingCallback dummyCallback = new MathMappingCallback() {

            @Override
            public void setProgressFraction(float fractionDone) {
            }

            @Override
            public void setMessage(String message) {
            }

            @Override
            public boolean isInterrupted() {
                return false;
            }
        };
        Simulation newSim = simContext.addNewSimulation(newSimName, dummyCallback, NetworkGenerationRequirements.ComputeFullStandardTimeout);
        parameterEstimationTask.getModelOptimizationMapping().applySolutionToMathOverrides(newSim, parameterEstimationTask.getOptimizationResultSet());
        DialogUtils.showInfoDialog(this, "created simulation \"" + newSim.getName() + "\"");
    } catch (UtilCancelException e) {
    // ignore, user cancelled operation
    } catch (Exception e) {
        e.printStackTrace(System.out);
        DialogUtils.showErrorDialog(this, e.getMessage(), e);
    }
}
Also used : UtilCancelException(org.vcell.util.UtilCancelException) MathMappingCallback(cbit.vcell.mapping.SimulationContext.MathMappingCallback) Simulation(cbit.vcell.solver.Simulation) ModelOptimizationSpec(cbit.vcell.modelopt.ModelOptimizationSpec) OptimizationSpec(cbit.vcell.opt.OptimizationSpec) SimulationContext(cbit.vcell.mapping.SimulationContext) OptimizationException(cbit.vcell.opt.OptimizationException) UtilCancelException(org.vcell.util.UtilCancelException) ExpressionException(cbit.vcell.parser.ExpressionException)

Example 10 with OptimizationSpec

use of cbit.vcell.opt.OptimizationSpec in project vcell by virtualcell.

the class OptXmlWriter method getCoapsiOptProblemDescriptionXML.

public static Element getCoapsiOptProblemDescriptionXML(ParameterEstimationTask parameterEstimationTask, MathMappingCallback mathMappingCallback) throws IOException, XmlParseException, ExpressionException, SBMLException, XMLStreamException {
    OptimizationSpec optimizationSpec = parameterEstimationTask.getModelOptimizationMapping().getOptimizationSpec();
    Element optProblemDescriptionElement = new Element(OptXmlTags.OptProblemDescription_Tag);
    File modelSbmlFile = File.createTempFile("mathModel", ".xml", ResourceUtil.getVcellHome());
    SimulationContext simulationContext = parameterEstimationTask.getSimulationContext();
    simulationContext.refreshMathDescription(mathMappingCallback, NetworkGenerationRequirements.ComputeFullStandardTimeout);
    MathModel vcellMathModel = new MathModel(null);
    vcellMathModel.setMathDescription(simulationContext.getMathDescription());
    // get math model string
    String sbmlString = MathModel_SBMLExporter.getSBMLString(vcellMathModel, 2, 4);
    // String modelSbml = XmlHelper.exportSBML(simulationContext.getBioModel(), 3, 1, 0, false, simulationContext, null);
    XmlUtil.writeXMLStringToFile(sbmlString, modelSbmlFile.getAbsolutePath(), true);
    Element element = new Element(OptXmlTags.MathModelSbmlFile_Tag);
    element.addContent(modelSbmlFile.getAbsolutePath());
    optProblemDescriptionElement.addContent(element);
    if (optimizationSpec.isComputeProfileDistributions()) {
        optProblemDescriptionElement.setAttribute(OptXmlTags.ComputeProfileDistributions_Attr, optimizationSpec.isComputeProfileDistributions() + "");
    }
    optProblemDescriptionElement.addContent(getVCellOptionsXML(parameterEstimationTask.getOptimizationSolverSpec()));
    optProblemDescriptionElement.addContent(getParameterDescriptionXML(optimizationSpec));
    Element dataElement = getCopasiDataXML(parameterEstimationTask);
    optProblemDescriptionElement.addContent(dataElement);
    element = getCopasiOptimizationMethodXML(parameterEstimationTask.getOptimizationSolverSpec().getCopasiOptimizationMethod());
    optProblemDescriptionElement.addContent(element);
    return optProblemDescriptionElement;
}
Also used : MathModel(cbit.vcell.mathmodel.MathModel) Element(org.jdom.Element) OptimizationSpec(cbit.vcell.opt.OptimizationSpec) SimulationContext(cbit.vcell.mapping.SimulationContext) File(java.io.File)

Aggregations

OptimizationSpec (cbit.vcell.opt.OptimizationSpec)11 OptimizationResultSet (cbit.vcell.opt.OptimizationResultSet)7 OptimizationSolverSpec (cbit.vcell.opt.OptimizationSolverSpec)7 PowellOptimizationSolver (cbit.vcell.opt.solvers.PowellOptimizationSolver)7 DefaultOptSolverCallbacks (org.vcell.optimization.DefaultOptSolverCallbacks)7 OptSolverCallbacks (org.vcell.optimization.OptSolverCallbacks)7 SimpleReferenceData (cbit.vcell.opt.SimpleReferenceData)5 SimulationContext (cbit.vcell.mapping.SimulationContext)4 OptSolverResultSet (cbit.vcell.opt.OptSolverResultSet)4 Parameter (cbit.vcell.opt.Parameter)4 ExplicitFitObjectiveFunction (cbit.vcell.opt.ExplicitFitObjectiveFunction)3 DefaultScalarFunction (cbit.function.DefaultScalarFunction)2 MathMappingCallback (cbit.vcell.mapping.SimulationContext.MathMappingCallback)2 MathModel (cbit.vcell.mathmodel.MathModel)2 ImplicitObjectiveFunction (cbit.vcell.opt.ImplicitObjectiveFunction)2 PdeObjectiveFunction (cbit.vcell.opt.PdeObjectiveFunction)2 Expression (cbit.vcell.parser.Expression)2 ExpressionException (cbit.vcell.parser.ExpressionException)2 File (java.io.File)2 MappingException (cbit.vcell.mapping.MappingException)1