use of org.vcell.optimization.thrift.OptProblem 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 org.vcell.optimization.thrift.OptProblem in project vcell by virtualcell.
the class CopasiServicePython method readOptProblem.
public static OptProblem readOptProblem(File optProblemFile) throws IOException {
TDeserializer deserializer = new TDeserializer(new TBinaryProtocol.Factory());
try {
OptProblem optProblem = new OptProblem();
byte[] bytes = FileUtils.readFileToByteArray(optProblemFile);
deserializer.deserialize(optProblem, bytes);
return optProblem;
} catch (TException e) {
e.printStackTrace();
throw new IOException("error reading optProblem from file " + optProblemFile.getPath() + ": " + e.getMessage(), e);
}
}
Aggregations