📄 model.java
字号:
package com.power.lpsolver.LPSolve;
import java.util.*;
/**
*
* <p>Title: PIPE Engine</p>
* <p>Description: Global Planning Optimization Engine</p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: Paraster, Inc.</p>
* @author Wei Tan
* @version 1.0
*/
/**
* Model is the representation of the entire LP model, including all variables,
* constraints, and objective function for each phase if a two-phase simplex
* is required. Model is a singular class.
*/
public class Model
{
private static final Model INSTANCE =
new Model();
/**
* Sole class constructor.
*/
private Model( ) {
}
public static Model getInstance( ) {
return INSTANCE;
}
/**
* the holder of all variables.
*/
private ModelVariables _mdlVariables = new ModelVariables();
/**
* the holder of all constraints.
*/
private ModelConstraints _mdlConstraints = new ModelConstraints();
/**
* the holder of the objective function.
*/
private ObjectiveFunction _mdlObjFunc;
/**
* the holder of Phase I objective function.
*/
private ObjectiveFunction _phaseOneObjFunc = new ObjectiveFunction( "MIN" );
/**
* the total number of columns in the model.
*/
private int _numCols;
/**
* the total number of rows in the model.
*/
private int _numRows;
/**
* the original user specified objective function, used when two-phase
* algorithm is required.
*/
private ObjectiveFunction _savedObjFunc;
/**
* the vector that holds all artificial variable indices.
*/
private Vector _artVarIndice = new Vector();
/**
* the user specified name for the model.
*/
private String _mdlName;
/**
* the precision to be used in LP computation. The default is set to 5.E-11.
*/
private double _precision = 5.E-11;
/**
* the total number of variable in original user specified model.
*/
private int _totOriginalVars = 0;
/**
* Sets the name of the model.
* @param name the name to be used for the model.
*/
public void setModelName( String name ) {
_mdlName = name;
}
/**
* Adds an artificial variable index to _artVarIndice.
* @param idx the variable index to be added.
*/
public void addArtVarIndex( int idx ) {
_artVarIndice.addElement( new Integer( idx ) );
}
/**
* Gets the vector of artificial variable indice.
* @return the vector that holds all artificial variables: _artVarIndice.
*/
public Vector getArtVarIndice() {
return _artVarIndice;
}
/**
* Adds a variable to the model.
* @param var the variable to be added.
*/
public void addVariable( Variable var ) {
_mdlVariables.addVariable( var );
}
/**
* Adds a constraint to the model.
* @param con the constraint to be added.
*/
public void addConstraint( Constraint con ) {
_mdlConstraints.addConstraint( con );
}
/**
* Gets the constraints in the model.
* @return the constraints holder: _mdlConstraints.
*/
public ModelConstraints getModelConstraints() {
return _mdlConstraints;
}
/**
* Gets the variables in the model.
* @return the variables holder: _mdlVariables.
*/
public ModelVariables getModelVariables() {
return _mdlVariables;
}
/**
* Sets the objective function for the model or for this phase.
* @param obj the objective function to be set for the model.
*/
public void setObjectiveFunction( ObjectiveFunction obj ) {
_mdlObjFunc = obj;
}
/**
* Gets the total number of columns (variables) of the model.
* @return the total number of variables.
*/
public int getNumberOfColumns() {
return _mdlVariables.getCardinality();
}
/**
* Initializes the model by remembering its origianl number of variables, and
* then initializes all constraints.
*/
public void initialize() {
_totOriginalVars = getNumberOfColumns();
_mdlConstraints.initialize();
//_mdlVariables.print();
}
/**
* Gets the total original number of variables.
* @return the total original number of variables.
*/
public int getTotOriginalVariables() {
return _totOriginalVars;
}
/**
* Gets the current objective function, create one if none exists.
* @return the current objective function.
*/
public ObjectiveFunction getObjectiveFunction() {
if( null == _mdlObjFunc ) {
_mdlObjFunc = new ObjectiveFunction( "MIN" );
}
return _mdlObjFunc;
}
/**
* Gets Phase I objective function.
* @return the Phase I objective function.
*/
public ObjectiveFunction getPhaseOneObjectiveFunction() {
return _phaseOneObjFunc;
}
/**
* Sets up model for Phase I optimization: saving user specified objective
* function, and make the Phase I objective function current.
*/
public void setModelForPhaseOne() {
_savedObjFunc = _mdlObjFunc;
_mdlObjFunc = _phaseOneObjFunc;
//_phaseOneObjFunc.print();
}
/**
* Sets up model for Phase II optimization: restoring user specified
* objective function, droping artificial variables, and sets up Phase II
* objective function.
*/
public void setModelForPhaseTwo() {
_mdlObjFunc = _savedObjFunc;
_mdlConstraints.dropArtificialVariables();
_mdlConstraints.setupPhaseTwoObjFunc();
//_mdlObjFunc.print();
}
/**
* Tests to see if the model requires the two-phase Simplex algorithm by
* checking the size of Phase I objective function.
* @return <code>true</code> if the size of Phase I objective function is greater than
* zero;
* <code>false</code> otherwise.
*/
public boolean needTwoPhaseSimplex() {
if( _phaseOneObjFunc.getElements().size() == 0 ) {
return false;
}
return true;
}
/**
* Gets the precision for the model.
* @return the precision.
*/
public double getPrecision() {
return _precision;
}
/**
* Gets the total number of rows of the model.
* @return the total number of rows.
*/
public int getNumberOfRows() {
return _mdlConstraints.getNumberOfRows();
}
/**
* Prints the model in LP format.
*/
public void print() {
_mdlObjFunc.print();
_mdlConstraints.print();
}
/**
* Tests to see if a column index is an artificial variable index.
* @param colIdx the column index to be checked.
* @return <code>true</code> if it is in the <code>Vector</code> _artVarIndice;
* <code>false</code> otherwise.
*/
public boolean isArtVarIndex( int colIdx ) {
Integer anInt = MemoryManager.getInstance().getInteger( colIdx );
if( _artVarIndice.contains( anInt ) ) {
return true;
}
return false;
}
/**
* Mormalizes the model to convert constraints with negative RHS.
*/
public void normalize() {
Vector cons = _mdlConstraints.getConstraints();
for( int i=0; i<cons.size(); i++ ) {
Constraint aCon = (Constraint) cons.elementAt( i );
aCon.normalize();
}
}
public Hashtable variableTable = new Hashtable();
public void addTableEntry( String MPSVar, String regVar ) {
variableTable.put( MPSVar, regVar );
}
public String getRegularVarName( String MPSVar ) {
return (String) variableTable.get( MPSVar );
}
public void reset() {
_mdlVariables = null;
_mdlConstraints.reset();
System.gc();
_mdlConstraints = null;
_mdlObjFunc = null;
_phaseOneObjFunc = null;
_artVarIndice = null;
System.gc();
MemoryManager.getInstance().reset();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -