use of cbit.vcell.opt.ObjectiveFunction 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;
}
Aggregations