use of cbit.vcell.opt.OptimizationSpec 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 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
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 cbit.vcell.opt.OptimizationSpec 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 cbit.vcell.opt.OptimizationSpec 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 cbit.vcell.opt.OptimizationSpec in project vcell by virtualcell.
the class ModelOptimizationMapping method computeOptimizationSpec.
/**
* Insert the method's description here.
* Creation date: (8/22/2005 9:26:52 AM)
* @return cbit.vcell.opt.OptimizationSpec
* @param modelOptimizationSpec cbit.vcell.modelopt.ModelOptimizationSpec
*/
MathSymbolMapping computeOptimizationSpec() throws MathException, MappingException {
if (getModelOptimizationSpec().getReferenceData() == null) {
System.out.println("no referenced data defined");
return null;
}
OptimizationSpec optSpec = new OptimizationSpec();
optSpec.setComputeProfileDistributions(modelOptimizationSpec.isComputeProfileDistributions());
parameterMappings = null;
//
// get original MathDescription (later to be substituted for local/global parameters).
//
SimulationContext simContext = modelOptimizationSpec.getSimulationContext();
MathMapping mathMapping = simContext.createNewMathMapping();
MathDescription origMathDesc = null;
mathSymbolMapping = null;
try {
origMathDesc = mathMapping.getMathDescription();
mathSymbolMapping = mathMapping.getMathSymbolMapping();
} catch (MatrixException e) {
e.printStackTrace(System.out);
throw new MappingException(e.getMessage());
} catch (ModelException e) {
e.printStackTrace(System.out);
throw new MappingException(e.getMessage());
} catch (ExpressionException e) {
e.printStackTrace(System.out);
throw new MathException(e.getMessage());
}
//
// create objective function (mathDesc and data)
//
ReferenceData referenceData = getRemappedReferenceData(mathMapping);
if (referenceData == null) {
throw new RuntimeException("no referenced data defined");
}
//
// get parameter mappings
//
ParameterMappingSpec[] parameterMappingSpecs = modelOptimizationSpec.getParameterMappingSpecs();
Vector<ParameterMapping> parameterMappingList = new Vector<ParameterMapping>();
Variable[] allVars = (Variable[]) BeanUtils.getArray(origMathDesc.getVariables(), Variable.class);
for (int i = 0; i < parameterMappingSpecs.length; i++) {
cbit.vcell.model.Parameter modelParameter = parameterMappingSpecs[i].getModelParameter();
String mathSymbol = null;
Variable mathVariable = null;
if (mathSymbolMapping != null) {
Variable variable = mathSymbolMapping.getVariable(modelParameter);
if (variable != null) {
mathSymbol = variable.getName();
}
if (mathSymbol != null) {
mathVariable = origMathDesc.getVariable(mathSymbol);
}
}
if (mathVariable != null) {
if (parameterMappingSpecs[i].isSelected()) {
if (parameterMappingSpecs[i].getHigh() < parameterMappingSpecs[i].getLow()) {
throw new MathException("The lower bound for Parameter '" + parameterMappingSpecs[i].getModelParameter().getName() + "' is greater than its upper bound.");
}
if (parameterMappingSpecs[i].getCurrent() < parameterMappingSpecs[i].getLow()) {
throw new MathException("The initial guess of '" + parameterMappingSpecs[i].getModelParameter().getName() + "' is smaller than its lower bound.");
}
if (parameterMappingSpecs[i].getCurrent() > parameterMappingSpecs[i].getHigh()) {
throw new MathException("The initial guess of '" + parameterMappingSpecs[i].getModelParameter().getName() + "' is greater than its upper bound.");
}
if (parameterMappingSpecs[i].getLow() < 0) {
throw new MathException("The lower bound for Parameter '" + parameterMappingSpecs[i].getModelParameter().getName() + "' is negative. All lower bounds must not be negative.");
}
if (Double.isInfinite(parameterMappingSpecs[i].getLow())) {
throw new MathException("The lower bound for Parameter '" + parameterMappingSpecs[i].getModelParameter().getName() + "' is infinity. Lower bounds must not be infinity.");
}
if (parameterMappingSpecs[i].getHigh() <= 0) {
throw new MathException("The upper bound for Parameter '" + parameterMappingSpecs[i].getModelParameter().getName() + "' is negative. All upper bounds must be positive.");
}
if (Double.isInfinite(parameterMappingSpecs[i].getHigh())) {
throw new MathException("The upper bound for Parameter '" + parameterMappingSpecs[i].getModelParameter().getName() + "' is infinity. Upper bounds must not be infinity.");
}
}
double low = parameterMappingSpecs[i].isSelected() && parameterMappingSpecs[i].getLow() == 0 ? 1e-8 : parameterMappingSpecs[i].getLow();
double high = parameterMappingSpecs[i].getHigh();
double scale = Math.abs(parameterMappingSpecs[i].getCurrent()) < 1.0E-10 ? 1.0 : Math.abs(parameterMappingSpecs[i].getCurrent());
double current = parameterMappingSpecs[i].getCurrent();
low = Math.min(low, current);
high = Math.max(high, current);
Parameter optParameter = new Parameter(mathSymbol, low, high, scale, current);
ParameterMapping parameterMapping = new ParameterMapping(modelParameter, optParameter, mathVariable);
//
if (mathVariable instanceof Constant) {
Constant origConstant = (Constant) mathVariable;
for (int j = 0; j < allVars.length; j++) {
if (allVars[j].equals(origConstant)) {
if (parameterMappingSpecs[i].isSelected()) {
allVars[j] = new ParameterVariable(origConstant.getName());
} else {
allVars[j] = new Constant(origConstant.getName(), new Expression(optParameter.getInitialGuess()));
}
break;
}
}
}
//
if (parameterMappingSpecs[i].isSelected()) {
parameterMappingList.add(parameterMapping);
}
}
}
parameterMappings = (ParameterMapping[]) BeanUtils.getArray(parameterMappingList, ParameterMapping.class);
try {
origMathDesc.setAllVariables(allVars);
} catch (ExpressionBindingException e) {
e.printStackTrace(System.out);
throw new MathException(e.getMessage());
}
//
for (int i = 0; i < parameterMappings.length; i++) {
optSpec.addParameter(parameterMappings[i].getOptParameter());
}
Vector<Issue> issueList = new Vector<Issue>();
IssueContext issueContext = new IssueContext();
optSpec.gatherIssues(issueContext, issueList);
for (int i = 0; i < issueList.size(); i++) {
Issue issue = issueList.elementAt(i);
if (issue.getSeverity() == Issue.SEVERITY_ERROR) {
throw new RuntimeException(issue.getMessage());
}
}
//
//
//
optimizationSpec = optSpec;
return mathSymbolMapping;
}
Aggregations