著名IT公司ILog的APS高级排产优化引擎
源代码在线查看: lpurlwriter.java
package com.power.lpsolver.LPSolve; import java.util.*; import java.io.*; import java.net.*; import com.power.pipe.*; import com.power.util.Message.*; import com.power.util.urltools.*; public class LPURLWriter { private static final LPURLWriter INSTANCE = new LPURLWriter(); private BufferedWriter _myFileWriter; //initialized in subclass // Private constructor supresses // default public constructor private LPURLWriter( ) { } public static LPURLWriter getInstance( ) { return INSTANCE; } private StringBuffer content = new StringBuffer(); private String urlAddr = GlobalConfig.getInstance().getLPServerURL() + "com.power.pipeengine.LP_ReadWriteFile"; public void write( String outDir ) { printObj(); Vector allCons = Model.getInstance().getModelConstraints().getConstraints(); MessageArea.getInstance().addMessage( "Total number of constraints: " + allCons.size() + "\n"); for( int i=0; i Constraint con = (Constraint) allCons.elementAt( i ); printCon( con ); if( (i%100) == 0 ) { MessageArea.getInstance().addMessage( "Number of consrtaints generated: " + i+ "\n"); } } URLFileWriter.getInstance().write( urlAddr, "c:\\paraster\\lpsolver\\model.lp", "WriteModel", content.toString() ); } public void printObj() { content.append( "Min" + ": "); Enumeration allElems = Model.getInstance().getObjectiveFunction().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 ) { content.append(" + "); } else { content.append(" - "); } content.append(Math.abs( elem.getCoefficient() )); content.append(var.getMPSName()); } content.append(";\n"); } /** * Prints the constraint in a readable format (LP format). */ public void printCon( Constraint con ) { Enumeration allElems = con.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 ) { content.append(" + "); } else { content.append(" - "); } content.append(Math.abs( elem.getCoefficient() )); content.append(var.getMPSName()); } content.append(" " + con.getSign() + " " + con.getRHS() + ";\n"); } }