use of cbit.vcell.opt.SimpleReferenceData in project vcell by virtualcell.
the class ReferenceDataPanel method showEditor.
/**
* Comment
*/
private void showEditor() {
ReferenceData referenceData = fieldParameterEstimationTask.getModelOptimizationSpec().getReferenceData();
if (referenceData != null) {
geteditorTextArea().setText(((SimpleReferenceData) referenceData).getCSV());
} else {
geteditorTextArea().setText("t, data1, data2\n0.0, 0.1, 0.21\n0.1, 0.15, 0.31\n0.2, 0.16, 0.44");
}
geteditorTextArea().setCaretPosition(0);
try {
int retVal = DialogUtils.showComponentOKCancelDialog(this, geteditorPanel(), "time series data editor");
if (retVal == javax.swing.JOptionPane.OK_OPTION) {
RowColumnResultSet rc = (new CSV()).importFrom(new java.io.StringReader(geteditorTextArea().getText()));
double[] weights = new double[rc.getDataColumnCount()];
java.util.Arrays.fill(weights, 1.0);
SimpleReferenceData simpleRefData = new SimpleReferenceData(rc, weights);
updateReferenceData(simpleRefData);
}
} catch (ParseException e) {
e.printStackTrace();
showHelp(e);
} catch (Exception e) {
e.printStackTrace(System.out);
DialogUtils.showErrorDialog(this, e.getMessage(), e);
}
}
use of cbit.vcell.opt.SimpleReferenceData in project vcell by virtualcell.
the class CopasiServicePython method makeOptProblem.
public static OptProblem makeOptProblem(ParameterEstimationTask parameterEstimationTask) throws IOException, ExpressionException, SBMLException, XMLStreamException {
OptimizationSpec optimizationSpec = parameterEstimationTask.getModelOptimizationMapping().getOptimizationSpec();
SimulationContext simulationContext = parameterEstimationTask.getSimulationContext();
MathMappingCallback callback = new MathMappingCallback() {
@Override
public void setProgressFraction(float fractionDone) {
Thread.dumpStack();
System.out.println("---> stdout mathMapping: progress = " + (fractionDone * 100.0) + "% done");
}
@Override
public void setMessage(String message) {
Thread.dumpStack();
System.out.println("---> stdout mathMapping: message = " + message);
}
@Override
public boolean isInterrupted() {
return false;
}
};
simulationContext.refreshMathDescription(callback, NetworkGenerationRequirements.ComputeFullStandardTimeout);
MathModel vcellMathModel = new MathModel(null);
vcellMathModel.setMathDescription(simulationContext.getMathDescription());
// get math model string
String sbmlString = MathModel_SBMLExporter.getSBMLString(vcellMathModel, 2, 4);
OptProblem optProblem = new OptProblem();
optProblem.setMathModelSbmlContents(sbmlString);
optProblem.setNumberOfOptimizationRuns(parameterEstimationTask.getOptimizationSolverSpec().getNumOfRuns());
for (Parameter p : optimizationSpec.getParameters()) {
ParameterDescription pdesc = new ParameterDescription(p.getName(), p.getScale(), p.getLowerBound(), p.getUpperBound(), p.getInitialGuess());
optProblem.addToParameterDescriptionList(pdesc);
}
SimpleReferenceData refData = (SimpleReferenceData) parameterEstimationTask.getModelOptimizationSpec().getReferenceData();
// check if t is at the first column
int timeIndex = refData.findColumn(ReservedVariable.TIME.getName());
if (timeIndex != 0) {
throw new RuntimeException("t must be the first column");
}
DataSet dataset = new DataSet();
for (int rowIndex = 0; rowIndex < refData.getNumDataRows(); rowIndex++) {
DataRow dataRow = new DataRow();
double[] array = refData.getDataByRow(rowIndex);
for (double d : array) {
dataRow.addToData(d);
}
dataset.addToRows(dataRow);
}
optProblem.setExperimentalDataSet(dataset);
optProblem.addToReferenceVariableList(new ReferenceVariable(ReservedVariable.TIME.getName(), ReferenceVariableType.independent));
// add all other dependent variables, recall that the dependent variables start from 2nd column onward in reference data
for (int i = 1; i < refData.getNumDataColumns(); i++) {
ReferenceDataMappingSpec rdms = parameterEstimationTask.getModelOptimizationSpec().getReferenceDataMappingSpec(refData.getColumnNames()[i]);
optProblem.addToReferenceVariableList(new ReferenceVariable(rdms.getModelObject().getName(), ReferenceVariableType.dependent));
}
cbit.vcell.opt.CopasiOptimizationMethod vcellCopasiOptimizationMethod = parameterEstimationTask.getOptimizationSolverSpec().getCopasiOptimizationMethod();
OptimizationMethodType optMethodType = OptimizationMethodType.valueOf(vcellCopasiOptimizationMethod.getType().name());
CopasiOptimizationMethod thriftOptMethod = new CopasiOptimizationMethod();
thriftOptMethod.setOptimizationMethodType(optMethodType);
for (cbit.vcell.opt.CopasiOptimizationParameter optParam : vcellCopasiOptimizationMethod.getParameters()) {
org.vcell.optimization.thrift.CopasiOptimizationParameter p = new org.vcell.optimization.thrift.CopasiOptimizationParameter();
p.setValue(optParam.getValue());
org.vcell.optimization.thrift.OptimizationParameterType optParmType = null;
org.vcell.optimization.thrift.OptimizationParameterDataType optDataType = null;
switch(optParam.getType()) {
case Cooling_Factor:
optParmType = OptimizationParameterType.Cooling_Factor;
optDataType = OptimizationParameterDataType.DOUBLE;
break;
case IterationLimit:
optParmType = OptimizationParameterType.IterationLimit;
optDataType = OptimizationParameterDataType.INT;
break;
case Number_of_Generations:
optParmType = OptimizationParameterType.Number_of_Generations;
optDataType = OptimizationParameterDataType.INT;
break;
case Number_of_Iterations:
optParmType = OptimizationParameterType.Number_of_Iterations;
optDataType = OptimizationParameterDataType.INT;
break;
case Pf:
optParmType = OptimizationParameterType.Pf;
optDataType = OptimizationParameterDataType.DOUBLE;
break;
case Population_Size:
optParmType = OptimizationParameterType.Population_Size;
optDataType = OptimizationParameterDataType.INT;
break;
case Random_Number_Generator:
optParmType = OptimizationParameterType.Random_Number_Generator;
optDataType = OptimizationParameterDataType.INT;
break;
case Rho:
optParmType = OptimizationParameterType.Rho;
optDataType = OptimizationParameterDataType.DOUBLE;
break;
case Scale:
optParmType = OptimizationParameterType.Scale;
optDataType = OptimizationParameterDataType.DOUBLE;
break;
case Seed:
optParmType = OptimizationParameterType.Seed;
optDataType = OptimizationParameterDataType.INT;
break;
case Start_Temperature:
optParmType = OptimizationParameterType.Start_Temperature;
optDataType = OptimizationParameterDataType.DOUBLE;
break;
case Std_Deviation:
optParmType = OptimizationParameterType.Std_Deviation;
optDataType = OptimizationParameterDataType.DOUBLE;
break;
case Swarm_Size:
optParmType = OptimizationParameterType.Swarm_Size;
optDataType = OptimizationParameterDataType.INT;
break;
case Tolerance:
optParmType = OptimizationParameterType.Tolerance;
optDataType = OptimizationParameterDataType.DOUBLE;
break;
default:
throw new RuntimeException("unsupported parameter type :" + optParam.getType().name() + " in COPASI optimization solver");
}
p.setParamType(optParmType);
p.setDataType(optDataType);
thriftOptMethod.addToOptimizationParameterList(p);
}
optProblem.setOptimizationMethod(thriftOptMethod);
return optProblem;
}
use of cbit.vcell.opt.SimpleReferenceData in project vcell by virtualcell.
the class OptXmlWriter method getDataXML.
public static Element getDataXML(ReferenceData refData) {
Element refDataElement = null;
if (refData instanceof SimpleReferenceData) {
refDataElement = new Element(OptXmlTags.SimpleReferenceData_Tag);
} else if (refData instanceof SpatialReferenceData) {
refDataElement = new Element(OptXmlTags.SpatialReferenceData_Tag);
}
if (refDataElement != null) {
// write variable declarations
// independent variable is t and dimension is 1, these are fixed.
Element timeVarElement = new Element(OptXmlTags.Variable_Tag);
timeVarElement.setAttribute(OptXmlTags.VariableType_Attr, OptXmlTags.VariableType_Attr_Independent);
timeVarElement.setAttribute(OptXmlTags.VariableName_Attr, ReservedVariable.TIME.getName());
timeVarElement.setAttribute(OptXmlTags.VariableDimension_Attr, "1");
refDataElement.addContent(timeVarElement);
// check if t is at the first column
int timeIndex = refData.findColumn(ReservedVariable.TIME.getName());
if (timeIndex != 0) {
throw new RuntimeException("t must be the first column");
}
// add all other dependent variables, recall that the dependent variables start from 2nd column onward in reference data
for (int i = 1; i < refData.getNumDataColumns(); i++) {
Element variableElement = new Element(OptXmlTags.Variable_Tag);
variableElement.setAttribute(OptXmlTags.VariableType_Attr, OptXmlTags.VariableType_Attr_Dependent);
variableElement.setAttribute(OptXmlTags.VariableName_Attr, refData.getColumnNames()[i]);
variableElement.setAttribute(OptXmlTags.VariableDimension_Attr, refData.getDataSize() + "");
refDataElement.addContent(variableElement);
}
// write data
Element dataRowElement = new Element(OptXmlTags.Datarow_Tag);
for (int i = 0; i < refData.getNumDataRows(); i++) {
double[] data = refData.getDataByRow(i);
Element rowElement = new Element(OptXmlTags.Row_Tag);
StringBuffer rowText = new StringBuffer();
for (int j = 0; j < data.length; j++) {
rowText.append(data[j] + " ");
}
rowElement.addContent(rowText.toString());
dataRowElement.addContent(rowElement);
}
refDataElement.addContent(dataRowElement);
// write weights
Element weightRowElement = new Element(OptXmlTags.WeightDatarow_Tag);
// add weight type
if (// print weights in multiple rows, each row has one-to-more elements
refData.getWeights() instanceof ElementWeights) {
weightRowElement.setAttribute(OptXmlTags.WeightType_Attr, OptXmlTags.WeightType_Attr_Element);
double[][] weightData = ((ElementWeights) refData.getWeights()).getWeightData();
// elementWeights: first dimensin is number of rows, second dimension is number of vars
for (int i = 0; i < weightData.length; i++) {
double[] rowData = weightData[i];
Element rowElement = new Element(OptXmlTags.Row_Tag);
StringBuffer rowText = new StringBuffer();
for (int j = 0; j < rowData.length; j++) {
rowText.append(rowData[j] + " ");
}
rowElement.addContent(rowText.toString());
weightRowElement.addContent(rowElement);
}
} else if (// print weights in one row, the row has one-to-more elements
refData.getWeights() instanceof VariableWeights) {
weightRowElement.setAttribute(OptXmlTags.WeightType_Attr, OptXmlTags.WeightType_Attr_Variable);
double[] weightData = ((VariableWeights) refData.getWeights()).getWeightData();
Element rowElement = new Element(OptXmlTags.Row_Tag);
StringBuffer rowText = new StringBuffer();
for (int j = 0; j < weightData.length; j++) {
rowText.append(weightData[j] + " ");
}
rowElement.addContent(rowText.toString());
weightRowElement.addContent(rowElement);
} else // time weights. Print weights in multiple rows, each row has one element
{
weightRowElement.setAttribute(OptXmlTags.WeightType_Attr, OptXmlTags.WeightType_Attr_Time);
double[] weightData = ((TimeWeights) refData.getWeights()).getWeightData();
for (int j = 0; j < weightData.length; j++) {
Element rowElement = new Element(OptXmlTags.Row_Tag);
StringBuffer rowText = new StringBuffer();
rowText.append(weightData[j] + " ");
rowElement.addContent(rowText.toString());
weightRowElement.addContent(rowElement);
}
}
refDataElement.addContent(weightRowElement);
}
return refDataElement;
}
use of cbit.vcell.opt.SimpleReferenceData 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;
}
use of cbit.vcell.opt.SimpleReferenceData in project vcell by virtualcell.
the class CurveFitting method solve.
public static OptimizationResultSet solve(ExplicitFitObjectiveFunction.ExpressionDataPair[] expDataPairs, Parameter[] parameters, double[] time, double[][] data, String[] colNames, Weights weights) throws ExpressionException, OptimizationException, IOException {
// choose optimization solver, currently we have Powell
PowellOptimizationSolver optService = new PowellOptimizationSolver();
OptimizationSpec optSpec = new OptimizationSpec();
// create simple reference data, columns: t + dataColumns
double[][] realData = new double[1 + data.length][time.length];
for (// add time column
int i = 0; // add time column
i < time.length; // add time column
i++) {
realData[0][i] = time[i];
}
for (// add each data column to realData
int i = 0; // add each data column to realData
i < data.length; // add each data column to realData
i++) {
for (int j = 0; j < time.length; j++) {
realData[1 + i][j] = data[i][j];
}
}
SimpleReferenceData refData = new SimpleReferenceData(colNames, weights, realData);
// send to optimization service
optSpec.setObjectiveFunction(new ExplicitFitObjectiveFunction(expDataPairs, refData));
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);
return optResultSet;
}
Aggregations