Search in sources :

Example 1 with ExplicitObjectiveFunction

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

the class OptXmlWriter method getObjectiveFunctionXML.

public static Element getObjectiveFunctionXML(OptimizationSpec optimizationSpec) {
    Element objectiveFunctionElement = new Element(OptXmlTags.ObjectiveFunction_Tag);
    ObjectiveFunction objectiveFunction = optimizationSpec.getObjectiveFunction();
    if (objectiveFunction instanceof ExplicitObjectiveFunction) {
        ExplicitObjectiveFunction explicitObjectiveFunction = (ExplicitObjectiveFunction) objectiveFunction;
        objectiveFunctionElement.setAttribute(OptXmlTags.ObjectiveFunctionType_Attr, OptXmlTags.ObjectiveFunctionType_Attr_Explicit);
        Element expressionElement = new Element(OptXmlTags.Expression_Tag);
        expressionElement.addContent(explicitObjectiveFunction.getExpression().infix());
        objectiveFunctionElement.addContent(expressionElement);
    } else if (objectiveFunction instanceof ExplicitFitObjectiveFunction) {
        ExplicitFitObjectiveFunction explicitFitObjectiveFunction = (ExplicitFitObjectiveFunction) objectiveFunction;
        objectiveFunctionElement.setAttribute(OptXmlTags.ObjectiveFunctionType_Attr, OptXmlTags.ObjectiveFunctionType_Attr_ExplicitFit);
        // add expression list
        Element expressionListElement = new Element(OptXmlTags.ExpressionList_Tag);
        objectiveFunctionElement.addContent(expressionListElement);
        ExplicitFitObjectiveFunction.ExpressionDataPair[] expDataPairs = explicitFitObjectiveFunction.getExpressionDataPairs();
        for (int i = 0; i < expDataPairs.length; i++) {
            Element expressionElement = new Element(OptXmlTags.Expression_Tag);
            expressionElement.setAttribute(OptXmlTags.ExpRefDataColumnIndex_Attr, expDataPairs[i].getReferenceDataIndex() + "");
            expressionElement.addContent(expDataPairs[i].getFitFunction().infix());
            expressionListElement.addContent(expressionElement);
        }
        if (explicitFitObjectiveFunction.getReferenceData() instanceof SimpleReferenceData) {
            Element dataElement = getDataXML(explicitFitObjectiveFunction.getReferenceData());
            objectiveFunctionElement.addContent(dataElement);
        } else {
            throw new OptimizationException("Optimization XML Writer exports SimpleReferenceData only.");
        }
    } else if (objectiveFunction instanceof PdeObjectiveFunction) {
        PdeObjectiveFunction pdeObjectiveFunction = (PdeObjectiveFunction) objectiveFunction;
        objectiveFunctionElement.setAttribute(OptXmlTags.ObjectiveFunctionType_Attr, OptXmlTags.ObjectiveFunctionType_Attr_PDE);
        SpatialReferenceData refData = pdeObjectiveFunction.getReferenceData();
        Element dataElement = getDataXML(refData);
        objectiveFunctionElement.addContent(dataElement);
        Element modelElement = getModelXML(pdeObjectiveFunction, optimizationSpec.getParameterNames());
        objectiveFunctionElement.addContent(modelElement);
        Simulation tempSim = new Simulation(pdeObjectiveFunction.getMathDescription());
        SimulationSymbolTable simSymbolTable = new SimulationSymbolTable(tempSim, 0);
        // 
        for (int i = 1; i < refData.getNumVariables(); i++) {
            Element modelMappingElement = new Element(OptXmlTags.ModelMapping_Tag);
            modelMappingElement.setAttribute(OptXmlTags.ModelMappingDataColumn_Attr, refData.getVariableNames()[i]);
            modelMappingElement.setAttribute(OptXmlTags.ModelMappingWeight_Attr, String.valueOf(refData.getVariableWeights()[i]));
            Expression mappingExpression = null;
            try {
                Variable var = pdeObjectiveFunction.getMathDescription().getVariable(refData.getVariableNames()[i]);
                if (var instanceof Function) {
                    Expression exp = new Expression(var.getExpression());
                    exp.bindExpression(simSymbolTable);
                    exp = MathUtilities.substituteFunctions(exp, simSymbolTable);
                    mappingExpression = exp.flatten();
                } else {
                    mappingExpression = new Expression(var.getName());
                }
            } catch (ExpressionException e) {
                e.printStackTrace();
                throw new OptimizationException(e.getMessage());
            }
            modelMappingElement.addContent(mappingExpression.infix());
            objectiveFunctionElement.addContent(modelMappingElement);
        }
    }
    return objectiveFunctionElement;
}
Also used : OptimizationException(cbit.vcell.opt.OptimizationException) ReservedVariable(cbit.vcell.math.ReservedVariable) Variable(cbit.vcell.math.Variable) Element(org.jdom.Element) PdeObjectiveFunction(cbit.vcell.opt.PdeObjectiveFunction) ExplicitFitObjectiveFunction(cbit.vcell.opt.ExplicitFitObjectiveFunction) SimulationSymbolTable(cbit.vcell.solver.SimulationSymbolTable) ExplicitFitObjectiveFunction(cbit.vcell.opt.ExplicitFitObjectiveFunction) ExplicitObjectiveFunction(cbit.vcell.opt.ExplicitObjectiveFunction) ObjectiveFunction(cbit.vcell.opt.ObjectiveFunction) PdeObjectiveFunction(cbit.vcell.opt.PdeObjectiveFunction) SimpleReferenceData(cbit.vcell.opt.SimpleReferenceData) Constraint(cbit.vcell.opt.Constraint) ExpressionException(cbit.vcell.parser.ExpressionException) ExplicitFitObjectiveFunction(cbit.vcell.opt.ExplicitFitObjectiveFunction) Function(cbit.vcell.math.Function) ExplicitObjectiveFunction(cbit.vcell.opt.ExplicitObjectiveFunction) ObjectiveFunction(cbit.vcell.opt.ObjectiveFunction) PdeObjectiveFunction(cbit.vcell.opt.PdeObjectiveFunction) Simulation(cbit.vcell.solver.Simulation) Expression(cbit.vcell.parser.Expression) ExplicitObjectiveFunction(cbit.vcell.opt.ExplicitObjectiveFunction) SpatialReferenceData(cbit.vcell.opt.SpatialReferenceData)

