use of cbit.vcell.matrix.MatrixException in project vcell by virtualcell.
the class FastSystemAnalyzer method refreshInvarianceMatrix.
/**
*/
private void refreshInvarianceMatrix() throws MathException, ExpressionException {
//
// the invariance's are expressed in matrix form
//
// |a a a a a -1 0 0| |x1| |0|
// |a a a a a 0 -1 0| * |x2| = |0|
// |a a a a a 0 0 -1| |x3| |0|
// |x4|
// |x5|
// |c1|
// |c2|
// |c3|
//
Variable[] vars = new Variable[fastVarList.size()];
fastVarList.copyInto(vars);
int numVars = fastVarList.size();
int rows = fastSystem.getNumFastInvariants();
int cols = numVars + fastSystem.getNumFastInvariants();
RationalExpMatrix matrix = new RationalExpMatrix(rows, cols);
Enumeration<FastInvariant> fastInvariantsEnum = fastSystem.getFastInvariants();
for (int i = 0; i < rows && fastInvariantsEnum.hasMoreElements(); i++) {
FastInvariant fi = (FastInvariant) fastInvariantsEnum.nextElement();
Expression function = fi.getFunction();
for (int j = 0; j < numVars; j++) {
Variable var = (Variable) fastVarList.elementAt(j);
Expression exp = function.differentiate(var.getName());
exp.bindExpression(null);
exp = exp.flatten();
RationalExp coeffRationalExp = RationalExpUtils.getRationalExp(exp);
matrix.set_elem(i, j, coeffRationalExp);
}
matrix.set_elem(i, numVars + i, -1);
}
// Print
System.out.println("origMatrix");
matrix.show();
//
// gaussian elimination on the matrix give the following representation
// note that some column pivoting (variable re-ordering) is sometimes required to
// determine N-r dependent vars
//
// |10i0iccc|
// |01i0iccc| where (c)'s are the coefficients for constants of invariances
// |00i1iccc| (i)'s are the coefficients for dependent vars in terms of independent vars
//
// Print
System.out.println("reducedMatrix");
if (rows > 0) {
try {
matrix.gaussianElimination(new RationalExpMatrix(rows, rows));
} catch (MatrixException e) {
e.printStackTrace(System.out);
throw new MathException(e.getMessage());
}
}
matrix.show();
for (int i = 0; i < vars.length; i++) {
System.out.print(vars[i].getName() + " ");
}
System.out.println("");
//
for (int i = 0; i < rows; i++) {
for (int j = 0; j < rows; j++) {
RationalExp rexp = matrix.get(i, j).simplify();
matrix.set_elem(i, j, rexp);
}
}
for (int i = 0; i < rows; i++) {
//
if (!matrix.get(i, i).isConstant() || matrix.get(i, i).getConstant().doubleValue() != 1) {
for (int j = i + 1; j < numVars; j++) {
if (matrix.get(i, j).isConstant() && matrix.get(i, j).getConstant().doubleValue() == 1.0) {
for (int ii = 0; ii < rows; ii++) {
RationalExp temp = matrix.get(ii, i);
matrix.set_elem(ii, i, matrix.get(ii, j));
matrix.set_elem(ii, j, temp);
}
Variable tempVar = vars[i];
vars[i] = vars[j];
vars[j] = tempVar;
break;
}
}
}
}
// Print
for (int i = 0; i < vars.length; i++) {
System.out.print(vars[i].getName() + " ");
}
System.out.println("");
matrix.show();
//
// separate into dependent and indepent variables, and chop off identity matrix (left N-r columns)
//
// T |iiccc| T
// [x1 x2 x4] = -1 * |iiccc| * [x3 x5 c1 c2 c3]
// |iiccc|
//
//
int numInvariants = fastSystem.getNumFastInvariants();
dependentVarList.removeAllElements();
for (int i = 0; i < numInvariants; i++) {
dependentVarList.addElement(vars[i]);
}
independentVarList.removeAllElements();
for (int i = numInvariants; i < vars.length; i++) {
independentVarList.addElement(vars[i]);
}
int new_cols = independentVarList.size() + numInvariants;
dependencyMatrix = new RationalExpMatrix(rows, new_cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < new_cols; j++) {
RationalExp rexp = matrix.get(i, j + dependentVarList.size()).simplify().minus();
dependencyMatrix.set_elem(i, j, rexp);
}
}
// Print
System.out.println("\n\nDEPENDENCY MATRIX");
dependencyMatrix.show();
System.out.print("dependent vars: ");
for (int i = 0; i < dependentVarList.size(); i++) {
System.out.print(((Variable) dependentVarList.elementAt(i)).getName() + " ");
}
System.out.println("");
System.out.print("independent vars: ");
for (int i = 0; i < independentVarList.size(); i++) {
System.out.print(((Variable) independentVarList.elementAt(i)).getName() + " ");
}
System.out.println("");
}
use of cbit.vcell.matrix.MatrixException in project vcell by virtualcell.
the class ModelOptimizationMapping method computeOptimizationSpec.
/**
* Insert the method's description here.
* Creation date: (8/22/2005 9:26:52 AM)
* @return cbit.vcell.opt.OptimizationSpec
* @param modelOptimizationSpec cbit.vcell.modelopt.ModelOptimizationSpec
*/
MathSymbolMapping computeOptimizationSpec() throws MathException, MappingException {
if (getModelOptimizationSpec().getReferenceData() == null) {
System.out.println("no referenced data defined");
return null;
}
OptimizationSpec optSpec = new OptimizationSpec();
optSpec.setComputeProfileDistributions(modelOptimizationSpec.isComputeProfileDistributions());
parameterMappings = null;
//
// get original MathDescription (later to be substituted for local/global parameters).
//
SimulationContext simContext = modelOptimizationSpec.getSimulationContext();
MathMapping mathMapping = simContext.createNewMathMapping();
MathDescription origMathDesc = null;
mathSymbolMapping = null;
try {
origMathDesc = mathMapping.getMathDescription();
mathSymbolMapping = mathMapping.getMathSymbolMapping();
} catch (MatrixException e) {
e.printStackTrace(System.out);
throw new MappingException(e.getMessage());
} catch (ModelException e) {
e.printStackTrace(System.out);
throw new MappingException(e.getMessage());
} catch (ExpressionException e) {
e.printStackTrace(System.out);
throw new MathException(e.getMessage());
}
//
// create objective function (mathDesc and data)
//
ReferenceData referenceData = getRemappedReferenceData(mathMapping);
if (referenceData == null) {
throw new RuntimeException("no referenced data defined");
}
//
// get parameter mappings
//
ParameterMappingSpec[] parameterMappingSpecs = modelOptimizationSpec.getParameterMappingSpecs();
Vector<ParameterMapping> parameterMappingList = new Vector<ParameterMapping>();
Variable[] allVars = (Variable[]) BeanUtils.getArray(origMathDesc.getVariables(), Variable.class);
for (int i = 0; i < parameterMappingSpecs.length; i++) {
cbit.vcell.model.Parameter modelParameter = parameterMappingSpecs[i].getModelParameter();
String mathSymbol = null;
Variable mathVariable = null;
if (mathSymbolMapping != null) {
Variable variable = mathSymbolMapping.getVariable(modelParameter);
if (variable != null) {
mathSymbol = variable.getName();
}
if (mathSymbol != null) {
mathVariable = origMathDesc.getVariable(mathSymbol);
}
}
if (mathVariable != null) {
if (parameterMappingSpecs[i].isSelected()) {
if (parameterMappingSpecs[i].getHigh() < parameterMappingSpecs[i].getLow()) {
throw new MathException("The lower bound for Parameter '" + parameterMappingSpecs[i].getModelParameter().getName() + "' is greater than its upper bound.");
}
if (parameterMappingSpecs[i].getCurrent() < parameterMappingSpecs[i].getLow()) {
throw new MathException("The initial guess of '" + parameterMappingSpecs[i].getModelParameter().getName() + "' is smaller than its lower bound.");
}
if (parameterMappingSpecs[i].getCurrent() > parameterMappingSpecs[i].getHigh()) {
throw new MathException("The initial guess of '" + parameterMappingSpecs[i].getModelParameter().getName() + "' is greater than its upper bound.");
}
if (parameterMappingSpecs[i].getLow() < 0) {
throw new MathException("The lower bound for Parameter '" + parameterMappingSpecs[i].getModelParameter().getName() + "' is negative. All lower bounds must not be negative.");
}
if (Double.isInfinite(parameterMappingSpecs[i].getLow())) {
throw new MathException("The lower bound for Parameter '" + parameterMappingSpecs[i].getModelParameter().getName() + "' is infinity. Lower bounds must not be infinity.");
}
if (parameterMappingSpecs[i].getHigh() <= 0) {
throw new MathException("The upper bound for Parameter '" + parameterMappingSpecs[i].getModelParameter().getName() + "' is negative. All upper bounds must be positive.");
}
if (Double.isInfinite(parameterMappingSpecs[i].getHigh())) {
throw new MathException("The upper bound for Parameter '" + parameterMappingSpecs[i].getModelParameter().getName() + "' is infinity. Upper bounds must not be infinity.");
}
}
double low = parameterMappingSpecs[i].isSelected() && parameterMappingSpecs[i].getLow() == 0 ? 1e-8 : parameterMappingSpecs[i].getLow();
double high = parameterMappingSpecs[i].getHigh();
double scale = Math.abs(parameterMappingSpecs[i].getCurrent()) < 1.0E-10 ? 1.0 : Math.abs(parameterMappingSpecs[i].getCurrent());
double current = parameterMappingSpecs[i].getCurrent();
low = Math.min(low, current);
high = Math.max(high, current);
Parameter optParameter = new Parameter(mathSymbol, low, high, scale, current);
ParameterMapping parameterMapping = new ParameterMapping(modelParameter, optParameter, mathVariable);
//
if (mathVariable instanceof Constant) {
Constant origConstant = (Constant) mathVariable;
for (int j = 0; j < allVars.length; j++) {
if (allVars[j].equals(origConstant)) {
if (parameterMappingSpecs[i].isSelected()) {
allVars[j] = new ParameterVariable(origConstant.getName());
} else {
allVars[j] = new Constant(origConstant.getName(), new Expression(optParameter.getInitialGuess()));
}
break;
}
}
}
//
if (parameterMappingSpecs[i].isSelected()) {
parameterMappingList.add(parameterMapping);
}
}
}
parameterMappings = (ParameterMapping[]) BeanUtils.getArray(parameterMappingList, ParameterMapping.class);
try {
origMathDesc.setAllVariables(allVars);
} catch (ExpressionBindingException e) {
e.printStackTrace(System.out);
throw new MathException(e.getMessage());
}
//
for (int i = 0; i < parameterMappings.length; i++) {
optSpec.addParameter(parameterMappings[i].getOptParameter());
}
Vector<Issue> issueList = new Vector<Issue>();
IssueContext issueContext = new IssueContext();
optSpec.gatherIssues(issueContext, issueList);
for (int i = 0; i < issueList.size(); i++) {
Issue issue = issueList.elementAt(i);
if (issue.getSeverity() == Issue.SEVERITY_ERROR) {
throw new RuntimeException(issue.getMessage());
}
}
//
//
//
optimizationSpec = optSpec;
return mathSymbolMapping;
}
Aggregations