Search in sources :

Example 56 with Parameter

use of cbit.vcell.opt.Parameter 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);
}
Also used : OptimizationResultSet(cbit.vcell.opt.OptimizationResultSet) OptimizationException(cbit.vcell.opt.OptimizationException) OptSolverResultSet(cbit.vcell.opt.OptSolverResultSet) Parameter(cbit.vcell.opt.Parameter) OptimizationStatus(cbit.vcell.opt.OptimizationStatus) ODESolverResultSet(cbit.vcell.solver.ode.ODESolverResultSet) OptRunResultSet(cbit.vcell.opt.OptSolverResultSet.OptRunResultSet)

Example 57 with Parameter

use of cbit.vcell.opt.Parameter in project vcell by virtualcell.

the class DisplayProfileLikelihoodPlotsOp method getSummaryFromProfileData.

// getting a profileSummary for each parameter that has acquired a profile likelihood distribution
ProfileSummaryData getSummaryFromProfileData(ProfileData profileData) {
    ArrayList<ProfileDataElement> profileElements = profileData.getProfileDataElements();
    int dataSize = profileElements.size();
    double[] paramValArray = new double[dataSize];
    double[] errorArray = new double[dataSize];
    if (dataSize > 0) {
        // profile likelihood curve
        String paramName = profileElements.get(0).getParamName();
        // find the parameter to locate the upper and lower bounds
        Parameter parameter = null;
        Parameter[] bestParameters = profileElements.get(0).getBestParameters();
        for (int i = 0; i < bestParameters.length; i++) {
            if (bestParameters[i] != null && bestParameters[i].getName().equals(paramName)) {
                parameter = bestParameters[i];
            }
        }
        // double logLowerBound = (lowerBound == 0)? 0: Math.log10(lowerBound);
        for (int i = 0; i < dataSize; i++) {
            paramValArray[i] = profileElements.get(i).getParameterValue();
            errorArray[i] = profileElements.get(i).getLikelihood();
        }
        PlotData dataPlot = new PlotData(paramValArray, errorArray);
        // get confidence interval line
        // make array copy in order to not change the data orders afte the sorting
        double[] paramValArrayCopy = new double[paramValArray.length];
        System.arraycopy(paramValArray, 0, paramValArrayCopy, 0, paramValArray.length);
        double[] errorArrayCopy = new double[errorArray.length];
        System.arraycopy(errorArray, 0, errorArrayCopy, 0, errorArray.length);
        DescriptiveStatistics paramValStat = DescriptiveStatistics.CreateBasicStatistics(paramValArrayCopy);
        DescriptiveStatistics errorStat = DescriptiveStatistics.CreateBasicStatistics(errorArrayCopy);
        double[] xArray = new double[2];
        double[][] yArray = new double[ConfidenceInterval.NUM_CONFIDENCE_LEVELS][2];
        // get confidence level plot lines
        xArray[0] = paramValStat.getMin() - (Math.abs(paramValStat.getMin()) * 0.2);
        xArray[1] = paramValStat.getMax() + (Math.abs(paramValStat.getMax()) * 0.2);
        for (int i = 0; i < ConfidenceInterval.NUM_CONFIDENCE_LEVELS; i++) {
            yArray[i][0] = errorStat.getMin() + ConfidenceInterval.DELTA_ALPHA_VALUE[i];
            yArray[i][1] = yArray[i][0];
        }
        PlotData confidence80Plot = new PlotData(xArray, yArray[ConfidenceInterval.IDX_DELTA_ALPHA_80]);
        PlotData confidence90Plot = new PlotData(xArray, yArray[ConfidenceInterval.IDX_DELTA_ALPHA_90]);
        PlotData confidence95Plot = new PlotData(xArray, yArray[ConfidenceInterval.IDX_DELTA_ALPHA_95]);
        PlotData confidence99Plot = new PlotData(xArray, yArray[ConfidenceInterval.IDX_DELTA_ALPHA_99]);
        // generate plot2D data
        Plot2D plots = new Plot2D(null, null, new String[] { "profile Likelihood Data", "80% confidence", "90% confidence", "95% confidence", "99% confidence" }, new PlotData[] { dataPlot, confidence80Plot, confidence90Plot, confidence95Plot, confidence99Plot }, new String[] { "Profile likelihood of " + paramName, "Log base 10 of " + paramName, "Profile Likelihood" }, new boolean[] { true, true, true, true, true });
        // get the best parameter for the minimal error
        int minErrIndex = -1;
        for (int i = 0; i < errorArray.length; i++) {
            if (errorArray[i] == errorStat.getMin()) {
                minErrIndex = i;
                break;
            }
        }
        double bestParamVal = Math.pow(10, paramValArray[minErrIndex]);
        // find confidence interval points
        ConfidenceInterval[] intervals = new ConfidenceInterval[ConfidenceInterval.NUM_CONFIDENCE_LEVELS];
        // half loop through the errors(left side curve)
        int[] smallLeftIdx = new int[ConfidenceInterval.NUM_CONFIDENCE_LEVELS];
        int[] bigLeftIdx = new int[ConfidenceInterval.NUM_CONFIDENCE_LEVELS];
        for (int i = 0; i < ConfidenceInterval.NUM_CONFIDENCE_LEVELS; i++) {
            smallLeftIdx[i] = -1;
            bigLeftIdx[i] = -1;
            for (// loop from bigger error to smaller error
            int j = 1; // loop from bigger error to smaller error
            j < minErrIndex + 1; // loop from bigger error to smaller error
            j++) {
                if ((errorArray[j] < (errorStat.getMin() + ConfidenceInterval.DELTA_ALPHA_VALUE[i])) && (errorArray[j - 1] > (errorStat.getMin() + ConfidenceInterval.DELTA_ALPHA_VALUE[i]))) {
                    smallLeftIdx[i] = j - 1;
                    bigLeftIdx[i] = j;
                    break;
                }
            }
        }
        // another half loop through the errors(right side curve)
        int[] smallRightIdx = new int[ConfidenceInterval.NUM_CONFIDENCE_LEVELS];
        int[] bigRightIdx = new int[ConfidenceInterval.NUM_CONFIDENCE_LEVELS];
        for (int i = 0; i < ConfidenceInterval.NUM_CONFIDENCE_LEVELS; i++) {
            smallRightIdx[i] = -1;
            bigRightIdx[i] = -1;
            for (// loop from bigger error to smaller error
            int j = (minErrIndex + 1); // loop from bigger error to smaller error
            j < errorArray.length; // loop from bigger error to smaller error
            j++) {
                if ((errorStat.getMin() + ConfidenceInterval.DELTA_ALPHA_VALUE[i]) < errorArray[j] && (errorStat.getMin() + ConfidenceInterval.DELTA_ALPHA_VALUE[i]) > errorArray[j - 1]) {
                    smallRightIdx[i] = j - 1;
                    bigRightIdx[i] = j;
                    break;
                }
            }
        }
        // calculate intervals
        for (int i = 0; i < ConfidenceInterval.NUM_CONFIDENCE_LEVELS; i++) {
            double lowerBound = Double.NEGATIVE_INFINITY;
            boolean bLowerBoundOpen = true;
            double upperBound = Double.POSITIVE_INFINITY;
            boolean bUpperBoundOpen = true;
            if (// no lower bound
            smallLeftIdx[i] == -1 && bigLeftIdx[i] == -1) {
                lowerBound = parameter.getLowerBound();
                bLowerBoundOpen = false;
            } else if (// there is a lower bound
            smallLeftIdx[i] != -1 && bigLeftIdx[i] != -1) {
                // x=x1+(x2-x1)*(y-y1)/(y2-y1);
                double x1 = paramValArray[smallLeftIdx[i]];
                double x2 = paramValArray[bigLeftIdx[i]];
                double y = errorStat.getMin() + ConfidenceInterval.DELTA_ALPHA_VALUE[i];
                double y1 = errorArray[smallLeftIdx[i]];
                double y2 = errorArray[bigLeftIdx[i]];
                lowerBound = x1 + (x2 - x1) * (y - y1) / (y2 - y1);
                lowerBound = Math.pow(10, lowerBound);
                bLowerBoundOpen = false;
            }
            if (// no upper bound
            smallRightIdx[i] == -1 && bigRightIdx[i] == -1) {
                upperBound = parameter.getUpperBound();
                bUpperBoundOpen = false;
            } else if (// there is a upper bound
            smallRightIdx[i] != -1 && bigRightIdx[i] != -1) {
                // x=x1+(x2-x1)*(y-y1)/(y2-y1);
                double x1 = paramValArray[smallRightIdx[i]];
                double x2 = paramValArray[bigRightIdx[i]];
                double y = errorStat.getMin() + ConfidenceInterval.DELTA_ALPHA_VALUE[i];
                double y1 = errorArray[smallRightIdx[i]];
                double y2 = errorArray[bigRightIdx[i]];
                upperBound = x1 + (x2 - x1) * (y - y1) / (y2 - y1);
                upperBound = Math.pow(10, upperBound);
                bUpperBoundOpen = false;
            }
            intervals[i] = new ConfidenceInterval(lowerBound, bLowerBoundOpen, upperBound, bUpperBoundOpen);
        }
        return new ProfileSummaryData(plots, bestParamVal, intervals, paramName);
    }
    return null;
}
Also used : PlotData(cbit.plot.PlotData) DescriptiveStatistics(org.vcell.util.DescriptiveStatistics) ProfileSummaryData(org.vcell.optimization.ProfileSummaryData) ProfileDataElement(org.vcell.optimization.ProfileDataElement) Parameter(cbit.vcell.opt.Parameter) Plot2D(cbit.plot.Plot2D) ConfidenceInterval(org.vcell.optimization.ConfidenceInterval)

Example 58 with Parameter

use of cbit.vcell.opt.Parameter in project vcell by virtualcell.

the class OptModelParamPanel method runAndSetBestParameters.

public void runAndSetBestParameters() throws Exception {
    Parameter[] parameters = optContext.getParameters();
    final Parameter[] inParameters = new Parameter[parameters.length];
    double[] values = getCurrentParameterValues();
    for (int i = 0; i < inParameters.length; i++) {
        Parameter p = parameters[i];
        inParameters[i] = new Parameter(p.getName(), p.getLowerBound(), p.getUpperBound(), p.getScale(), values[i]);
    }
    AsynchClientTask optTask = new AsynchClientTask("Running optimization ...", AsynchClientTask.TASKTYPE_NONSWING_BLOCKING) {

        public void run(Hashtable<String, Object> hashTable) throws Exception {
            String[] outParaNames = new String[inParameters.length];
            double[] outParaValues = new double[inParameters.length];
            OptContextSolver.estimate(optContext, inParameters, outParaNames, outParaValues);
            hashTable.put("outParaValues", outParaValues);
        }
    };
    AsynchClientTask showResultTask = new AsynchClientTask("Running optimization ...", AsynchClientTask.TASKTYPE_SWING_BLOCKING) {

        public void run(Hashtable<String, Object> hashTable) throws Exception {
            double[] outParaVales = (double[]) hashTable.get("outParaValues");
            setParameterValues(outParaVales);
            plotDerivedSimulationResults();
        }
    };
    // dispatch
    ClientTaskDispatcher.dispatch(OptModelParamPanel.this, new Hashtable<String, Object>(), new AsynchClientTask[] { optTask, showResultTask }, false, false, null, true);
}
Also used : AsynchClientTask(cbit.vcell.client.task.AsynchClientTask) Hashtable(java.util.Hashtable) Parameter(cbit.vcell.opt.Parameter) WorkflowParameter(org.vcell.workflow.WorkflowParameter)

Example 59 with Parameter

use of cbit.vcell.opt.Parameter in project vcell by virtualcell.

the class RunProfileLikelihoodGeneralOp method getBestParameters.

private Parameter[] getBestParameters(OptContext optContext, Parameter[] inParams, Parameter fixedParam) throws Exception {
    int numFixedParam = (fixedParam == null) ? 0 : 1;
    // return to the caller, parameter should be a whole set including the fixed parameter
    Parameter[] outputParams = new Parameter[inParams.length + numFixedParam];
    // send to optimizer
    String[] outParaNames = new String[inParams.length];
    // send to optimizer
    double[] outParaVals = new double[inParams.length];
    // optimization
    leastError = OptContextSolver.estimate(optContext, inParams, outParaNames, outParaVals);
    // get results as Parameters (take fixed parameter into account)
    for (int fullParamIndex = 0; fullParamIndex < optContext.getParameters().length; fullParamIndex++) {
        Parameter p = optContext.getParameters()[fullParamIndex];
        for (int estParamIndex = 0; estParamIndex < outParaNames.length; estParamIndex++) {
            if (outParaNames[estParamIndex].equals(p.getName())) {
                double value = outParaVals[estParamIndex];
                if (value > p.getUpperBound()) {
                    value = p.getUpperBound();
                } else if (value < p.getLowerBound()) {
                    value = p.getLowerBound();
                }
                outputParams[fullParamIndex] = new Parameter(p.getName(), p.getLowerBound(), p.getUpperBound(), 1.0, value);
            }
        }
        // add fixed parameter to the best parameter output to make a whole set
        if (fixedParam != null && p.getName().equals(fixedParam.getName())) {
            outputParams[fullParamIndex] = fixedParam;
        }
    }
    // }
    return outputParams;
}
Also used : Parameter(cbit.vcell.opt.Parameter)

Aggregations

Parameter (cbit.vcell.opt.Parameter)59 FRAPStudy (cbit.vcell.microscopy.FRAPStudy)11 Hashtable (java.util.Hashtable)10 AsynchClientTask (cbit.vcell.client.task.AsynchClientTask)9 ProfileDataElement (org.vcell.optimization.ProfileDataElement)8 Expression (cbit.vcell.parser.Expression)7 File (java.io.File)7 FRAPModel (cbit.vcell.microscopy.FRAPModel)6 ProfileData (org.vcell.optimization.ProfileData)6 OptimizationResultSet (cbit.vcell.opt.OptimizationResultSet)5 Element (org.jdom.Element)5 OptSolverResultSet (cbit.vcell.opt.OptSolverResultSet)4 OptimizationSpec (cbit.vcell.opt.OptimizationSpec)4 SimpleReferenceData (cbit.vcell.opt.SimpleReferenceData)4 ArrayList (java.util.ArrayList)4 ROI (cbit.vcell.VirtualMicroscopy.ROI)3 Dimension (java.awt.Dimension)3 Plot2D (cbit.plot.Plot2D)2 PlotData (cbit.plot.PlotData)2 BioModel (cbit.vcell.biomodel.BioModel)2