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

📄 mpsreader.java

📁 著名IT公司ILog的APS高级排产优化引擎
💻 JAVA
字号:
package com.power.lpsolver.LPSolve;

import java.util.*;
import java.io.*;
import com.power.pipeengine.*;

public class MPSReader
{
    static ResourceBundle res = ResourceBundle.getBundle("com.power.lpsolver.LPSolve.Res",
                                                          EngineConfig.getInstance().getLocale() );
    private String _fileName;
	private File _inputFile;

	public MPSReader() {
		//_fileName = "d:\\LPSolve\\TestData\\blend.mps";
	}

	public void setFileName( String fileName ) {
		_fileName = "d:\\LPSolve\\TestData\\" + fileName;
		_inputFile = new File( _fileName );
	}

	private void testFile() throws IOException {
		if( !_inputFile.exists() ) {
			abort( res.getString("File_does_not_exist_") + _fileName );
		}

		if( !_inputFile.isFile() ) {
			abort( res.getString("File_is_a_directory") + _fileName );
		}

		if( !_inputFile.canRead() ) {
			abort( res.getString("File_is_unreadable_") + _fileName );
		}
	}

	private static void abort( String msg ) throws IOException {
		throw new IOException( msg );
	}

	public void readFile( )
		throws IOException
	{
		testFile();

		//long fileLength = _inputFile.length();

		FileReader fr = new FileReader( _inputFile );
		BufferedReader bufRdr = new BufferedReader( fr );

		readModelName( bufRdr );
		readRows( bufRdr );
		readColumns( bufRdr );
		readRHS( bufRdr );

		String line = bufRdr.readLine();
		if( line.toUpperCase().indexOf( "BOUNDS" ) >= 0 ) {
			//readBounds( bufRdr );
		}

		fr.close();

	}

	private void errorExit( String msg ) {
		System.out.println( msg );
		System.exit( 0 );
	}

	private void readModelName( BufferedReader bufRdr )
	throws IOException
	{
		String line = bufRdr.readLine();

		if( line.toUpperCase().indexOf( "NAME" ) < 0 ) {
			errorExit( res.getString("File_reading_error") );
		}

		String problemTitle = line.substring( 14 );
		Model.getInstance().setModelName( problemTitle );
	}

	public void readRows( BufferedReader bufRdr )
		throws IOException
	{
		String line;
		String rowType;
		String rowName;
		int rowNum = 0;
		Model mdl = Model.getInstance();

		//read and discard "ROWS"
		bufRdr.readLine();

		while( ( line = bufRdr.readLine() ) != null ) {
			if( line.toUpperCase().indexOf( "COLUMNS" ) == 0 ) return;
			//System.out.println( line );

			rowType = stripOffWhiteSpaces( line.substring( 1, 3 ) );
			//System.out.println( rowType );

			rowName = stripOffWhiteSpaces( line.substring( 4 ) );
			//System.out.println( rowName );

			if( rowType.equals( "N" ) ){
				ObjectiveFunction obj = new ObjectiveFunction( "MIN" );
				mdl.setObjectiveFunction( obj );
				obj.setName( rowName );
			} else {
				Constraint con = new Constraint( rowName, rowNum );
				con.setMPSSign( rowType );
				mdl.addConstraint( con );

				rowNum++;
				//System.out.println( "A constraint added" );
			}
		}
	}

	public void readColumns( BufferedReader bufRdr )
		throws IOException
	{
		String line;
		String colName;
		String rowName;
		int colNum = 0;
		double coeff;
		Model mdl = Model.getInstance();
		ModelVariables mdlVars = mdl.getModelVariables();
		Constraint con;
		MemoryManager memMgr = MemoryManager.getInstance();
		String tmpStr;

		while( ( line = bufRdr.readLine() ) != null ) {
			//System.out.println( line );
			if( line.toUpperCase().indexOf( "RHS" ) == 0 ) return;

			colName = stripOffWhiteSpaces( line.substring( 4, 12 ) );
			//System.out.println( colName );
			colNum = mdlVars.addVariable( colName );
			rowName = stripOffWhiteSpaces( line.substring( 14, 22 ) );
			//System.out.println( rowName );
			con = mdl.getModelConstraints().getConstraint( rowName );

			if( null == con ) {
				System.out.println( res.getString("Row") + rowName + res.getString("is_not_defined_") );
			}

			tmpStr = stripOffWhiteSpaces( line.substring( 24, 36 ) );
			coeff = Double.valueOf( tmpStr ).doubleValue();
			Element elem = memMgr.getElement();
			elem.setProperties( colNum, coeff );
			con.addElement( elem );

			if( line.length() <= 40 ) continue;

			rowName = stripOffWhiteSpaces( line.substring( 39, 47 ) );
			con = mdl.getModelConstraints().getConstraint( rowName );

			tmpStr = stripOffWhiteSpaces( line.substring( 49, 61 ) );
			coeff = Double.valueOf( tmpStr ).doubleValue();
			elem = memMgr.getElement();
			elem.setProperties( colNum, coeff );
			con.addElement( elem );
		}
	}

	public void readRHS( BufferedReader bufRdr )
		throws IOException
	{
		String line;
		String vectorName;
		String rowName;
		double rhs;
		Model mdl = Model.getInstance();
		Constraint con;
		String tmpStr;

		while( ( line = bufRdr.readLine() ) != null ) {
			if( line.charAt( 0 ) != ' ' ) {
				bufRdr.reset();
				return;
			}
			//System.out.println( line );
			vectorName = stripOffWhiteSpaces( line.substring( 4, 12 ) );
			rowName = stripOffWhiteSpaces( line.substring( 14, 22 ) );
			con = mdl.getModelConstraints().getConstraint( rowName );

			tmpStr = stripOffWhiteSpaces( line.substring( 24, 36 ) );
			rhs = Double.valueOf( tmpStr ).doubleValue();
			con.setRHS( rhs );

			if( line.length() <= 40 ) continue;

			rowName = stripOffWhiteSpaces( line.substring( 39, 47 ) );
			con = mdl.getModelConstraints().getConstraint( rowName );

			tmpStr = stripOffWhiteSpaces( line.substring( 49 ) );
			rhs = Double.valueOf( tmpStr ).doubleValue();
			con.setRHS( rhs );

			bufRdr.mark( 120 );
		}
	}

	public void readBounds( BufferedReader bufRdr )
		throws IOException
	{
		String line;
		String colName;
		int colNum = 0;
		double bound;
		Model mdl = Model.getInstance();
		ModelVariables mdlVars = mdl.getModelVariables();

		while( ( line = bufRdr.readLine() ) != null ) {
			if( line.toUpperCase().indexOf( "ENDATA" ) >= 0 ) return;

			colName = line.substring( 4, 11 );
			colNum = mdlVars.addVariable( colName );

			bound = Double.valueOf( line.substring( 24, 35 ) ).doubleValue();
		}
	}

	private String stripOffWhiteSpaces( String str ) {
		int firstChar = 0;
		int lastChar = str.length() - 1;

		for( int i=0; i<str.length(); i++ ) {
			if( str.charAt( i ) != ' ' ) {
				firstChar = i;
				break;
			}
		}

		for( int i=str.length()-1; i>=0; i-- ) {
			if( str.charAt( i ) != ' ' ) {
				lastChar = i;
				break;
			}
		}

		return str.substring( firstChar, lastChar+1 );
	}






}

⌨️ 快捷键说明

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