Example 2 with ExplicitObjectiveFunction

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

Constraint (cbit.vcell.opt.Constraint)2 ExplicitFitObjectiveFunction (cbit.vcell.opt.ExplicitFitObjectiveFunction)2 ExplicitObjectiveFunction (cbit.vcell.opt.ExplicitObjectiveFunction)2 Expression (cbit.vcell.parser.Expression)2 DynamicScalarFunction (cbit.function.DynamicScalarFunction)1 DynamicVectorFunction (cbit.function.DynamicVectorFunction)1 ScalarFunction (cbit.function.ScalarFunction)1 Function (cbit.vcell.math.Function)1 ReservedVariable (cbit.vcell.math.ReservedVariable)1 Variable (cbit.vcell.math.Variable)1 ConstraintType (cbit.vcell.opt.ConstraintType)1 ImplicitObjectiveFunction (cbit.vcell.opt.ImplicitObjectiveFunction)1 ObjectiveFunction (cbit.vcell.opt.ObjectiveFunction)1 OptimizationException (cbit.vcell.opt.OptimizationException)1 Parameter (cbit.vcell.opt.Parameter)1 PdeObjectiveFunction (cbit.vcell.opt.PdeObjectiveFunction)1 SimpleReferenceData (cbit.vcell.opt.SimpleReferenceData)1 SpatialReferenceData (cbit.vcell.opt.SpatialReferenceData)1 ExpressionException (cbit.vcell.parser.ExpressionException)1 Simulation (cbit.vcell.solver.Simulation)1