Search in sources :

Example 1 with OptSolverCallbacks

use of org.vcell.optimization.OptSolverCallbacks in project vcell by virtualcell.

the class OptContextSolver method estimate.

public static double estimate(OptContext optContext, Parameter[] inParams, String[] outParaNames, double[] outParaVals) throws Exception {
    // long startTime =System.currentTimeMillis();
    // create optimization solver
    PowellOptimizationSolver optSolver = new PowellOptimizationSolver();
    // create optimization spec
    OptimizationSpec optSpec = new OptimizationSpec();
    DefaultScalarFunction scalarFunc = new OptContextObjectiveFunction(optContext);
    optSpec.setObjectiveFunction(new ImplicitObjectiveFunction(scalarFunc));
    // create solver spec
    OptimizationSolverSpec optSolverSpec = new OptimizationSolverSpec(OptimizationSolverSpec.SOLVERTYPE_POWELL, FTOL);
    // create solver call back
    OptSolverCallbacks optSolverCallbacks = new DefaultOptSolverCallbacks();
    // create optimization result set
    OptimizationResultSet optResultSet = null;
    for (int i = 0; i < inParams.length; i++) {
        // add parameters
        optSpec.addParameter(inParams[i]);
    }
    optResultSet = optSolver.solve(optSpec, optSolverSpec, optSolverCallbacks);
    OptSolverResultSet optSolverResultSet = optResultSet.getOptSolverResultSet();
    // System.out.println("obj function value:"+optResultSet.getObjectiveFunctionValue());
    // System.out.println("");
    // copy results to output parameters
    String[] names = optSolverResultSet.getParameterNames();
    double[] values = optSolverResultSet.getBestEstimates();
    for (int i = 0; i < names.length; i++) {
        outParaNames[i] = names[i];
        outParaVals[i] = values[i];
    }
    // System.out.println("total: " + ( endTime - startTime) );
    return optSolverResultSet.getLeastObjectiveFunctionValue();
}
Also used : PowellOptimizationSolver(cbit.vcell.opt.solvers.PowellOptimizationSolver) DefaultOptSolverCallbacks(org.vcell.optimization.DefaultOptSolverCallbacks) OptSolverCallbacks(org.vcell.optimization.OptSolverCallbacks) OptimizationResultSet(cbit.vcell.opt.OptimizationResultSet) OptSolverResultSet(cbit.vcell.opt.OptSolverResultSet) DefaultScalarFunction(cbit.function.DefaultScalarFunction) DefaultOptSolverCallbacks(org.vcell.optimization.DefaultOptSolverCallbacks) ImplicitObjectiveFunction(cbit.vcell.opt.ImplicitObjectiveFunction) OptimizationSolverSpec(cbit.vcell.opt.OptimizationSolverSpec) OptimizationSpec(cbit.vcell.opt.OptimizationSpec)

Example 2 with OptSolverCallbacks

use of org.vcell.optimization.OptSolverCallbacks in project vcell by virtualcell.

the class FRAPOptimizationUtils method estimate.

// estimate best parameters and return the least error
public static double estimate(FRAPOptData argOptData, Parameter[] inParams, String[] outParaNames, double[] outParaVals, boolean[] eoi) throws Exception {
    /*PowellSolver  solver = new PowellSolver();
		LookupTableObjectiveFunction func = new LookupTableObjectiveFunction(argOptData);
		
		//best point found. we have only one dimension here.
		double[] p = new double[1];
		p[0] = iniDiffGuess;
		//current direction set
		double[][] xi = new double[1][1];
		for (int i=0;i<1;i++)
		{
		   for (int j=0;j<1;j++)
		   {
		       xi[i][j]=(i == j ? 1.0 : 0.0);
		   }
		}
		//run powell with initial guess, initial direction set and objective function
		double minError = solver.powell(1, p, xi, FTOL, func);
		parameters[FRAPOptData.idxOptDiffRate] = p[0];
		parameters[FRAPOptData.idxMinError] = minError;*/
    // long startTime =System.currentTimeMillis();
    // create optimization solver
    PowellOptimizationSolver optSolver = new PowellOptimizationSolver();
    // create optimization spec
    OptimizationSpec optSpec = new OptimizationSpec();
    // add opt function
    DefaultScalarFunction scalarFunc = new LookupTableObjectiveFunction(argOptData, eoi);
    optSpec.setObjectiveFunction(new ImplicitObjectiveFunction(scalarFunc));
    // create solver spec
    OptimizationSolverSpec optSolverSpec = new OptimizationSolverSpec(OptimizationSolverSpec.SOLVERTYPE_POWELL, FRAPOptimizationUtils.FTOL);
    // create solver call back
    OptSolverCallbacks optSolverCallbacks = new DefaultOptSolverCallbacks();
    // create optimization result set
    OptimizationResultSet optResultSet = null;
    for (int i = 0; i < inParams.length; i++) {
        // add parameters
        optSpec.addParameter(inParams[i]);
    }
    optResultSet = optSolver.solve(optSpec, optSolverSpec, optSolverCallbacks);
    OptSolverResultSet optSolverResultSet = optResultSet.getOptSolverResultSet();
    // if the parameters are 5, we have to go over again to see if we get the best answer.
    if (// 5 parameters
    inParams.length == 5) {
        OptimizationSpec optSpec2 = new OptimizationSpec();
        optSpec2.setObjectiveFunction(new ImplicitObjectiveFunction(scalarFunc));
        Parameter[] inParamsFromResult = generateInParamSet(inParams, optSolverResultSet.getBestEstimates());
        for (int i = 0; i < inParamsFromResult.length; i++) {
            // add parameters
            optSpec2.addParameter(inParamsFromResult[i]);
        }
        OptimizationResultSet tempOptResultSet = optSolver.solve(optSpec2, optSolverSpec, optSolverCallbacks);
        OptSolverResultSet tempOptSolverResultSet = tempOptResultSet.getOptSolverResultSet();
        if (optSolverResultSet.getLeastObjectiveFunctionValue() > tempOptSolverResultSet.getLeastObjectiveFunctionValue()) {
            optSolverResultSet = tempOptSolverResultSet;
        }
    }
    // System.out.println("obj function value:"+optResultSet.getObjectiveFunctionValue());
    // System.out.println("");
    // copy results to output parameters
    String[] names = optSolverResultSet.getParameterNames();
    double[] values = optSolverResultSet.getBestEstimates();
    for (int i = 0; i < names.length; i++) {
        outParaNames[i] = names[i];
        outParaVals[i] = values[i];
    }
    // System.out.println("total: " + ( endTime - startTime) );
    return optSolverResultSet.getLeastObjectiveFunctionValue();
}
Also used : PowellOptimizationSolver(cbit.vcell.opt.solvers.PowellOptimizationSolver) DefaultOptSolverCallbacks(org.vcell.optimization.DefaultOptSolverCallbacks) OptSolverCallbacks(org.vcell.optimization.OptSolverCallbacks) OptimizationResultSet(cbit.vcell.opt.OptimizationResultSet) OptSolverResultSet(cbit.vcell.opt.OptSolverResultSet) DefaultScalarFunction(cbit.function.DefaultScalarFunction) DefaultOptSolverCallbacks(org.vcell.optimization.DefaultOptSolverCallbacks) ImplicitObjectiveFunction(cbit.vcell.opt.ImplicitObjectiveFunction) Parameter(cbit.vcell.opt.Parameter) OptimizationSolverSpec(cbit.vcell.opt.OptimizationSolverSpec) OptimizationSpec(cbit.vcell.opt.OptimizationSpec)

Example 3 with OptSolverCallbacks

use of org.vcell.optimization.OptSolverCallbacks in project vcell by virtualcell.

the class ReactionDominantTest method solve.

