📄 objectivefunction.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
*/
/**
* ObjectiveFunction is a special kind of constraint in that it has a sense
* property (MAX or MIN). The default sense is MIN. Also ObjectiveFunction
* has operations different from a constraint.
*/
public class ObjectiveFunction extends Constraint
{
/**
* The sense of the ObjectiveFunction, may only be MAX or MIN.
*/
private String _sense;
/**
* class constructor, sense is initialized.
* @param sense the sense of the ObjectiveFunction.
*/
public ObjectiveFunction( String sense ) {
super( "Objective Function", -1 );
_sense = sense;
//setRHS( 0 );
setSign( "=" );
}
/**
* class constructor, also default constructor.
*/
public ObjectiveFunction() {
super( "Objective Function", 0 );
//setRHS( 0 );
setSign( "=" );
}
public void addElement( Element elem ) {
_conElements.put( _memMgr.getIntegerString( elem.getColumnNumber() ),
elem );
}
/**
* Sets the sense of the ObjectiveFunction.
* @param str the sense to be set.
*/
public void setSense( String str ) {
_sense = str;
}
/**
* Tests to see if current solution is optimal.
* @return <code>true</code> when all coefficients are greater than zero;
* <code>false</code> otherwise.
*/
public boolean isOptimal() {
Enumeration allElems = super.getElements().elements();
Hashtable basicVariables = Solve.getInstance().getBasicVariables();
MemoryManager memMgr = MemoryManager.getInstance();
while( allElems.hasMoreElements() ) {
Element elem = (Element) allElems.nextElement();
Object obj = basicVariables.get( memMgr.getIntegerString( elem.getColumnNumber() ) );
if( null != obj ) {
continue;
}
if( elem.getCoefficient() < 0 ) return false;
}
return true;
}
/**
* Gets the pivotal column number - the column with the most negative
* coefficient.
* @return the found column number;
* -1 when none exists.
*/
public int getPivotColumn() {
Solve solve = Solve.getInstance();
Hashtable basicVariables = solve.getBasicVariables();
Enumeration allElems = super.getElements().elements();
MemoryManager memMgr = MemoryManager.getInstance();
double mostNegative = 0;
int colIdx = -1;
while( allElems.hasMoreElements() ) {
Element elem = (Element) allElems.nextElement();
double coeff = elem.getCoefficient();
//System.out.println( "coeff = " + coeff );
if( coeff >= 0 ) continue;
//must be nonbasic variable
Object obj = basicVariables.get( memMgr.getIntegerString( elem.getColumnNumber() ) );
if( null != obj ) {
continue;
}
//get most negative
if( coeff < mostNegative ) {
mostNegative = coeff;
colIdx = elem.getColumnNumber();
}
}
//System.out.println("returned colidx " + colIdx );
return colIdx;
}
/**
* Sets up Phase I objective function with a given constraint - replaces
* an artificial variable in the objective function using a constraint
* that contains the artificial variable.
* con: sum(rest) + artVar = b may be rewritten as
* con: artVar = b - sum(rest).
* @param con a constraint that contains an artificial variable.
*/
public void setupPhaseOneObjWithConstraint( Constraint con ) {
Enumeration allElems = con.getElements().elements();
while( allElems.hasMoreElements() ) {
Element elem = (Element) allElems.nextElement();
Element alreadyInObj = (Element) getElementAt( elem.getColumnNumber() );
if( null == alreadyInObj ) {
alreadyInObj = (Element) elem.clone();
alreadyInObj.applyMultiplier( -1 );
this.addElement( alreadyInObj );
} else {
alreadyInObj.addCoefficient( -1, elem );
}
}
setRHS( getRHS() - con.getRHS() );
eliminateFloatingError();
}
public String getMPSRowType() {
return new String( " N " );
}
public void print() {
System.out.print( "\n" + "Min" + ": " );
Enumeration allElems = this.getElements().elements();
ModelVariables mdlVars = Model.getInstance().getModelVariables();
while( allElems.hasMoreElements() ) {
Element elem = (Element) allElems.nextElement();
Variable var = mdlVars.getVariable( elem.getColumnNumber() );
if( elem.getCoefficient() > 0 ) {
System.out.print( " + " );
} else {
System.out.print( " - " );
}
System.out.print( Math.abs( elem.getCoefficient() ) );
System.out.print( var.getMPSName() );
}
System.out.print( ";\n" );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -