Search in sources :

Example 6 with RealInterval

use of net.sourceforge.interval.ia_math.RealInterval in project vcell by virtualcell.

the class ASTNotNode method bind.

public void bind(SymbolTable symbolTable) throws ExpressionBindingException {
    super.bind(symbolTable);
    // either true or false
    setInterval(new RealInterval(0.0, 1.0), null);
}
Also used : RealInterval(net.sourceforge.interval.ia_math.RealInterval)

Example 7 with RealInterval

use of net.sourceforge.interval.ia_math.RealInterval in project vcell by virtualcell.

the class StructureMapping method gatherIssues.

/**
 * Insert the method's description here.
 * Creation date: (11/1/2005 9:57:23 AM)
 * @param issueVector java.util.Vector
 */
public void gatherIssues(IssueContext issueContext, List<Issue> issueVector) {
    // size parameter must be set to non zero value for new ode, and all stoch simulations.
    if (simulationContext != null) {
        Parameter sizeParam = null;
        if (simulationContext.getGeometry().getDimension() == 0) {
            sizeParam = getSizeParameter();
        } else {
            sizeParam = getUnitSizeParameter();
        }
        if (sizeParam != null) {
            if (sizeParam.getExpression() == null) {
                if (!simulationContext.getGeometryContext().isAllVolFracAndSurfVolSpecified()) {
                    issueVector.add(new Issue(this, issueContext, IssueCategory.StructureMappingSizeParameterNotSet, "Size parameter is not set.", Issue.SEVERITY_ERROR));
                }
            } else {
                try {
                    double val = sizeParam.getExpression().evaluateConstant();
                    if (val <= 0) {
                        issueVector.add(new Issue(this, issueContext, IssueCategory.StructureMappingSizeParameterNotPositive, "Size parameter is not positive.", Issue.SEVERITY_ERROR));
                    }
                } catch (ExpressionException e) {
                    e.printStackTrace();
                    issueVector.add(new Issue(this, issueContext, IssueCategory.StructureMappingSizeParameterNotConstant, "Size parameter is not a constant.", Issue.SEVERITY_ERROR));
                }
            }
        }
    }
    // 
    for (int i = 0; fieldParameters != null && i < fieldParameters.length; i++) {
        RealInterval simpleBounds = parameterBounds[fieldParameters[i].getRole()];
        if (simpleBounds != null) {
            String parmName = fieldParameters[i].getNameScope().getName() + "." + fieldParameters[i].getName();
            issueVector.add(new SimpleBoundsIssue(fieldParameters[i], issueContext, simpleBounds, "parameter " + parmName + ": must be within " + simpleBounds.toString()));
        }
    }
    if (geometryClass == null) {
        issueVector.add(new Issue(this, issueContext, IssueCategory.StructureNotMapped, getStructure().getTypeName() + " " + getStructure().getName() + " is not mapped to a geometry subdomain.", Issue.SEVERITY_WARNING));
    }
    if (geometryClass != null && simulationContext.getGeometryContext().getGeometry().getDimension() > 0) {
        detectMappingConflictIssues(issueContext, issueVector, this);
    }
}
Also used : SimpleBoundsIssue(cbit.vcell.model.SimpleBoundsIssue) Issue(org.vcell.util.Issue) SimpleBoundsIssue(cbit.vcell.model.SimpleBoundsIssue) Parameter(cbit.vcell.model.Parameter) RealInterval(net.sourceforge.interval.ia_math.RealInterval) ExpressionException(cbit.vcell.parser.ExpressionException)

Example 8 with RealInterval

use of net.sourceforge.interval.ia_math.RealInterval in project vcell by virtualcell.

the class ConstraintSolver method narrow.

/**
 * Insert the method's description here.
 * Creation date: (6/25/2001 9:45:31 PM)
 */
public boolean narrow() throws ExpressionException {
    RealInterval[] prevValues = new RealInterval[fieldIntervals.length];
    for (int i = 0; i < prevValues.length; i++) {
        prevValues[i] = (RealInterval) fieldIntervals[i].clone();
    }
    RealInterval[] values = new RealInterval[fieldIntervals.length];
    for (int i = 0; i < values.length; i++) {
        values[i] = (RealInterval) fieldIntervals[i].clone();
    }
    // System.out.println(constraintContainerImpl.toECLiPSe());
    // 
    // clear inconsistent flag for all constraints
    // 
    GeneralConstraint[] generalConstraints = constraintContainerImpl.getGeneralConstraints();
    for (int i = 0; i < generalConstraints.length; i++) {
        constraintContainerImpl.setConsistent(generalConstraints[i], true);
    }
    boolean bValuesChanged = true;
    boolean bValuesEverChanged = false;
    while (bValuesChanged) {
        for (int i = 0; i < expressionList.size(); i++) {
            Expression exp = (Expression) expressionList.elementAt(i);
            if (!exp.narrow(values)) {
                // 
                // set failed constraint to inconsistent
                // 
                int genConstraintIndex = 0;
                for (int j = 0; j < generalConstraints.length; j++) {
                    if (constraintContainerImpl.getActive(generalConstraints[j])) {
                        if (genConstraintIndex == i) {
                            constraintContainerImpl.setConsistent(generalConstraints[genConstraintIndex], false);
                        }
                    }
                    genConstraintIndex++;
                }
                // should we have this??
                setIntervals(prevValues);
                return false;
            }
        }
        bValuesChanged = false;
        for (int i = 0; i < values.length; i++) {
            if (!prevValues[i].equals(values[i])) {
                bValuesChanged = true;
                bValuesEverChanged = true;
                prevValues[i] = (RealInterval) values[i].clone();
            }
        }
    }
    if (bValuesEverChanged) {
        setIntervals(values);
    }
    return true;
}
Also used : Expression(cbit.vcell.parser.Expression) RealInterval(net.sourceforge.interval.ia_math.RealInterval)

Example 9 with RealInterval

use of net.sourceforge.interval.ia_math.RealInterval in project vcell by virtualcell.

the class ParameterContext method gatherIssues.

public void gatherIssues(IssueContext issueContext, List<Issue> issueList, ParameterRoleEnum userDefinedRole) {
    // 
    for (int i = 0; fieldUnresolvedParameters != null && i < fieldUnresolvedParameters.length; i++) {
        issueList.add(new Issue(fieldUnresolvedParameters[i], issueContext, IssueCategory.UnresolvedParameter, "Unresolved parameter '" + fieldUnresolvedParameters[i].getName(), Issue.SEVERITY_ERROR));
    }
    // 
    for (int i = 0; fieldParameters != null && i < fieldParameters.length; i++) {
        if (fieldParameters[i].getRole() == userDefinedRole) {
            try {
                if (!isReferenced(fieldParameters[i], 0)) {
                    issueList.add(new Issue(fieldParameters[i], issueContext, IssueCategory.KineticsUnreferencedParameter, "Unreferenced Kinetic Parameter '" + fieldParameters[i].getName(), Issue.SEVERITY_WARNING));
                }
            } catch (ExpressionException e) {
                issueList.add(new Issue(fieldParameters[i], issueContext, IssueCategory.KineticsExpressionError, "error resolving expression " + e.getMessage(), Issue.SEVERITY_WARNING));
            }
        }
    }
    // 
    if (fieldParameters != null) {
        for (LocalParameter parameter : this.fieldParameters) {
            if (parameter.getExpression() == null) {
                issueList.add(new Issue(parameter, issueContext, IssueCategory.KineticsExpressionMissing, "expression is missing", Issue.SEVERITY_INFO));
            } else {
                Expression exp = parameter.getExpression();
                String[] symbols = exp.getSymbols();
                String issueMessagePrefix = "parameter '" + parameter.getName() + "' ";
                if (symbols != null) {
                    for (int j = 0; j < symbols.length; j++) {
                        SymbolTableEntry ste = exp.getSymbolBinding(symbols[j]);
                        if (ste instanceof LocalProxyParameter) {
                            ste = ((LocalProxyParameter) ste).getTarget();
                        }
                        if (ste == null) {
                            issueList.add(new Issue(parameter, issueContext, IssueCategory.KineticsExpressionUndefinedSymbol, issueMessagePrefix + "references undefined symbol '" + symbols[j] + "'", Issue.SEVERITY_ERROR));
                        // } else if (ste instanceof SpeciesContext) {
                        // if (!getReactionStep().getModel().contains((SpeciesContext)ste)) {
                        // issueList.add(new Issue(parameter,issueContext,IssueCategory.KineticsExpressionUndefinedSymbol, issueMessagePrefix + "references undefined species '"+symbols[j]+"'",Issue.SEVERITY_ERROR));
                        // }
                        // if (reactionStep.countNumReactionParticipants((SpeciesContext)ste) == 0){
                        // issueList.add(new Issue(parameter,issueContext,IssueCategory.KineticsExpressionNonParticipantSymbol, issueMessagePrefix + "references species context '"+symbols[j]+"', but it is not a reactant/product/catalyst of this reaction",Issue.SEVERITY_WARNING));
                        // }
                        // } else if (ste instanceof ModelParameter) {
                        // if (!getReactionStep().getModel().contains((ModelParameter)ste)) {
                        // issueList.add(new Issue(parameter,issueContext,IssueCategory.KineticsExpressionUndefinedSymbol, issueMessagePrefix + "references undefined global parameter '"+symbols[j]+"'",Issue.SEVERITY_ERROR));
                        // }
                        }
                    }
                }
            }
        }
        // looking for local param which masks a global and issueing a warning
        for (LocalParameter parameter : fieldParameters) {
            String name = parameter.getName();
            SymbolTableEntry ste = nameScope.getExternalEntry(name, this);
            String steName;
            if (ste != null) {
                if (ste instanceof Displayable) {
                    steName = ((Displayable) ste).getDisplayType() + " " + ste.getName();
                } else {
                    steName = ste.getClass().getSimpleName() + " " + ste.getName();
                }
                String msg = steName + " is overriden by a local parameter " + name;
                issueList.add(new Issue(parameter, issueContext, IssueCategory.Identifiers, msg, Issue.SEVERITY_WARNING));
            }
        }
    }
    try {
        // 
        // determine unit consistency for each expression
        // 
        VCUnitSystem unitSystem = unitSystemProvider.getUnitSystem();
        VCUnitEvaluator unitEvaluator = new VCUnitEvaluator(unitSystem);
        for (int i = 0; i < fieldParameters.length; i++) {
            if (fieldParameters[i].getExpression() == null) {
                continue;
            }
            try {
                VCUnitDefinition paramUnitDef = fieldParameters[i].getUnitDefinition();
                VCUnitDefinition expUnitDef = unitEvaluator.getUnitDefinition(fieldParameters[i].getExpression());
                if (paramUnitDef == null) {
                    issueList.add(new Issue(fieldParameters[i], issueContext, IssueCategory.Units, "defined unit is null", Issue.SEVERITY_WARNING));
                } else if (paramUnitDef.isTBD()) {
                    issueList.add(new Issue(fieldParameters[i], issueContext, IssueCategory.Units, "undefined unit " + unitSystem.getInstance_TBD().getSymbol(), Issue.SEVERITY_WARNING));
                } else if (expUnitDef == null) {
                    issueList.add(new Issue(fieldParameters[i], issueContext, IssueCategory.Units, "computed unit is null", Issue.SEVERITY_WARNING));
                } else if (paramUnitDef.isTBD() || (!paramUnitDef.isEquivalent(expUnitDef) && !expUnitDef.isTBD())) {
                    issueList.add(new Issue(fieldParameters[i], issueContext, IssueCategory.Units, "inconsistent units, defined=[" + fieldParameters[i].getUnitDefinition().getSymbol() + "], computed=[" + expUnitDef.getSymbol() + "]", Issue.SEVERITY_WARNING));
                }
            } catch (VCUnitException e) {
                issueList.add(new Issue(fieldParameters[i], issueContext, IssueCategory.Units, e.getMessage(), Issue.SEVERITY_WARNING));
            } catch (ExpressionException e) {
                issueList.add(new Issue(fieldParameters[i], issueContext, IssueCategory.Units, e.getMessage(), Issue.SEVERITY_WARNING));
            }
        }
    } catch (Throwable e) {
        issueList.add(new Issue(parameterPolicy.getIssueSource(), issueContext, IssueCategory.Units, "unexpected exception: " + e.getMessage(), Issue.SEVERITY_INFO));
    }
    // 
    for (int i = 0; i < fieldParameters.length; i++) {
        RealInterval simpleBounds = parameterPolicy.getConstraintBounds(fieldParameters[i].getRole());
        if (simpleBounds != null) {
            String parmName = fieldParameters[i].getName();
            issueList.add(new SimpleBoundsIssue(fieldParameters[i], issueContext, simpleBounds, "parameter " + parmName + ": must be within " + simpleBounds.toString()));
        }
    }
}
Also used : Displayable(org.vcell.util.Displayable) VCUnitSystem(cbit.vcell.units.VCUnitSystem) SimpleBoundsIssue(cbit.vcell.model.SimpleBoundsIssue) Issue(org.vcell.util.Issue) SimpleBoundsIssue(cbit.vcell.model.SimpleBoundsIssue) RealInterval(net.sourceforge.interval.ia_math.RealInterval) ExpressionException(cbit.vcell.parser.ExpressionException) VCUnitException(cbit.vcell.units.VCUnitException) VCUnitEvaluator(cbit.vcell.parser.VCUnitEvaluator) SymbolTableEntry(cbit.vcell.parser.SymbolTableEntry) VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) Expression(cbit.vcell.parser.Expression)

Example 10 with RealInterval

use of net.sourceforge.interval.ia_math.RealInterval in project vcell by virtualcell.

the class ExpressionTest method testEvaluateNarrowing.

/**
 * This method was created in VisualAge.
 */
public static void testEvaluateNarrowing(int numTrials, int depth, long seed) {
    try {
        java.util.Random random = new java.util.Random(seed);
        for (int i = 0; i < numTrials; i++) {
            Expression constraint = ExpressionUtils.generateExpression(random, depth, true);
            String[] symbols = constraint.getSymbols();
            if (symbols == null || symbols.length < 1) {
                continue;
            }
            RealInterval[] initialItervals = new RealInterval[symbols.length];
            RealInterval[] values = new RealInterval[symbols.length];
            RealInterval[] prevValues = new RealInterval[symbols.length];
            for (int j = 0; j < initialItervals.length; j++) {
                double middle = random.nextGaussian();
                double size = random.nextGaussian() * 10;
                initialItervals[j] = new RealInterval(middle - Math.abs(size), middle + Math.abs(size));
                values[j] = new RealInterval(initialItervals[j].lo(), initialItervals[j].hi());
                prevValues[j] = new RealInterval(initialItervals[j].lo(), initialItervals[j].hi());
            }
            SimpleSymbolTable symbolTable = new SimpleSymbolTable(symbols);
            constraint.bindExpression(symbolTable);
            System.out.println(i + ") narrowing '" + constraint + "'");
            boolean bValuesChanged = true;
            boolean bValuesEverChanged = true;
            boolean bConstraintSatisfied = true;
            final int maxIterations = 100;
            int iteration = 0;
            while (bValuesChanged && bConstraintSatisfied && iteration < maxIterations) {
                iteration++;
                try {
                    bConstraintSatisfied = constraint.narrow(values);
                } catch (Exception e) {
                    e.printStackTrace(System.out);
                    bConstraintSatisfied = false;
                }
                if (bConstraintSatisfied == false) {
                    System.out.println("     constraint failed");
                    continue;
                }
                bValuesChanged = false;
                System.out.print("     iteration " + iteration);
                for (int j = 0; j < values.length; j++) {
                    if (!prevValues[j].equals(values[j])) {
                        bValuesChanged = true;
                        bValuesEverChanged = true;
                        prevValues[j] = (RealInterval) values[j].clone();
                    }
                    System.out.print(", " + symbols[j] + " = " + values[j].toString());
                }
                System.out.println();
            }
            boolean bNarrowedIsSubset = true;
            boolean bNarrowedIsDifferent = false;
            if (bConstraintSatisfied) {
                // 
                for (int j = 0; j < values.length; j++) {
                    if (values[j].lo() >= initialItervals[j].lo() && values[j].lo() <= initialItervals[j].hi() && values[j].hi() >= initialItervals[j].lo() && values[j].hi() <= initialItervals[j].hi()) {
                        if (!values[j].equals(initialItervals[j])) {
                            bNarrowedIsDifferent = true;
                        }
                        System.out.println("     ------narrowed interval " + symbols[j] + " = " + values[j] + " (" + initialItervals[j] + ") OK");
                    } else {
                        System.out.println("     ------narrowed interval " + symbols[j] + " = " + values[j] + " (" + initialItervals[j] + ") <<<<< B A D>>>>>>");
                        bNarrowedIsSubset = false;
                    }
                }
                if (bNarrowedIsSubset && bNarrowedIsDifferent) {
                    // 
                    // verify that samples taken from the removed portions of the initial interval all fail.
                    // 
                    int failedCount = 0;
                    int successCount = 0;
                    int exceptionCount = 0;
                    final int maxExceptionCount = 3000;
                    double[] samples = new double[values.length];
                    while ((failedCount + successCount) < 300 && exceptionCount < maxExceptionCount) {
                        boolean bSamplesOutsideNarrowedIntervals = false;
                        for (int k = 0; k < initialItervals.length; k++) {
                            samples[k] = sample(initialItervals[k], random);
                            if (samples[k] < values[k].lo() || samples[k] > values[k].hi()) {
                                bSamplesOutsideNarrowedIntervals = true;
                            }
                        }
                        try {
                            if (bSamplesOutsideNarrowedIntervals) {
                                if (constraint.evaluateVector(samples) == 1.0) {
                                    failedCount++;
                                    System.out.print("     ---NARROWING FAILED, removed point was in the solution");
                                    for (int m = 0; m < values.length; m++) {
                                        System.out.print(", " + symbols[m] + " = " + samples[m]);
                                    }
                                    System.out.println("");
                                } else {
                                    successCount++;
                                }
                            }
                        } catch (FunctionDomainException e) {
                            exceptionCount++;
                            if (exceptionCount >= maxExceptionCount) {
                                System.out.println(e.toString());
                            }
                        }
                    }
                    if (failedCount > 0 || successCount == 0) {
                        System.out.println("     --<<BAD>>--  " + failedCount + " evaluations out of " + (failedCount + successCount) + " failed, " + exceptionCount + " exceptions");
                    } else {
                        System.out.println("     --<<GOOD>>-- " + failedCount + " evaluations out of " + (failedCount + successCount) + " failed, " + exceptionCount + " exceptions");
                    }
                }
            }
        }
    } catch (Throwable e) {
        e.printStackTrace(System.out);
    }
}
Also used : RealInterval(net.sourceforge.interval.ia_math.RealInterval)

Aggregations

RealInterval (net.sourceforge.interval.ia_math.RealInterval)25 Expression (cbit.vcell.parser.Expression)7 ExpressionException (cbit.vcell.parser.ExpressionException)4 Issue (org.vcell.util.Issue)4 AbstractConstraint (cbit.vcell.constraints.AbstractConstraint)3 ConstraintContainerImpl (cbit.vcell.constraints.ConstraintContainerImpl)3 GeneralConstraint (cbit.vcell.constraints.GeneralConstraint)3 SimpleBounds (cbit.vcell.constraints.SimpleBounds)3 SimpleBoundsIssue (cbit.vcell.model.SimpleBoundsIssue)3 SpeciesContextSpecParameter (cbit.vcell.mapping.SpeciesContextSpec.SpeciesContextSpecParameter)2 Kinetics (cbit.vcell.model.Kinetics)2 KineticsParameter (cbit.vcell.model.Kinetics.KineticsParameter)2 MassActionKinetics (cbit.vcell.model.MassActionKinetics)2 SymbolTableEntry (cbit.vcell.parser.SymbolTableEntry)2 VCUnitEvaluator (cbit.vcell.parser.VCUnitEvaluator)2 VCUnitDefinition (cbit.vcell.units.VCUnitDefinition)2 VCUnitException (cbit.vcell.units.VCUnitException)2 ConstraintSolver (cbit.vcell.constraints.ConstraintSolver)1 SubVolume (cbit.vcell.geometry.SubVolume)1 SurfaceClass (cbit.vcell.geometry.SurfaceClass)1