use of org.vcell.optimization.DefaultOptSolverCallbacks 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();
}
use of org.vcell.optimization.DefaultOptSolverCallbacks 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();
}
use of org.vcell.optimization.DefaultOptSolverCallbacks 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;
}
use of org.vcell.optimization.DefaultOptSolverCallbacks 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;
}
use of org.vcell.optimization.DefaultOptSolverCallbacks in project vcell by virtualcell.
the class AugmentedObjectiveFunctionTest method getExample1.
/**
* Insert the method's description here.
* Creation date: (5/3/2002 2:49:06 PM)
* @return opt.AugmentedObjectiveFunction
*/
public static AugmentedObjectiveFunction getExample1() {
try {
Expression scalarFn_exp = new Expression("pow(x1-2,4)+pow(x1-2*x2,2)");
Expression[] eqConstraints_exps = { new Expression("pow(x1,2)-x2") };
Expression[] ineqConstraints_exps = { new Expression("pow(x1,2)-x2") };
;
String[] ids = { "x1", "x2" };
ScalarFunction scalarFn = new DynamicScalarFunction(scalarFn_exp, ids);
VectorFunction eqConstraints = new DynamicVectorFunction(eqConstraints_exps, ids);
VectorFunction ineqConstraints = new DynamicVectorFunction(ineqConstraints_exps, ids);
AugmentedObjectiveFunction aof = new AugmentedObjectiveFunction(scalarFn, eqConstraints, ineqConstraints, 2.0, 0.1, new DefaultOptSolverCallbacks());
return aof;
} catch (ExpressionException e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}
}
Aggregations