⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 modelconstraints.java

📁 今天为网友提供的是JAVA源码
💻 JAVA
字号:
package com.power.lpsolver.LPSolve;

import java.util.*;
import com.power.pipeengine.*;
/**
 *
 * <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
 */

/**
 * ModelConstraints is a place holder that anchors all constraints. It also has
 * methods for inter-constraint operations.
 */
public class ModelConstraints
{
    /**
     * stores all constraints.
     */
    static ResourceBundle res = ResourceBundle.getBundle("com.power.lpsolver.LPSolve.Res",
                                                          EngineConfig.getInstance().getLocale() );
    private Vector _constraints = new Vector();

    /**
     * stores all constraints hashed by constraint symbolic name.
     */
	private Hashtable _constraintsByName = new Hashtable();

    /**
     * Sole class constructor.
     */
	public ModelConstraints() {
	}

    /**
     * Adds a constraint to the _constraints <code>Vector</code>, also create an
     * entry in the corresponding Hashtable _constraintsByName.
     * @param aCon
     */
	public void addConstraint( Constraint aCon ) {
		_constraints.addElement( aCon );
		_constraintsByName.put( aCon.getName(), aCon );
        if( ( _constraints.size() % 1000 ) == 0 ) {
            //System.out.println( "Number of constraints: " + _constraints.size() );
        }
        if( ((_constraints.size() % 1000 ) > 0) &&
            ((_constraints.size() % 3000 ) == 0) ) {
            System.gc();
        }

	}

    /**
     * Gets all constraints.
     * @return the Vector that holds all constraints.
     */
	public Vector getConstraints() {
		return _constraints;
	}

    /**
     * Gets a constraint by its symbolic name.
     * @param name the name of the constraint to get.
     * @return     the constraint with the given name.
     */
	public Constraint getConstraint( String name ) {
		Constraint con = (Constraint) _constraintsByName.get( name );
		if( null == con ) {
			ObjectiveFunction obj = Model.getInstance().getObjectiveFunction();
			if( obj.getName().equals( name ) ) {
				con = obj;
			}
		}

		return con;
	}

    /**
     * Removes a constraint object con when it becomes redundant.
     * @param con the constraint object to be removed.
     */
	public void removeConstraint( Constraint con ) {
        String key = con.getName();
        _constraintsByName.remove( key );
		_constraints.removeElement( con );
		for( int i=0; i<_constraints.size(); i++ ) {
			Constraint aCon = (Constraint) _constraints.elementAt( i );
			aCon.setRowNumber( i );
		}
	}


    /**
     * Conducts Gaussian elimination by a given row and a pivotal column.
     * @param rowNum   the row number of the pivotal row.
     * @param pivotCol the column number of the pivotal column.
     */
	public void gaussianEliminationByRow( int rowNum, int pivotCol ) {
		Constraint pivotRow = (Constraint) _constraints.elementAt( rowNum );

		for( int i=0; i<_constraints.size(); i++ ) {
			if( i == rowNum ) continue;
			Constraint con = (Constraint) _constraints.elementAt( i );
			Element elem = con.getElementAt( pivotCol );

			if( null == elem ) continue;

			double coeff = elem.getCoefficient();
			con.plusConstraint( - coeff, pivotRow );
		}

		//work on the objective function
		ObjectiveFunction objFunc = Model.getInstance().getObjectiveFunction();
		Element elem = objFunc.getElementAt( pivotCol );
		if( null == elem ) return;

		double coeff = elem.getCoefficient();
		objFunc.plusConstraint( -coeff, pivotRow );

	}

    /**
     * Initializes all constraints.
     */
	public void initialize() {
		for( int i=0; i<_constraints.size(); i++ ) {
			Constraint con = (Constraint) _constraints.elementAt( i );
			con.initialize();
		}
	}

    /**
     * Gets the pivotal row number based on given pivotal column number according
     * to the Simplex algorithm.
     * @param  pivotColumn the pivotal column number.
     * @return the pivotal row number.
     */
	public int getPivotRowNumber( int pivotColumn ) {
		double minRatio = Double.POSITIVE_INFINITY;
		int pivotRow = -1;
		int numCandidates = 5;
		int[] candidateRows = new int[numCandidates];
		int lastFoundRow = -1;
		int index = 0;
		double adjustedRHS = Model.getInstance().getPrecision() * 100;

		for( int i=0; i<_constraints.size(); i++ ) {
			Constraint con = (Constraint) _constraints.elementAt( i );
			Element elem = con.getElementAt( pivotColumn );

			if( null == elem ) continue;
			double coeff = elem.getCoefficient();
			if( coeff <= 0 ) continue;

			double rhs = con.getRHS();
			double ratio;
			ratio = rhs / coeff;
			if( rhs == 0.0 ) {
				ratio = adjustedRHS * Math.random()  / coeff;
			} else {
				ratio = rhs / coeff;
			}


			if( ratio < minRatio ) {
				minRatio = ratio;
				pivotRow = i;
			} /*else if( ratio == minRatio ) {
				if( pivotRow != lastFoundRow ) {
					lastFoundRow = pivotRow;
					candidateRows[0] = pivotRow;
					candidateRows[1] = i;
					for( int k=2; k<numCandidates; k++ ) {
						candidateRows[k] = -1;
					}
					index = 2;
				} else {
					if( index >= numCandidates ) continue;
					candidateRows[index] = i;
					index++;
				}
			}*/

		}

		if( pivotRow < 0 ) {
			System.out.println( res.getString("Problem_is_unbounded_") );
			System.exit( 0 );
		}

		/*if( pivotRow == lastFoundRow ) {
			int rnd = (int) ( Math.random() * 1000 );
			pivotRow = candidateRows[rnd%(index-1)];
		}*/

		return pivotRow;

	}

    /**
     * Gets a constraint by its row number.
     * @param rowNum the row number of the constraint to get.
     * @return the constraint object of the given row number.
     */
	public Constraint getConsrtaint( int rowNum ) {
		return (Constraint) _constraints.elementAt( rowNum );
	}

    /**
     * Drops artificial variables after Phase I optimization completed.
     */
	public void dropArtificialVariables() {
		for( int i=0; i<_constraints.size(); i++ ) {
			Constraint con = (Constraint) _constraints.elementAt( i );
			con.dropArtificialVariables();
		}
	}

    /**
     * Sets up Phase II objective function after Phase I completes.
     */
	public void setupPhaseTwoObjFunc() {
		for( int i=0; i<_constraints.size(); i++ ) {
			Constraint con = (Constraint) _constraints.elementAt( i );
			con.setupPhaseTwoObjFunc();
		}
	}

    /**
     * Gets the total number of rows currently in the model, exculding removed
     * rows.
     * @return the total number of rows.
     */
	public int getNumberOfRows() {
		return _constraints.size();
	}

    /**
     * Prints all constraints.
     */
	public void print() {
		for( int i=0; i<_constraints.size(); i++ ) {
			Constraint con = (Constraint) _constraints.elementAt( i );
			con.print();
		}
	}

	public void reset() {
		for( int i=0; i<_constraints.size(); i++ ) {
			Constraint con = (Constraint) _constraints.elementAt( i );
			con.reset();
		}
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -