Search in sources :

Example 1 with OptimizationResultSet

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

the class ParameterEstimationRunTaskPanel method solve.

private void solve() throws NumberFormatException {
    CopasiOptimizationMethod com = optimizationMethodParameterTableModel.copasiOptimizationMethod;
    OptimizationSolverSpec optSolverSpec = new OptimizationSolverSpec(com);
    // get num runs for stochstic opt mehtods before starting solving...
    if (com.getType().isStochasticMethod()) {
        int numRuns = Integer.parseInt(((String) numberOfRunComboBox.getSelectedItem()));
        optSolverSpec.setNumOfRuns(numRuns);
    }
    parameterEstimationTask.setOptimizationSolverSpec(optSolverSpec);
    parameterEstimationTask.getModelOptimizationSpec().setComputeProfileDistributions(computeProfileDistributionsCheckBox.isSelected());
    optSolverCallbacks.reset();
    Double endValue = com.getEndValue();
    optSolverCallbacks.setEvaluation(0, Double.POSITIVE_INFINITY, 0, endValue, 0);
    // (endValue != null);
    getRunStatusDialog().showProgressBar(com);
    Collection<AsynchClientTask> taskList = ClientRequestManager.updateMath(this, parameterEstimationTask.getSimulationContext(), false, NetworkGenerationRequirements.ComputeFullStandardTimeout);
    AsynchClientTask task1 = new AsynchClientTask("checking issues", AsynchClientTask.TASKTYPE_NONSWING_BLOCKING) {

        @Override
        public void run(Hashtable<String, Object> hashTable) throws Exception {
            StringBuffer issueText = new StringBuffer();
            java.util.Vector<Issue> issueList = new java.util.Vector<Issue>();
            IssueContext issueContext = new IssueContext();
            parameterEstimationTask.gatherIssues(issueContext, issueList);
            boolean bFailed = false;
            for (int i = 0; i < issueList.size(); i++) {
                Issue issue = (Issue) issueList.elementAt(i);
                issueText.append(issue.getMessage() + "\n");
                if (issue.getSeverity() == Issue.SEVERITY_ERROR) {
                    bFailed = true;
                    break;
                }
            }
            if (bFailed) {
                throw new OptimizationException(issueText.toString());
            }
            parameterEstimationTask.refreshMappings();
        }
    };
    taskList.add(task1);
    AsynchClientTask task2 = new AsynchClientTask("solving", AsynchClientTask.TASKTYPE_NONSWING_BLOCKING) {

        @Override
        public void run(Hashtable<String, Object> hashTable) throws Exception {
            MathMappingCallback mathMappingCallback = new MathMappingCallbackTaskAdapter(getClientTaskStatusSupport());
            // OptimizationResultSet optResultSet = CopasiOptimizationSolver.solveLocalPython(new ParameterEstimationTaskSimulatorIDA(),parameterEstimationTask,optSolverCallbacks,mathMappingCallback);
            OptimizationResultSet optResultSet = CopasiOptimizationSolver.solveRemoteApi(new ParameterEstimationTaskSimulatorIDA(), parameterEstimationTask, optSolverCallbacks, mathMappingCallback);
            hashTable.put(ORS_KEY, optResultSet);
        }
    };
    taskList.add(task2);
    AsynchClientTask setResultTask = new AsynchClientTask("set results", AsynchClientTask.TASKTYPE_SWING_BLOCKING) {

        @Override
        public void run(Hashtable<String, Object> hashTable) throws Exception {
            OptimizationResultSet optResultSet = (OptimizationResultSet) hashTable.get(ORS_KEY);
            parameterEstimationTask.setOptimizationResultSet(optResultSet);
        }
    };
    taskList.add(setResultTask);
    ClientTaskDispatcher.dispatch(this, new Hashtable<String, Object>(), taskList.toArray(new AsynchClientTask[taskList.size()]), getRunStatusDialog(), true, true, true, null, false);
}
Also used : OptimizationException(cbit.vcell.opt.OptimizationException) AsynchClientTask(cbit.vcell.client.task.AsynchClientTask) Issue(org.vcell.util.Issue) MathMappingCallbackTaskAdapter(cbit.vcell.mapping.MathMappingCallbackTaskAdapter) MathMappingCallback(cbit.vcell.mapping.SimulationContext.MathMappingCallback) Hashtable(java.util.Hashtable) OptimizationResultSet(cbit.vcell.opt.OptimizationResultSet) CopasiOptimizationMethod(cbit.vcell.opt.CopasiOptimizationMethod) IssueContext(org.vcell.util.IssueContext) OptimizationSolverSpec(cbit.vcell.opt.OptimizationSolverSpec) EventObject(java.util.EventObject) ParameterEstimationTaskSimulatorIDA(org.vcell.optimization.ParameterEstimationTaskSimulatorIDA)

Example 2 with OptimizationResultSet

use of cbit.vcell.opt.OptimizationResultSet 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 3 with OptimizationResultSet

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

the class LocalOptimizationService method solve.

/**
 * solve method comment.
 */
public OptimizationResultSet solve(OptimizationSpec optSpec, OptimizationSolverSpec optSolverSpec, OptSolverCallbacks optSolverCallbacks) {
    OptimizationSolver optSolver = null;
    if (optSolverSpec.getSolverType().equals(OptimizationSolverSpec.SOLVERTYPE_POWELL)) {
        optSolver = new PowellOptimizationSolver();
    // }else if (optSolverSpec.getSolverType().equals(OptimizationSolverSpec.SOLVERTYPE_CONJUGATE_GRADIENT)){
    // optSolver = new ConjugateGradientOptimizationSolver();
    } else {
        throw new RuntimeException("unsupported solver type '" + optSolverSpec.getSolverType() + "'");
    }
    OptimizationResultSet optResultSet = null;
    try {
        optResultSet = optSolver.solve(optSpec, optSolverSpec, optSolverCallbacks);
    } catch (IOException e) {
        e.printStackTrace(System.out);
        throw new RuntimeException(e.getMessage());
    } catch (ExpressionException e) {
        e.printStackTrace(System.out);
        throw new RuntimeException(e.getMessage());
    } catch (OptimizationException e) {
        e.printStackTrace(System.out);
        throw new RuntimeException(e.getMessage());
    }
    return optResultSet;
}
Also used : OptimizationException(cbit.vcell.opt.OptimizationException) PowellOptimizationSolver(cbit.vcell.opt.solvers.PowellOptimizationSolver) OptimizationSolver(cbit.vcell.opt.solvers.OptimizationSolver) PowellOptimizationSolver(cbit.vcell.opt.solvers.PowellOptimizationSolver) OptimizationResultSet(cbit.vcell.opt.OptimizationResultSet) IOException(java.io.IOException) ExpressionException(cbit.vcell.parser.ExpressionException)

Example 4 with OptimizationResultSet

use of cbit.vcell.opt.OptimizationResultSet 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 5 with OptimizationResultSet

use of cbit.vcell.opt.OptimizationResultSet 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)

Aggregations

OptimizationResultSet (cbit.vcell.opt.OptimizationResultSet)20 OptSolverResultSet (cbit.vcell.opt.OptSolverResultSet)12 OptimizationSolverSpec (cbit.vcell.opt.OptimizationSolverSpec)10 PowellOptimizationSolver (cbit.vcell.opt.solvers.PowellOptimizationSolver)8 OptimizationSpec (cbit.vcell.opt.OptimizationSpec)7 DefaultOptSolverCallbacks (org.vcell.optimization.DefaultOptSolverCallbacks)7 OptSolverCallbacks (org.vcell.optimization.OptSolverCallbacks)7 OptimizationException (cbit.vcell.opt.OptimizationException)6 Parameter (cbit.vcell.opt.Parameter)6 SimpleReferenceData (cbit.vcell.opt.SimpleReferenceData)5 ExplicitFitObjectiveFunction (cbit.vcell.opt.ExplicitFitObjectiveFunction)4 OptRunResultSet (cbit.vcell.opt.OptSolverResultSet.OptRunResultSet)4 Expression (cbit.vcell.parser.Expression)4 CopasiOptimizationMethod (cbit.vcell.opt.CopasiOptimizationMethod)3 OptimizationStatus (cbit.vcell.opt.OptimizationStatus)3 DefaultScalarFunction (cbit.function.DefaultScalarFunction)2 RowColumnResultSet (cbit.vcell.math.RowColumnResultSet)2 Parameter (cbit.vcell.model.Parameter)2 CopasiOptimizationParameter (cbit.vcell.opt.CopasiOptimizationParameter)2 ImplicitObjectiveFunction (cbit.vcell.opt.ImplicitObjectiveFunction)2