Search in sources :

Example 1 with ReferenceVariable

use of org.vcell.optimization.thrift.ReferenceVariable 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;
}
Also used : MathModel(cbit.vcell.mathmodel.MathModel) DataSet(org.vcell.optimization.thrift.DataSet) ReferenceDataMappingSpec(cbit.vcell.modelopt.ReferenceDataMappingSpec) DataRow(org.vcell.optimization.thrift.DataRow) CopasiOptimizationMethod(org.vcell.optimization.thrift.CopasiOptimizationMethod) OptimizationParameterType(org.vcell.optimization.thrift.OptimizationParameterType) OptimizationParameterDataType(org.vcell.optimization.thrift.OptimizationParameterDataType) OptimizationSpec(cbit.vcell.opt.OptimizationSpec) ReferenceVariable(org.vcell.optimization.thrift.ReferenceVariable) MathMappingCallback(cbit.vcell.mapping.SimulationContext.MathMappingCallback) OptProblem(org.vcell.optimization.thrift.OptProblem) SimulationContext(cbit.vcell.mapping.SimulationContext) SimpleReferenceData(cbit.vcell.opt.SimpleReferenceData) OptimizationMethodType(org.vcell.optimization.thrift.OptimizationMethodType) Parameter(cbit.vcell.opt.Parameter) ParameterDescription(org.vcell.optimization.thrift.ParameterDescription)

Aggregations

SimulationContext (cbit.vcell.mapping.SimulationContext)1 MathMappingCallback (cbit.vcell.mapping.SimulationContext.MathMappingCallback)1 MathModel (cbit.vcell.mathmodel.MathModel)1 ReferenceDataMappingSpec (cbit.vcell.modelopt.ReferenceDataMappingSpec)1 OptimizationSpec (cbit.vcell.opt.OptimizationSpec)1 Parameter (cbit.vcell.opt.Parameter)1 SimpleReferenceData (cbit.vcell.opt.SimpleReferenceData)1 CopasiOptimizationMethod (org.vcell.optimization.thrift.CopasiOptimizationMethod)1 DataRow (org.vcell.optimization.thrift.DataRow)1 DataSet (org.vcell.optimization.thrift.DataSet)1 OptProblem (org.vcell.optimization.thrift.OptProblem)1 OptimizationMethodType (org.vcell.optimization.thrift.OptimizationMethodType)1 OptimizationParameterDataType (org.vcell.optimization.thrift.OptimizationParameterDataType)1 OptimizationParameterType (org.vcell.optimization.thrift.OptimizationParameterType)1 ParameterDescription (org.vcell.optimization.thrift.ParameterDescription)1 ReferenceVariable (org.vcell.optimization.thrift.ReferenceVariable)1