📄 plan.java
字号:
package com.power.pipeengine.DispatchReportMap;
/**
* <p>Title: PIPE Engine</p>
* <p>Description: Global Planning Optimization Engine</p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: Paraster, Inc.</p>
* @author not attributable
* @version 1.0
*/
import java.util.*;
import java.io.*;
import com.power.pipeengine.Report.*;
import com.power.pipeengine.Entity.*;
import com.power.util.Message.*;
import com.power.util.urltools.*;
import com.power.pipeengine.DispatchReportMap.*;
import com.power.pipeengine.InputData.*;
import com.power.pipe.*;
import java.text.*;
public class Plan {
private String orderID;
private String styleID;
private Integer procID;
private Hashtable dailyPlan = new Hashtable();
private Vector dates = new Vector();
private Vector qtyByDate = new Vector();
private Vector scheduleByEmployee = new Vector();
private Vector employees = new Vector();
private Vector originalAssignedEmployees = new Vector();
private Hashtable originalScheduleByDate = new Hashtable();
private int plannedQtyForYesterday = 0;
private int cumWIP = 0;
public Plan( String odrID, String style, Integer pid ) {
orderID = odrID;
styleID = style;
procID = pid;
}
public void addRecord( Date date, Integer qty ) {
Calendar today = Calendar.getInstance();
if( date.before( today.getTime() ) ) return;
Integer amt = (Integer) dailyPlan.get( date.toString() );
boolean newEntry = false;
if( null == amt ) {
dailyPlan.put(date.toString(), qty);
amt = qty;
newEntry = true;
} else {
amt = new Integer( amt.intValue() + qty.intValue() );
dailyPlan.put(date.toString(), amt );
}
if( newEntry ) {
for (int i = 0; i < dates.size(); i++) {
Date aDate = (Date) dates.elementAt(i);
if (date.before(aDate)) {
dates.insertElementAt(date, i);
qtyByDate.insertElementAt(amt, i);
return;
}
}
dates.add(date);
qtyByDate.add(amt);
} else {
for (int i = 0; i < dates.size(); i++) {
Date aDate = (Date) dates.elementAt(i);
if (date.compareTo( aDate ) == 0 ) {
qtyByDate.remove( i );
qtyByDate.insertElementAt(amt, i);
return;
}
}
}
}
public String getOrderID() {
return orderID;
}
public int getPlanForDate( Date date ) {
return ((Integer)dailyPlan.get( date.toString() )).intValue();
}
private int getCumPlannedQty() {
Enumeration allQtys = dailyPlan.elements();
int cum = 0;
while( allQtys.hasMoreElements() ) {
Integer anInt = (Integer) allQtys.nextElement();
cum += anInt.intValue();
}
return cum;
}
public void replan( int finishedQty ) {
cumWIP = finishedQty;
//int demand = OrderDetailsManHourMap.getInstance().getDemand( orderID, styleID );
int demand = OrderDetailsManHourMap.getInstance().getDemandForDetailedStyle ( orderID, styleID );
int diff = demand - finishedQty - getCumPlannedQty();
//case when follow the plan
if( diff == 0 ) {
return;
}
System.out.println( "demand = " + demand + "\tdiff = " + diff + "\tfinishedQty = " + finishedQty);
//case when faster than planned
if( diff < 0 ) {
//double speedUp = Math.abs( diff ) / finishedQty;
int speedUpQty = Math.abs( diff );
int extra = 2 * Math.abs( diff );
//chop the extra off from the end
for( int i=qtyByDate.size()-1; i>=0; i-- ) {
Integer qty = (Integer) qtyByDate.elementAt( i );
if( extra >= qty.intValue() ) {
extra -= qty.intValue();
qty = new Integer( 0 );
qtyByDate.remove( i );
qtyByDate.insertElementAt( qty, i );
} else if( extra < qty.intValue() ) {
qty = new Integer( qty.intValue() - extra );
extra = 0;
qtyByDate.remove( i );
qtyByDate.insertElementAt( qty, i );
}
if( extra == 0 || i == 0 ) break;
}
System.out.print( "after 1 : " );
print();
//allocate the speedUpQty to the qty array
for( int i=0; i<speedUpQty; i++ ) {
int index = i% qtyByDate.size();
Integer qty = (Integer) qtyByDate.elementAt( index );
/*if( qty.intValue() == 0 ) {
i--;
continue;
}*/
qty = new Integer( qty.intValue() + 1 );
qtyByDate.remove( index );
qtyByDate.insertElementAt( qty, index );
}
}
//case when slower than planned
if( diff > 0 ) {
for( int i=0; i<diff; i++ ) {
Integer qty = (Integer) qtyByDate.elementAt( i% qtyByDate.size() );
qty = new Integer( qty.intValue() + 1 );
qtyByDate.remove( i% qtyByDate.size() );
qtyByDate.insertElementAt( qty, i% qtyByDate.size() );
}
}
//clean up zero entries
/*while( qtyByDate.size() > 0 ) {
System.out.println( "debug 4 : " );
Integer qty = (Integer) qtyByDate.lastElement();
if( qty.intValue() == 0 ) {
Date aDate = (Date) dates.lastElement();
dates.remove( aDate );
qtyByDate.remove( qty );
dailyPlan.remove( aDate.toString() );
}
}*/
System.out.print( "after 2: " );
print();
}
public void allocateOverEmployees() {
Calendar yesterday = Calendar.getInstance();
yesterday.add( Calendar.DAY_OF_YEAR, -1 );
SimpleDateFormat sdfInput = new SimpleDateFormat("yyyy-MM-dd");
sdfInput.setDateFormatSymbols(new DateFormatSymbols(Locale.CHINA));
/* Date tmpDate = null;
try{
tmpDate = sdfInput.parse(yesterday.getTime().toString() );
} catch( Exception e ) {
System.out.println("error parsing "+ e.getMessage());
}
System.out.println("debug 4");*/
if( cumWIP == 0 && originalScheduleByDate.get( yesterday.getTime().toString() ) == null ) {
//the style has not started yet, no plan for yesterday, use the original plan
System.out.println("debug 5");
genOriginalSchedule();
return;
}
System.out.println("debug 5.5");
if( cumWIP == 0 && originalScheduleByDate.get( yesterday.getTime().toString() ) != null ) {
//the style has not started yet, was a plan for yesterday, modify the original plan
System.out.println("debug 6");
modifyOriginalSchedule();
return;
}
System.out.println("debug 6.5");
Vector lastQtyByEmployee = LastDayProduction.getInstance().getProduction(
orderID, styleID, procID );
//if noboday working on this yesterday
if( null == lastQtyByEmployee || originalAssignedEmployees.size() == 0 ) {
System.out.println( "Error: nobody worked on this process yesterday: " +
orderID + ", " + styleID +
", " + procID );
int demand = OrderDetailsManHourMap.getInstance().getDemandForDetailedStyle ( orderID, styleID );
int diff = demand - cumWIP - getCumPlannedQty();
if(diff >0 ) {
modifyOriginalSchedule();
} else if( diff == 0 ) {
genOriginalSchedule();
} else { // diff < 0
adjustOriginalSchedule();
}
return;
}
System.out.println("debug 7");
double totalQty = 0;
for( int i=0; i<lastQtyByEmployee.size(); i++ ){
Object[] data = (Object[]) lastQtyByEmployee.elementAt( i );
totalQty += ((Integer) data[1]).intValue();
employees.add( data[0] );
}
double[] ratio = new double[lastQtyByEmployee.size()];
for( int i=0; i<lastQtyByEmployee.size(); i++ ){
scheduleByEmployee.add( new Vector() );
Object[] data = (Object[]) lastQtyByEmployee.elementAt( i );
ratio[i] = ((Integer) data[1]).intValue() / totalQty;
}
for( int i=0; i<qtyByDate.size(); i++ ) {
int qty = ((Integer) qtyByDate.elementAt( i )).intValue();
int cumAllocatedQty = 0;
int j=0;
for ( j = 0; j < lastQtyByEmployee.size()-1; j++) {
Vector vec = (Vector) scheduleByEmployee.elementAt( j );
int allocatedQty = (int) ratio[j] * qty;
vec.add( new Integer( allocatedQty ) );
cumAllocatedQty += allocatedQty;
}
Vector vec = (Vector) scheduleByEmployee.elementAt( j );
vec.add( new Integer( qty - cumAllocatedQty ) );
}
genSchedules();
}
public String getSchedules() {
return schedules.toString();
}
StringBuffer schedules = new StringBuffer();
public void genSchedules() {
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
Calendar cal = Calendar.getInstance();
for( int i=0; i<employees.size(); i++ ) {
Integer eID = (Integer) employees.elementAt( i );
Vector scheduleByThisEmployee = (Vector) scheduleByEmployee.elementAt(i);
for( int j=0; j<scheduleByThisEmployee.size(); j++ ) {
Date aDate = (Date) dates.elementAt( j );
cal.setTime( aDate );
Integer qty = (Integer) scheduleByThisEmployee.elementAt( j );
schedules.append("-1,"); //equipment
schedules.append( orderID + ","); //orderID
schedules.append( styleID + ","); //style ID
schedules.append( procID.toString() + ","); //process ID
schedules.append( eID.toString() + ","); //employee ID
schedules.append( qty.intValue() + ","); //qty
schedules.append("0,"); //finished qty
cal.set(Calendar.HOUR_OF_DAY, 8);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
schedules.append(sdf.format( cal.getTime() ) + ",");
cal.set(Calendar.HOUR_OF_DAY, 17);
schedules.append(sdf.format( cal.getTime() ) + ",");
schedules.append("null,"); //null value
schedules.append("1\n"); //line number
}
}
}
private void genOriginalSchedule() {
System.out.println( "gen original schedule");
Enumeration keys = originalScheduleByDate.keys();
Calendar today = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd" );
SimpleDateFormat sdfLong = new SimpleDateFormat( "MMM dd yyyy" );
sdfLong.setDateFormatSymbols(new DateFormatSymbols(Locale.US));
while( keys.hasMoreElements() ) {
String key = (String) keys.nextElement();
Date aDate = null;
try{
aDate = sdfLong.parse(key.substring(4,10) + " " + key.substring( 24, 28) );
} catch( Exception e) {
System.out.println( "parsing error -1: " + e.getMessage() );
}
if (aDate.before(today.getTime())) continue;
Vector vec = (Vector) originalScheduleByDate.get( key );
for( int i=0; i<vec.size(); i++ ) {
Object[] rcd = (Object[]) vec.elementAt( i );
schedules.append("-1,"); //equipment
schedules.append( orderID + ","); //orderID
schedules.append( styleID + ","); //style ID
schedules.append( procID.toString() + ","); //process ID
schedules.append( ((Integer) rcd[0]).toString() + ","); //operator ID
schedules.append( ((Integer) rcd[1]).toString() + ","); //qty
schedules.append( "0,"); //already finished qty
schedules.append( sdf.format( aDate ) + " 08:00:00," ); //start time
schedules.append( sdf.format( aDate ) + " 17:00:00," ); //end time
schedules.append("null,"); //null value
schedules.append("1\n"); //line number
}
}
}
private void modifyOriginalSchedule() {
System.out.println( "modify original schedule");
int demand = OrderDetailsManHourMap.getInstance().getDemandForDetailedStyle( orderID, styleID );
Calendar today = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd" );
SimpleDateFormat sdfLong = new SimpleDateFormat( "MMM dd yyyy" );
sdfLong.setDateFormatSymbols(new DateFormatSymbols(Locale.US));
int diff = demand - getCumPlannedQty();
while( diff > 0 ) {
Enumeration keys = originalScheduleByDate.keys();
while( keys.hasMoreElements() ) {
String key = (String) keys.nextElement();
try{
if (sdf.parse(key.substring(4,10) + " " + key.substring( 24, 28)).before(today.getTime()))
continue;
} catch( Exception e) {
System.out.println( "Date parsing error -2: " + e.getMessage() );
}
Vector vec = (Vector) originalScheduleByDate.get( key );
for( int i=0; i<vec.size(); i++ ) {
Object[] rcd = (Object[]) vec.elementAt( i );
Integer amt = new Integer( ((Integer) rcd[1]).intValue() + 1 );
rcd[1] = amt;
diff--;
if( diff <= 0 ) break;
}
if( diff <= 0 ) break;
}
}
genOriginalSchedule();
}
private void adjustOriginalSchedule() {
System.out.println( "modify original schedule");
int demand = OrderDetailsManHourMap.getInstance().getDemandForDetailedStyle( orderID, styleID );
Calendar today = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd" );
sdf.setDateFormatSymbols(new DateFormatSymbols(Locale.US));
SimpleDateFormat sdfLong = new SimpleDateFormat( "MMM dd yyyy" );
sdfLong.setDateFormatSymbols(new DateFormatSymbols(Locale.US));
int diff = demand - getCumPlannedQty() - cumWIP;
diff = Math.abs( diff );
while( diff > 0 ) {
Enumeration keys = originalScheduleByDate.keys();
while( keys.hasMoreElements() ) {
String key = (String) keys.nextElement();
try{
if (sdf.parse(key.substring(4,10) + " " + key.substring( 24, 28)).before(today.getTime()))
continue;
} catch( Exception e) {
System.out.println( "Date parsing error -3: " + e.getMessage() );
}
Vector vec = (Vector) originalScheduleByDate.get( key );
for( int i=0; i<vec.size(); i++ ) {
Object[] rcd = (Object[]) vec.elementAt( i );
//Integer amt = new Integer( ((Integer) rcd[1]).intValue() + 1 );
Integer amt = (Integer) rcd[1];
if( amt.intValue() == 0 ) continue;
amt = new Integer( amt.intValue() - 1 );
rcd[1] = amt;
diff--;
if( diff <= 0 ) break;
}
if( diff <= 0 ) break;
}
}
genOriginalSchedule();
}
public void addOriginalAssignedEmployee(Date date, Integer opID, Integer amt ) {
originalAssignedEmployees.add( opID );
Object[] obj = new Object[2];
obj[0] = opID;
obj[1] = amt;
Vector vec = (Vector) originalScheduleByDate.get( date.toString() );
if( null == vec ) {
vec = new Vector();
originalScheduleByDate.put( date.toString(), vec );
}
vec.add( obj );
}
public void setPlannedQtyForYesterday( int amt ) {
plannedQtyForYesterday = amt;
}
public void print() {
for( int i=0; i<qtyByDate.size(); i++ ) {
Integer qty = (Integer) qtyByDate.elementAt( i );
System.out.print( qty.intValue() + ", " );
}
System.out.println();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -