use of org.vcell.optimization.thrift.OptRun 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();
}
}
use of org.vcell.optimization.thrift.OptRun in project vcell by virtualcell.
the class CopasiServicePython method readOptRun.
public static OptRun readOptRun(File optRunFile) throws IOException {
TDeserializer deserializer = new TDeserializer(new TBinaryProtocol.Factory());
try {
OptRun optJob = new OptRun();
byte[] bytes = FileUtils.readFileToByteArray(optRunFile);
deserializer.deserialize(optJob, bytes);
return optJob;
} catch (TException e) {
e.printStackTrace();
throw new IOException("error reading optRun from file " + optRunFile.getPath() + ": " + e.getMessage(), e);
}
}
use of org.vcell.optimization.thrift.OptRun 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();
}
}
use of org.vcell.optimization.thrift.OptRun 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());
}
}
use of org.vcell.optimization.thrift.OptRun in project vcell by virtualcell.
the class CopasiOptimizationSolver method solveLocalPython.
public static OptimizationResultSet solveLocalPython(ParameterEstimationTaskSimulatorIDA parestSimulator, ParameterEstimationTask parameterEstimationTask, CopasiOptSolverCallbacks optSolverCallbacks, MathMappingCallback mathMappingCallback) throws IOException, ExpressionException, OptimizationException {
File dir = Files.createTempDirectory("parest", new FileAttribute<?>[] {}).toFile();
try {
String prefix = "testing_" + Math.abs(new Random().nextInt(10000));
File optProblemThriftFile = new File(dir, prefix + ".optprob.bin");
File optRunFile = new File(dir, prefix + ".optrun.bin");
//
// Setup Python COPASI opt problem and write to disk
//
OptProblem optProblem = CopasiServicePython.makeOptProblem(parameterEstimationTask);
CopasiServicePython.writeOptProblem(optProblemThriftFile, optProblem);
//
// run Python COPASI opt problem
//
CopasiServicePython.runCopasiPython(optProblemThriftFile, optRunFile);
if (!optRunFile.exists()) {
throw new RuntimeException("COPASI optimization output file not found:\n" + optRunFile.getAbsolutePath());
}
OptRun optRun = CopasiServicePython.readOptRun(optRunFile);
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 PYTHON---------------\n" + optResultSet.toString());
return copasiOptimizationResultSet;
} catch (Throwable e) {
e.printStackTrace(System.out);
throw new OptimizationException(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
} finally {
if (dir != null && dir.exists()) {
FileUtils.deleteDirectory(dir);
}
}
}
Aggregations