Search in sources :

Example 1 with OptRunStatus

use of org.vcell.optimization.thrift.OptRunStatus in project vcell by virtualcell.

the class OptServerImplTest method main.

public static void main(String[] args) {
    try {
        System.setProperty(PropertyLoader.installationRoot, "/Users/schaff/Documents/workspace-modular/vcell");
        System.setProperty(PropertyLoader.pythonExe, "/Users/schaff/anaconda/bin/python");
        PythonSupport.verifyInstallation(new PythonPackage[] { PythonPackage.COPASI, PythonPackage.LIBSBML, PythonPackage.THRIFT });
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
        }
        File optProbFile = new File(ResourceUtil.getVCellOptPythonDir(), "optprob.bin");
        System.out.println("using optProblem: " + optProbFile.getAbsolutePath());
        OptServerImpl optServerImpl = new OptServerImpl();
        OptProblem optProblem = CopasiServicePython.readOptProblem(optProbFile);
        ArrayList<String> jobIDs = new ArrayList<String>();
        jobIDs.add(optServerImpl.submit(optProblem));
        jobIDs.add(optServerImpl.submit(optProblem));
        optServerImpl.start();
        jobIDs.add(optServerImpl.submit(optProblem));
        jobIDs.add(optServerImpl.submit(optProblem));
        boolean done = false;
        while (!done) {
            done = true;
            for (String jobID : jobIDs) {
                OptRunContext optRunContext = optServerImpl.getOptRunContextByOptimizationId(jobID);
                if (optRunContext == null) {
                    throw new RuntimeException("optRunContext was null for id = " + jobID);
                }
                OptRunStatus status = optRunContext.getStatus();
                if (status != OptRunStatus.Complete && status != OptRunStatus.Failed) {
                    done = false;
                }
                if (status == OptRunStatus.Complete) {
                    OptRun optRun = CopasiServicePython.readOptRun(optRunContext.getOptRunBinaryFile());
                    System.out.println("job " + jobID + ": status " + status + " " + optRun.getOptResultSet().toString());
                } else {
                    System.out.println("job " + jobID + ": status " + status);
                }
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
        }
        System.out.println("done with all jobs");
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : OptProblem(org.vcell.optimization.thrift.OptProblem) ArrayList(java.util.ArrayList) OptRunContext(org.vcell.optimization.OptServerImpl.OptRunContext) OptRunStatus(org.vcell.optimization.thrift.OptRunStatus) OptRun(org.vcell.optimization.thrift.OptRun) File(java.io.File)

Example 2 with OptRunStatus

use of org.vcell.optimization.thrift.OptRunStatus in project vcell by virtualcell.

the class OptimizationApiTest method main.

public static void main(String[] args) {
    try {
        boolean bIgnoreCertProblems = true;
        boolean bIgnoreHostMismatch = true;
        VCellApiClient apiClient = new VCellApiClient(host, port, bIgnoreCertProblems, bIgnoreHostMismatch);
        File optProbFile = new File("../pythonScripts/VCell_Opt/optprob.bin");
        System.out.println("using optProblem: " + optProbFile.getAbsolutePath());
        OptProblem optProblem = readOptProblem(optProbFile);
        TSerializer serializer = new TSerializer(new TJSONProtocol.Factory());
        String optProblemJson = serializer.toString(optProblem);
        ArrayList<String> jobIDs = new ArrayList<String>();
        jobIDs.add(apiClient.submitOptimization(optProblemJson));
        jobIDs.add(apiClient.submitOptimization(optProblemJson));
        jobIDs.add(apiClient.submitOptimization(optProblemJson));
        jobIDs.add(apiClient.submitOptimization(optProblemJson));
        boolean done = false;
        while (!done) {
            done = true;
            for (String jobID : jobIDs) {
                String optRunJson = apiClient.getOptRunJson(jobID);
                TDeserializer deserializer = new TDeserializer(new TJSONProtocol.Factory());
                OptRun optRun = new OptRun();
                deserializer.deserialize(optRun, optRunJson.getBytes());
                OptRunStatus status = optRun.status;
                if (status != OptRunStatus.Complete && status != OptRunStatus.Failed) {
                    done = false;
                }
                if (status == OptRunStatus.Complete) {
                    System.out.println("job " + jobID + ": status " + status + " " + optRun.getOptResultSet().toString());
                } else {
                    System.out.println("job " + jobID + ": status " + status);
                }
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
        }
        System.out.println("done with all jobs");
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : TDeserializer(org.apache.thrift.TDeserializer) OptProblem(org.vcell.optimization.thrift.OptProblem) ArrayList(java.util.ArrayList) VCellApiClient(org.vcell.api.client.VCellApiClient) TException(org.apache.thrift.TException) IOException(java.io.IOException) TSerializer(org.apache.thrift.TSerializer) TJSONProtocol(org.apache.thrift.protocol.TJSONProtocol) OptRunStatus(org.vcell.optimization.thrift.OptRunStatus) OptRun(org.vcell.optimization.thrift.OptRun) File(java.io.File)

Example 3 with OptRunStatus

use of org.vcell.optimization.thrift.OptRunStatus in project vcell by virtualcell.

the class CopasiOptimizationSolver method solveRemoteApi.

public static OptimizationResultSet solveRemoteApi(ParameterEstimationTaskSimulatorIDA parestSimulator, ParameterEstimationTask parameterEstimationTask, CopasiOptSolverCallbacks optSolverCallbacks, MathMappingCallback mathMappingCallback) throws IOException, ExpressionException, OptimizationException {
    try {
        OptProblem optProblem = CopasiServicePython.makeOptProblem(parameterEstimationTask);
        boolean bIgnoreCertProblems = true;
        boolean bIgnoreHostMismatch = true;
        // e.g. vcell.serverhost=vcellapi.cam.uchc.edu:8080
        String serverHost = PropertyLoader.getRequiredProperty(PropertyLoader.vcellServerHost);
        String[] parts = serverHost.split(":");
        String host = parts[0];
        int port = Integer.parseInt(parts[1]);
        VCellApiClient apiClient = new VCellApiClient(host, port, bIgnoreCertProblems, bIgnoreHostMismatch);
        TSerializer serializer = new TSerializer(new TJSONProtocol.Factory());
        String optProblemJson = serializer.toString(optProblem);
        String optimizationId = apiClient.submitOptimization(optProblemJson);
        // 20 second minute timeout
        final long TIMEOUT_MS = 1000 * 20;
        long startTime = System.currentTimeMillis();
        OptRun optRun = null;
        while ((System.currentTimeMillis() - startTime) < TIMEOUT_MS) {
            if (optSolverCallbacks.getStopRequested()) {
                throw new RuntimeException(STOP_REQUESTED);
            }
            String optRunJson = apiClient.getOptRunJson(optimizationId);
            TDeserializer deserializer = new TDeserializer(new TJSONProtocol.Factory());
            optRun = new OptRun();
            deserializer.deserialize(optRun, optRunJson.getBytes());
            OptRunStatus status = optRun.status;
            if (status == OptRunStatus.Complete) {
                System.out.println("job " + optimizationId + ": status " + status + " " + optRun.getOptResultSet().toString());
                break;
            }
            if (status == OptRunStatus.Failed) {
                throw new RuntimeException("optimization failed, message=" + optRun.statusMessage);
            }
            System.out.println("job " + optimizationId + ": status " + status);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
        }
        System.out.println("done with optimization");
        OptResultSet optResultSet = optRun.getOptResultSet();
        int numFittedParameters = optResultSet.getOptParameterValues().size();
        String[] paramNames = new String[numFittedParameters];
        double[] paramValues = new double[numFittedParameters];
        for (int pIndex = 0; pIndex < numFittedParameters; pIndex++) {
            OptParameterValue optParamValue = optResultSet.getOptParameterValues().get(pIndex);
            paramNames[pIndex] = optParamValue.parameterName;
            paramValues[pIndex] = optParamValue.bestValue;
        }
        OptimizationStatus status = new OptimizationStatus(OptimizationStatus.NORMAL_TERMINATION, optRun.statusMessage);
        OptRunResultSet optRunResultSet = new OptRunResultSet(paramValues, optResultSet.objectiveFunction, optResultSet.numFunctionEvaluations, status);
        OptSolverResultSet copasiOptSolverResultSet = new OptSolverResultSet(paramNames, optRunResultSet);
        RowColumnResultSet copasiRcResultSet = parestSimulator.getRowColumnRestultSetByBestEstimations(parameterEstimationTask, paramNames, paramValues);
        OptimizationResultSet copasiOptimizationResultSet = new OptimizationResultSet(copasiOptSolverResultSet, copasiRcResultSet);
        System.out.println("-----------SOLUTION FROM VCellAPI---------------\n" + optResultSet.toString());
        return copasiOptimizationResultSet;
    } catch (Exception e) {
        e.printStackTrace(System.out);
        if (e.getMessage() != null && e.getMessage().equals(STOP_REQUESTED)) {
            throw UserCancelException.CANCEL_GENERIC;
        }
        throw new OptimizationException(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
    }
}
Also used : OptimizationException(cbit.vcell.opt.OptimizationException) TDeserializer(org.apache.thrift.TDeserializer) OptimizationResultSet(cbit.vcell.opt.OptimizationResultSet) OptParameterValue(org.vcell.optimization.thrift.OptParameterValue) OptSolverResultSet(cbit.vcell.opt.OptSolverResultSet) TJSONProtocol(org.apache.thrift.protocol.TJSONProtocol) OptRunStatus(org.vcell.optimization.thrift.OptRunStatus) OptRun(org.vcell.optimization.thrift.OptRun) OptResultSet(org.vcell.optimization.thrift.OptResultSet) OptimizationStatus(cbit.vcell.opt.OptimizationStatus) RowColumnResultSet(cbit.vcell.math.RowColumnResultSet) OptProblem(org.vcell.optimization.thrift.OptProblem) VCellApiClient(org.vcell.api.client.VCellApiClient) OptimizationException(cbit.vcell.opt.OptimizationException) IOException(java.io.IOException) ExpressionException(cbit.vcell.parser.ExpressionException) MathException(cbit.vcell.math.MathException) UserCancelException(org.vcell.util.UserCancelException) TSerializer(org.apache.thrift.TSerializer) OptRunResultSet(cbit.vcell.opt.OptSolverResultSet.OptRunResultSet)

Aggregations

OptProblem (org.vcell.optimization.thrift.OptProblem)3 OptRun (org.vcell.optimization.thrift.OptRun)3 OptRunStatus (org.vcell.optimization.thrift.OptRunStatus)3 File (java.io.File)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 TDeserializer (org.apache.thrift.TDeserializer)2 TSerializer (org.apache.thrift.TSerializer)2 TJSONProtocol (org.apache.thrift.protocol.TJSONProtocol)2 VCellApiClient (org.vcell.api.client.VCellApiClient)2 MathException (cbit.vcell.math.MathException)1 RowColumnResultSet (cbit.vcell.math.RowColumnResultSet)1 OptSolverResultSet (cbit.vcell.opt.OptSolverResultSet)1 OptRunResultSet (cbit.vcell.opt.OptSolverResultSet.OptRunResultSet)1 OptimizationException (cbit.vcell.opt.OptimizationException)1 OptimizationResultSet (cbit.vcell.opt.OptimizationResultSet)1 OptimizationStatus (cbit.vcell.opt.OptimizationStatus)1 ExpressionException (cbit.vcell.parser.ExpressionException)1 TException (org.apache.thrift.TException)1 OptRunContext (org.vcell.optimization.OptServerImpl.OptRunContext)1