Search in sources :

Example 1 with ImplicitObjectiveFunction

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

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

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

the class OptUtils method getAugmentedObjectiveFunction.

/**
 * Insert the method's description here.
 * Creation date: (8/4/2005 3:26:14 PM)
 * @return function.AugmentedObjectiveFunction
 * @param optSpec cbit.vcell.opt.OptimizationSpec
 * @param power double
 * @param mu double
 */
public static AugmentedObjectiveFunction getAugmentedObjectiveFunction(OptimizationSpec optSpec, double power, double mu, OptSolverCallbacks optSolverCallbacks) throws cbit.vcell.parser.ExpressionException {
    Parameter[] parameters = optSpec.getParameters();
    // 
    // build symbol list
    // 
    String[] origSymbols = optSpec.getParameterNames();
    String[] scaledSymbols = optSpec.getScaledParameterNames();
    double[] scaleFactors = optSpec.getScaleFactors();
    ScalarFunction objFunction = null;
    if (optSpec.getObjectiveFunction() instanceof ExplicitObjectiveFunction) {
        ExplicitObjectiveFunction explicitObjectiveFunction = (ExplicitObjectiveFunction) optSpec.getObjectiveFunction();
        // 
        // build objective function
        // 
        objFunction = new DynamicScalarFunction(explicitObjectiveFunction.getScaledExpression(origSymbols, scaledSymbols, scaleFactors), scaledSymbols);
    } else if (optSpec.getObjectiveFunction() instanceof ImplicitObjectiveFunction) {
        ImplicitObjectiveFunction implicitObjectiveFunction = (ImplicitObjectiveFunction) optSpec.getObjectiveFunction();
        // 
        // build objective function
        // 
        objFunction = implicitObjectiveFunction.getObjectiveFunction();
    } else if (optSpec.getObjectiveFunction() instanceof ExplicitFitObjectiveFunction) {
        ExplicitFitObjectiveFunction explicitFitObjectiveFunction = (ExplicitFitObjectiveFunction) optSpec.getObjectiveFunction();
        objFunction = explicitFitObjectiveFunction.getScalarFunction();
    } else {
        throw new RuntimeException("unsupported objective function type : " + optSpec.getObjectiveFunction().getClass().getName());
    }
    // 
    // build equality and inequality constraints
    // 
    DynamicVectorFunction equalityConstraints = null;
    DynamicVectorFunction inequalityConstraints = null;
    Constraint[] constraints = optSpec.getConstraints();
    Vector equExpList = new Vector();
    Vector inequExpList = new Vector();
    // 
    for (int i = 0; i < constraints.length; i++) {
        ConstraintType type = constraints[i].getConstraintType();
        if (type.equals(ConstraintType.LinearEquality) || type.equals(ConstraintType.NonlinearEquality)) {
            equExpList.add(constraints[i].getExpression());
        } else {
            inequExpList.add(constraints[i].getExpression());
        }
    }
    // 
    for (int i = 0; i < parameters.length; i++) {
        if (!Double.isInfinite(parameters[i].getLowerBound())) {
            inequExpList.add(new Expression("(" + parameters[i].getLowerBound() + " - " + parameters[i].getName() + ")/" + scaleFactors[i]));
        }
        if (!Double.isInfinite(parameters[i].getUpperBound())) {
            inequExpList.add(new Expression("(" + parameters[i].getName() + " - " + parameters[i].getUpperBound() + ")/" + scaleFactors[i]));
        }
    }
    if (equExpList.size() > 0) {
        Expression[] exps = (Expression[]) org.vcell.util.BeanUtils.getArray(equExpList, Expression.class);
        equalityConstraints = new DynamicVectorFunction(exps, origSymbols);
    }
    if (inequExpList.size() > 0) {
        Expression[] exps = (Expression[]) org.vcell.util.BeanUtils.getArray(inequExpList, Expression.class);
        inequalityConstraints = new DynamicVectorFunction(exps, origSymbols);
    }
    AugmentedObjectiveFunction augmentedObjFunc = new AugmentedObjectiveFunction(objFunction, equalityConstraints, inequalityConstraints, power, mu, optSolverCallbacks);
    return augmentedObjFunc;
}
Also used : Constraint(cbit.vcell.opt.Constraint) ExplicitFitObjectiveFunction(cbit.vcell.opt.ExplicitFitObjectiveFunction) Constraint(cbit.vcell.opt.Constraint) DynamicScalarFunction(cbit.function.DynamicScalarFunction) ScalarFunction(cbit.function.ScalarFunction) DynamicScalarFunction(cbit.function.DynamicScalarFunction) Expression(cbit.vcell.parser.Expression) ExplicitObjectiveFunction(cbit.vcell.opt.ExplicitObjectiveFunction) Parameter(cbit.vcell.opt.Parameter) ImplicitObjectiveFunction(cbit.vcell.opt.ImplicitObjectiveFunction) DynamicVectorFunction(cbit.function.DynamicVectorFunction) ConstraintType(cbit.vcell.opt.ConstraintType) Vector(java.util.Vector)

Aggregations

ImplicitObjectiveFunction (cbit.vcell.opt.ImplicitObjectiveFunction)3 DefaultScalarFunction (cbit.function.DefaultScalarFunction)2 OptSolverResultSet (cbit.vcell.opt.OptSolverResultSet)2 OptimizationResultSet (cbit.vcell.opt.OptimizationResultSet)2 OptimizationSolverSpec (cbit.vcell.opt.OptimizationSolverSpec)2 OptimizationSpec (cbit.vcell.opt.OptimizationSpec)2 Parameter (cbit.vcell.opt.Parameter)2 PowellOptimizationSolver (cbit.vcell.opt.solvers.PowellOptimizationSolver)2 DefaultOptSolverCallbacks (org.vcell.optimization.DefaultOptSolverCallbacks)2 OptSolverCallbacks (org.vcell.optimization.OptSolverCallbacks)2 DynamicScalarFunction (cbit.function.DynamicScalarFunction)1 DynamicVectorFunction (cbit.function.DynamicVectorFunction)1 ScalarFunction (cbit.function.ScalarFunction)1 Constraint (cbit.vcell.opt.Constraint)1 ConstraintType (cbit.vcell.opt.ConstraintType)1 ExplicitFitObjectiveFunction (cbit.vcell.opt.ExplicitFitObjectiveFunction)1 ExplicitObjectiveFunction (cbit.vcell.opt.ExplicitObjectiveFunction)1 Expression (cbit.vcell.parser.Expression)1 Vector (java.util.Vector)1