private OptimizationResultSet solve() throws ExpressionException, IOException {
    Expression Fbleached_bleachFastExp = new Expression(Fbleached_bleachFast);
    Expression OmegaExp = new Expression(strOmega);
    Expression Omega_cExp = new Expression(strOmega_c);
    Expression WExp = new Expression(strW);
    Fbleached_bleachFastExp.substituteInPlace(OmegaExp, new Expression(Omega));
    Fbleached_bleachFastExp.substituteInPlace(Omega_cExp, new Expression(Omega_c));
    Fbleached_bleachFastExp.substituteInPlace(WExp, new Expression(W));
    Parameter[] parameters = new Parameter[] { para_alpha, para_Kon_star, para_Koff };
    // Expression Fbleached_bleachFast = Fbleached_bleachFastExp.flatten();
    // choose optimization solver, currently we have Powell and CFSQP
    PowellOptimizationSolver optService = new PowellOptimizationSolver();
    OptimizationSpec optSpec = new OptimizationSpec();
    // create simple reference data
    double[][] realData = new double[2][t.length];
    for (int i = 0; i < t.length; i++) {
        realData[0][i] = t[i];
        realData[1][i] = data_bleached[i];
    }
    String[] colNames = new String[] { "t", "intensity" };
    double[] weights = new double[] { 1.0, 1.0 };
    SimpleReferenceData refData = new SimpleReferenceData(colNames, weights, realData);
    // send to optimization service
    ExplicitFitObjectiveFunction.ExpressionDataPair oneExpDataPair = new ExplicitFitObjectiveFunction.ExpressionDataPair(Fbleached_bleachFastExp.flatten(), 1);
    ExplicitFitObjectiveFunction.ExpressionDataPair[] expDataPairs = new ExplicitFitObjectiveFunction.ExpressionDataPair[] { oneExpDataPair };
    optSpec.setObjectiveFunction(new ExplicitFitObjectiveFunction(expDataPairs, refData));
    optSpec.setComputeProfileDistributions(true);
    // 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);
    String[] paramNames = optResultSet.getOptSolverResultSet().getParameterNames();
    ArrayList<ProfileDistribution> profileDistributionList = optResultSet.getOptSolverResultSet().getProfileDistributionList();
    for (int i = 0; i < profileDistributionList.size(); i++) {
        outputProfileLikelihood(profileDistributionList.get(i), i, paramNames[i], new File(fileDir));
    }
    return optResultSet;
}
Also used : PowellOptimizationSolver(cbit.vcell.opt.solvers.PowellOptimizationSolver) ProfileDistribution(cbit.vcell.opt.OptSolverResultSet.ProfileDistribution) DefaultOptSolverCallbacks(org.vcell.optimization.DefaultOptSolverCallbacks) OptSolverCallbacks(org.vcell.optimization.OptSolverCallbacks) OptimizationResultSet(cbit.vcell.opt.OptimizationResultSet) ExplicitFitObjectiveFunction(cbit.vcell.opt.ExplicitFitObjectiveFunction) SimpleReferenceData(cbit.vcell.opt.SimpleReferenceData) Expression(cbit.vcell.parser.Expression) DefaultOptSolverCallbacks(org.vcell.optimization.DefaultOptSolverCallbacks) Parameter(cbit.vcell.opt.Parameter) OptimizationSolverSpec(cbit.vcell.opt.OptimizationSolverSpec) OptimizationSpec(cbit.vcell.opt.OptimizationSpec) File(java.io.File)

Example 4 with OptSolverCallbacks

use of org.vcell.optimization.OptSolverCallbacks 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
    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 5 with OptSolverCallbacks

use of org.vcell.optimization.OptSolverCallbacks 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)

Aggregations

OptimizationResultSet (cbit.vcell.opt.OptimizationResultSet)7 OptimizationSolverSpec (cbit.vcell.opt.OptimizationSolverSpec)7 OptimizationSpec (cbit.vcell.opt.OptimizationSpec)7 PowellOptimizationSolver (cbit.vcell.opt.solvers.PowellOptimizationSolver)7 DefaultOptSolverCallbacks (org.vcell.optimization.DefaultOptSolverCallbacks)7 OptSolverCallbacks (org.vcell.optimization.OptSolverCallbacks)7 OptSolverResultSet (cbit.vcell.opt.OptSolverResultSet)4 ExplicitFitObjectiveFunction (cbit.vcell.opt.ExplicitFitObjectiveFunction)3 SimpleReferenceData (cbit.vcell.opt.SimpleReferenceData)3 DefaultScalarFunction (cbit.function.DefaultScalarFunction)2 ImplicitObjectiveFunction (cbit.vcell.opt.ImplicitObjectiveFunction)2 Parameter (cbit.vcell.opt.Parameter)2 PdeObjectiveFunction (cbit.vcell.opt.PdeObjectiveFunction)2 ProfileDistribution (cbit.vcell.opt.OptSolverResultSet.ProfileDistribution)1 Expression (cbit.vcell.parser.Expression)1 File (java.io.File)1