use of org.vcell.optimization.thrift.OptParameterValue 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.OptParameterValue 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);
}
}
}
use of org.vcell.optimization.thrift.OptParameterValue in project vcell by virtualcell.
the class CopasiOptimizationSolver method solveRemoteApi.
public static OptimizationResultSet solveRemoteApi(ParameterEstimationTaskSimulatorIDA parestSimulator, ParameterEstimationTask parameterEstimationTask, CopasiOptSolverCallbacks optSolverCallbacks, MathMappingCallback mathMappingCallback, ClientTaskStatusSupport clientTaskStatusSupport) throws IOException, ExpressionException, OptimizationException {
try {
if (clientTaskStatusSupport != null) {
clientTaskStatusSupport.setMessage("Generating opt problem...");
}
OptProblem optProblem = CopasiServicePython.makeOptProblem(parameterEstimationTask);
boolean bIgnoreCertProblems = true;
boolean bIgnoreHostMismatch = true;
// e.g. vcell.serverhost=vcellapi.cam.uchc.edu:443
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);
if (clientTaskStatusSupport != null) {
clientTaskStatusSupport.setMessage("Submitting opt problem...");
}
// Submit but allow user to get out from restlet blocking call
final String[] optIdHolder = new String[] { null };
final Exception[] exceptHolder = new Exception[] { null };
Thread submitThread = new Thread(new Runnable() {
@Override
public void run() {
try {
optIdHolder[0] = apiClient.submitOptimization(optProblemJson);
if (optSolverCallbacks.getStopRequested()) {
apiClient.getOptRunJson(optIdHolder[0], optSolverCallbacks.getStopRequested());
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
exceptHolder[0] = e;
}
}
});
submitThread.setDaemon(true);
submitThread.start();
while (optIdHolder[0] == null && exceptHolder[0] == null && !optSolverCallbacks.getStopRequested()) {
Thread.sleep(200);
}
if (exceptHolder[0] != null) {
throw exceptHolder[0];
}
if (optSolverCallbacks.getStopRequested()) {
throw UserCancelException.CANCEL_GENERIC;
}
// 200 second timeout
final long TIMEOUT_MS = 1000 * 200;
long startTime = System.currentTimeMillis();
OptRun optRun = null;
if (clientTaskStatusSupport != null) {
clientTaskStatusSupport.setMessage("Waiting for progress...");
}
while ((System.currentTimeMillis() - startTime) < TIMEOUT_MS) {
String optRunJson = apiClient.getOptRunJson(optIdHolder[0], optSolverCallbacks.getStopRequested());
if (optSolverCallbacks.getStopRequested()) {
throw UserCancelException.CANCEL_GENERIC;
}
if (optRunJson.startsWith(OptRunStatus.Queued.name() + ":")) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
optSolverCallbacks.setEvaluation(0, 0, 0, optSolverCallbacks.getEndValue(), 0);
}
});
if (clientTaskStatusSupport != null) {
clientTaskStatusSupport.setMessage("Queued...");
}
} else if (optRunJson.startsWith("Failed:") || optRunJson.startsWith("exception:") || optRunJson.startsWith("Exception:")) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
optSolverCallbacks.setEvaluation(0, 0, 0, optSolverCallbacks.getEndValue(), 0);
}
});
if (clientTaskStatusSupport != null) {
clientTaskStatusSupport.setMessage(optRunJson);
}
} else if (optRunJson.startsWith(OptRunStatus.Running.name() + ":")) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
StringTokenizer st = new StringTokenizer(optRunJson, " :\t\r\n");
if (st.countTokens() != 4) {
System.out.println(optRunJson);
return;
}
// OptRunStatus mesg
st.nextToken();
int runNum = Integer.parseInt(st.nextToken());
double objFunctionValue = Double.parseDouble(st.nextToken());
int numObjFuncEvals = Integer.parseInt(st.nextToken());
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
optSolverCallbacks.setEvaluation(numObjFuncEvals, objFunctionValue, 1.0, null, runNum);
}
});
} catch (Exception e) {
System.out.println(optRunJson);
e.printStackTrace();
}
}
});
if (clientTaskStatusSupport != null) {
clientTaskStatusSupport.setMessage("Running...");
}
} else {
// File f = new File("/home/vcell/fake_share_apps_vcell3/users/ParamOptemize_"+optimizationId+"/ParamOptemize_"+optimizationId+"_optRun.bin");
// byte[] filesbytes = FileUtils.readFileToByteArray(f);
// optRunJson.getBytes();
byte[] jsonbytes = Base64.getDecoder().decode(optRunJson);
// System.out.println(filesbytes.length+" "+jsonbytes.length);
// for (int i = 0; i < filesbytes.length; i++) {
// if(filesbytes[i] != jsonbytes[i]) {
// System.out.println("differ at "+i);
// break;
// }
// }
TDeserializer deserializer = new TDeserializer(new TBinaryProtocol.Factory());
// TDeserializer deserializer = new TDeserializer(new TJSONProtocol.Factory());
optRun = new OptRun();
deserializer.deserialize(optRun, jsonbytes);
OptRunStatus status = optRun.status;
String statusMessage = optRun.getStatusMessage();
if (statusMessage != null && (statusMessage.toLowerCase().startsWith(OptRunStatus.Complete.name().toLowerCase()))) {
final OptRun or2 = optRun;
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
// Double endValue = null;
// for (org.vcell.optimization.thrift.CopasiOptimizationParameter cop : or2.getOptProblem().getOptimizationMethod().getOptimizationParameterList()) {
// if (cop.getParamType().name().equals(CopasiOptimizationParameterType.Number_of_Generations.name())
// || cop.getParamType().name().equals(CopasiOptimizationParameterType.IterationLimit.name())){
// endValue = cop.getValue();
// break;
// }
// }
optSolverCallbacks.setEvaluation((int) or2.getOptResultSet().getNumFunctionEvaluations(), or2.getOptResultSet().objectiveFunction, 1.0, null, or2.getOptProblem().numberOfOptimizationRuns);
}
});
// }
}
if (status == OptRunStatus.Complete) {
System.out.println("job " + optIdHolder[0] + ": status " + status + " " + optRun.getOptResultSet().toString());
if (clientTaskStatusSupport != null) {
clientTaskStatusSupport.setProgress(100);
}
break;
}
if (status == OptRunStatus.Failed) {
throw new RuntimeException("optimization failed, message=" + optRun.statusMessage);
}
System.out.println("job " + optIdHolder[0] + ": status " + status);
}
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
}
}
if ((System.currentTimeMillis() - startTime) >= TIMEOUT_MS) {
throw new RuntimeException("optimization timed out.");
}
System.out.println("done with optimization");
OptResultSet optResultSet = optRun.getOptResultSet();
if (optResultSet == null) {
throw new RuntimeException("optResultSet is null, status is " + optRun.getStatusMessage());
}
if (optResultSet != null && optResultSet.getOptParameterValues() == null) {
throw new RuntimeException("getOptParameterValues is null, status is " + optRun.getStatusMessage());
}
if (clientTaskStatusSupport != null) {
clientTaskStatusSupport.setMessage("Done, getting results...");
}
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 (UserCancelException e) {
throw e;
} catch (Exception e) {
e.printStackTrace(System.out);
throw new OptimizationException(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
}
}
Aggregations