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

📄 capacityconstraint.java

📁 简介: 今天为网友提供的是JAVA源码
💻 JAVA
字号:
package com.power.pipeengine.Constraint;

import java.util.*;
import com.power.pipeengine.Entity.*;
import com.power.pipeengine.InputData.*;
import com.power.pipeengine.LPModel.*;
import com.power.pipeengine.Report.*;
import com.power.pipeengine.Variable.*;
import com.power.pipeengine.*;
import com.power.lpsolver.LPSolve.*;

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


/**
 * This class creates capacity constraint (see LP model document for algorithm).
 * It is a singular class.
 */
public class CapacityConstraint extends Constraint
{
    static ResourceBundle res = ResourceBundle.getBundle("com.power.pipeengine.Res",
                                                          EngineConfig.getInstance().getLocale() );
    private static final CapacityConstraint INSTANCE =
                              new CapacityConstraint();

    /**
     * Sole class constructor.
     */
    private CapacityConstraint( ) {
        setConstraintType( res.getString("CAPACITY_CONSRTAINT") );
    }

    public static CapacityConstraint getInstance( ) {
        return INSTANCE;
    }


    /**
     * Builds all capacity constraints by looping through all resources and all
     * buckets.
     */
    public void buildConstraints() {
        super.publishMessage();
        DataModel dataModel = DataModel.getInstance();
        ResourceUses rscUses = dataModel.getResourceUses();
        Enumeration allRscs = dataModel.getResources().getResources().elements();
        int numOfBuckets = dataModel.getCalendar().getTotalNumOfBuckets();
        PIPECalendar cal = dataModel.getCalendar();
        Routes routes = dataModel.getRoutes();
        StartsVariable startsVar = StartsVariable.getInstance();
        OutsVariable outsVar = OutsVariable.getInstance();
        Products products = dataModel.getProducts();
        RscSlackVariable rscSlackVar = RscSlackVariable.getInstance();
        Model mdl = Model.getInstance();
        MemoryManager memMgr = MemoryManager.getInstance();
        //loop through all resources
        while( allRscs.hasMoreElements() ) {
                Resource rsc = (Resource) allRscs.nextElement();

                //skip if no rscUse defined for this rsc
                if( rscUses.getResourceUse( rsc.getResourceID() ) == null ) continue;
                //for all time buckets
                for( int t=1; t<=numOfBuckets; t++ ) {
                        Bucket b = cal.getBucket( t );
                        if( rsc.getFacility().getNumWorkingDaysForBucket( t ) == 0 ) {
                            continue;
                        }

                        //create a new constraint for the LPSolve
                        int rowNumber = Model.getInstance().getNumberOfRows();
                        com.power.lpsolver.LPSolve.Constraint con =
                            new com.power.lpsolver.LPSolve.Constraint(
                                      "C" + rowNumber,
                                      rowNumber );


                        //loop through all resources ueses
                        Enumeration rus = rscUses.getResourceUse( rsc.getResourceID() ).elements();
                        while( rus.hasMoreElements() ) {
                                ResourceUse ru = (ResourceUse) rus.nextElement();
                                Route r = routes.getRoute( ru.getRouteID() );

                                if( r.getLastStartBucketHavingOuts() < t ) continue;

                                Product p = r.getProduct();
                                Facility f = r.getFacility();
                                double cycleTime = ru.getCycleTimeToResource();
                                double yield = ru.getYieldToResource();
                                double numWDays = f.getNumWorkingDaysForBucket( t );
                                double uph = ru.getUPH();

                                if( cycleTime < numWDays ) {
                                        //resource use rate
                                        double coeff = yield * numWDays / (numWDays - cycleTime) ;
                                        coeff = coeff / uph;
                                        String sVar = startsVar.getVariable( r, p, b );

                                        int colIdx = Model.getInstance().getModelVariables().addVariable( sVar );
                                        Element elem = MemoryManager.getInstance().getElement();
                                        elem.setProperties( colIdx, coeff );
                                        con.addElement( elem );
                                        this.addMPSElem( con, colIdx, elem );
                                }

                                //skip if first period
                                if( t == 1 ) continue;

                                //Earliest possible starts period, if too early, then start from bucket 1
                                Bucket esb = outsVar.getRouteStartBucket( r, p, b );
                                if( esb == null ) {
                                        esb = cal.getBucket( 1 );
                                        for( int n=1; n<=t; n++ ) {
                                            if( f.getNumWorkingDaysForBucket( n ) != 0 ) {
                                                esb = cal.getBucket( n );
                                                break;
                                            }
                                        }
                                }

                                for( int i= esb.getBucketID(); i<t; i++ ) {
                                        double rteStartToThisBucket = 0;
                                        for( int j=i; j<=t; j++ ) {
                                                rteStartToThisBucket += f.getNumWorkingDaysForBucket( j );
                                        }

                                        if( cycleTime >= rteStartToThisBucket ) continue;
                                        if( cycleTime < rteStartToThisBucket - numWDays ) continue;
                                        //remaining cycletime of the route
                                        double remCycleTime = r.getCycleTime() -
                                                                                  rteStartToThisBucket +
                                                                                  numWDays;

                                        //Although the outs are in this bucket, but it is due to the midpoint rule
                                        if( remCycleTime <= 0 ) continue;

                                        double coeff = yield * numWDays / remCycleTime / uph;
                                        if( r.getFacility().getNumWorkingDaysForBucket( i ) == 0 ) continue;
                                        String varName = startsVar.getVariable( r, p, cal.getBucket(i) );
                                        int colIdx = Model.getInstance().getModelVariables().addVariable( varName );
                                        Element elem = MemoryManager.getInstance().getElement();
                                        elem.setProperties( colIdx, coeff );
                                        con.addElement( elem );
                                        this.addMPSElem( con, colIdx, elem );

                                }

                        }

                        //add rsc slack variable
                        String varName = rscSlackVar.getVariable( rsc, rsc.getFacility(), b );
                        int colIdx = Model.getInstance().getModelVariables().addVariable( varName );
                        Element elem = MemoryManager.getInstance().getElement();
                        elem.setProperties( colIdx, 1.0 );
                        con.addElement( elem );
                        this.addMPSElem( con, colIdx, elem );

                        //RHS
                        if( con.getElements().size() > 0 ) {
                        //if( con.getNumberOfElements() > 0 ) {
                                double hours = rsc.getResourceHours( b ) *
                                                           rsc.getFacility().getNumWorkingDaysForBucket(t);
                                con.setRHS( hours );
                        }

                        con.setSign( "=" );
                        mdl.addConstraint( con );

                }
        }


    }

}

⌨️ 快捷键说明

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