use of cbit.vcell.opt.OptimizationStatus in project vcell by virtualcell.
the class OptXmlReader method getOptRunResultSet.
private static OptRunResultSet getOptRunResultSet(Element optRunElement) throws DataConversionException {
double bestObjectFuncValue = optRunElement.getAttribute(OptXmlTags.OptSolverResultSetBestObjectiveFunction_Attr).getDoubleValue();
long numObjectFuncEvals = optRunElement.getAttribute(OptXmlTags.OptSolverResultSetNumObjectiveFunctionEvaluations_Attr).getLongValue();
String status = optRunElement.getAttributeValue(OptXmlTags.OptSolverResultSetStatus_Attr);
Attribute statusMessageAttr = optRunElement.getAttribute(OptXmlTags.OptSolverResultSetStatusMessage_Attr);
List<Element> paramElementList = optRunElement.getChildren(OptXmlTags.Parameter_Tag);
String[] parameterNames = new String[paramElementList.size()];
double[] parameterValues = new double[paramElementList.size()];
int count = 0;
for (Element parameterElement : paramElementList) {
parameterNames[count] = parameterElement.getAttributeValue(OptXmlTags.ParameterName_Attr);
parameterValues[count] = Double.parseDouble(parameterElement.getAttributeValue(OptXmlTags.ParameterBestValue_Attr));
count++;
}
int statusID = OptimizationStatus.statusFromXMLName(status);
String statusMessage = null;
if (statusMessageAttr != null) {
statusMessage = statusMessageAttr.getValue();
}
OptimizationStatus optimizationStatus = new OptimizationStatus(statusID, statusMessage);
OptRunResultSet optRun = new OptRunResultSet(parameterValues, new Double(bestObjectFuncValue), numObjectFuncEvals, optimizationStatus);
return optRun;
}
use of cbit.vcell.opt.OptimizationStatus 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 cbit.vcell.opt.OptimizationStatus 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 cbit.vcell.opt.OptimizationStatus in project vcell by virtualcell.
the class PowellOptimizationSolver method solve.
/**
* Insert the method's description here.
* Creation date: (3/5/00 11:15:15 PM)
* @return double[]
* @param optSpec cbit.vcell.opt.OptimizationSpec
* @exception java.io.IOException The exception description.
* @exception cbit.vcell.parser.ExpressionException The exception description.
* @exception cbit.vcell.opt.OptimizationException The exception description.
*/
public OptimizationResultSet solve(OptimizationSpec os, OptimizationSolverSpec optSolverSpec, OptSolverCallbacks optSolverCallbacks) throws java.io.IOException, cbit.vcell.parser.ExpressionException, OptimizationException {
final double power = 2.0;
final double MU_START = 0.1;
final double MU_END = 100000.0;
final double MU_STEP = 10.0;
AugmentedObjectiveFunction augmentedObjFunc = OptUtils.getAugmentedObjectiveFunction(os, power, MU_START, optSolverCallbacks);
//
// initialize starting guess
//
Parameter[] parameters = os.getParameters();
double[] parameterValues = new double[parameters.length];
for (int i = 0; i < parameters.length; i++) {
parameterValues[i] = parameters[i].getInitialGuess();
System.out.println("initial " + parameters[i].getName() + " = " + parameterValues[i]);
}
//
// get initial direction set
//
double[][] xi = new double[parameterValues.length][parameterValues.length];
for (int i = 0; i < parameterValues.length; i++) {
for (int j = 0; j < parameterValues.length; j++) {
xi[i][j] = (i == j ? 1.0 : 0.0);
}
}
final double ftol = 1e-6;
double fret = augmentedObjFunc.f(parameterValues);
PowellSolver powellSolver = new PowellSolver();
for (double mu = MU_START; mu <= MU_END; mu *= MU_STEP) {
try {
if (optSolverCallbacks.getStopRequested()) {
break;
}
augmentedObjFunc.setMu(mu);
fret = powellSolver.powell(parameterValues.length, parameterValues, xi, ftol, augmentedObjFunc);
System.out.println("mu=" + mu + ", function value=" + fret);
if (augmentedObjFunc.getPenalty(parameterValues) == 0.0) {
break;
}
} catch (Exception ex) {
ex.printStackTrace();
break;
}
}
OptimizationStatus optStatus = new OptimizationStatus(OptimizationStatus.NORMAL_TERMINATION, "Normal Termination");
ODESolverResultSet odeSolverResultSet = null;
if (optSolverCallbacks.getStopRequested()) {
optStatus = new OptimizationStatus(OptimizationStatus.STOPPED_BY_USER, "Stopped by user");
}
parameterValues = optSolverCallbacks.getBestEvaluation().getParameterValues();
fret = optSolverCallbacks.getBestEvaluation().getObjectiveFunctionValue();
odeSolverResultSet = optSolverCallbacks.getBestResultSet();
for (int i = 0; i < parameters.length; i++) {
System.out.println("final " + parameters[i].getName() + " = " + parameterValues[i]);
}
OptRunResultSet bestResult = new OptRunResultSet(parameterValues, new Double(fret), optSolverCallbacks.getEvaluationCount(), optStatus);
return new OptimizationResultSet(new OptSolverResultSet(os.getParameterNames(), bestResult), odeSolverResultSet);
}
Aggregations