//  ¼öÁ¤ÇÏ´ø °Íµé~~
/*
#include <ilcplex/ilocplex.h>
#include <ilconcert/iloalg.h>
#include <time.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
#include <stdlib.h>
#include <math.h>

using namespace std;
#define UTIME						2
#define MTIME						1
#define LTIME						1
#define JOBNUM						5

ILOSTLBEGIN

typedef IloArray<IloNumVarArray> NumVarMatrix;				// Ã·ÀÚ°¡ 2°³ÀÎ Real º¯¼ö¸¦ À§ÇÑ 2Â÷¿ø Çà·Ä 
typedef IloArray<IloNumArray> NumMatrix;

int
main (void) {
   IloEnv env;
   try {
      IloModel model(env);
  		char *buffer = NULL;
		buffer = new char[200];
		FILE *f;

		IloInt i,j,k;
		
		NumMatrix ptime(env, JOBNUM);

			ptime[0]=IloNumArray(env, 9, 35, 32, 30, 59, 31, 63, 36, 37, 35);			
			ptime[1]=IloNumArray(env, 9, 44, 45, 30, 33, 21, 38, 26, 42, 46);
			ptime[2]=IloNumArray(env, 9, 35, 32, 30, 59, 31, 63, 36, 37, 35);
			ptime[3]=IloNumArray(env, 9, 35, 32, 30, 59, 31, 63, 36, 37, 35);
			ptime[4]=IloNumArray(env, 9, 25, 42, 20, 29, 31, 35, 46, 27, 22);
/*
		int show[JOBNUM][9];
		for (i=0; i<JOBNUM; i++)
			for (j=0;j<9;j++)
				if (ptime[i][j]>0)
					show[i][j] = 1;
		
	//	show[2][7] = 0;							// skip
	//	show[1][2] = 0;		show[1][7] = 0;		show[4][7] = 0;		show[5][7] = 0;
	//	show[5][2] = 0;		show[3][7] = 0;

		IloNumVar makespan(env, 0, IloInfinity);

		NumVarMatrix ut(env, JOBNUM);
		NumVarMatrix mt(env, JOBNUM);
		NumVarMatrix lt(env, JOBNUM);
		NumVarMatrix pt(env, JOBNUM);
		NumVarMatrix m2t(env, JOBNUM);
		for (i=0;i<JOBNUM;i++){
			ut[i]=IloNumVarArray(env, 10, 0, IloInfinity, ILOFLOAT);
			mt[i]=IloNumVarArray(env, 10, 0, IloInfinity, ILOFLOAT);
			lt[i]=IloNumVarArray(env, 10, 0, IloInfinity, ILOFLOAT);
			m2t[i]=IloNumVarArray(env, 10, 0, IloInfinity, ILOFLOAT);
			pt[i]=IloNumVarArray(env, 10, 0, IloInfinity, ILOFLOAT);
		}

		



			
			for (i=0; i<JOBNUM; i++){				// conjunctive arc modeling
				for (j=0; j<10; j++){
//					if (show[i][j] == 1) {						
						
						model.add(mt[i][j] >= ut[i][j] + UTIME);
						model.add(lt[i][j] >= mt[i][j] + MTIME);
						model.add(m2t[i][j] >= lt[i][j] + LTIME);
						model.add(pt[i][j] == lt[i][j] + LTIME);
/*					}
					else {
						model.add(mt[i][j] >= ut[i][j]);
						model.add(lt[i][j] >= mt[i][j]);
						model.add(m2t[i][j] >= lt[i][j]);
						model.add(pt[i][j] == lt[i][j]);

					}
				}
				
				for (j=0;j<9;j++) {					// conjunctive arc unloading with/out no-wait
					if (show[i][j] == 1) {
						if (j==1 || j==3 || j==5) model.add(ut[i][j+1] == pt[i][j]+ptime[i][j]);
						else model.add(ut[i][j+1] >= pt[i][j]+ptime[i][j]);
					}
					else {
						model.add(ut[i][j+1] >= pt[i][j]);
					}
				}				
			}
			
			for (j=0; j<9; j++) {					// type 1 (X-shape) disjunctive arc modeling
				for (i=0; i<JOBNUM-1; i++) {
					for (k=i+1; k<JOBNUM; k++) {
						if (show[i][j]==1 && show[k][j]==1)
						model.add(ut[k][j] >= m2t[i][j+1]+MTIME || ut[i][j] >= m2t[k][j+1]+MTIME);
					}
				}
			}
			
			for (i=0; i<JOBNUM; i++) {			// type 2 (double arrow) disjunctive arc modeling
				for (k=0; k<JOBNUM; k++) {
					if (i != k) {
						if (show[i][0] ==1 && show[k][2])
							model.add(ut[i][0] >= m2t[k][2]+MTIME || ut[k][2] >= m2t[i][0]+MTIME);
						if (show[i][5] ==1 && show[k][7])
							model.add(ut[i][5] >= m2t[k][7]+MTIME || ut[k][7] >= m2t[i][5]+MTIME);
						if (show[i][5] ==1 && show[k][8])
							model.add(ut[i][5] >= m2t[k][8]+MTIME || ut[k][8] >= m2t[i][5]+MTIME);
						if (show[i][6] ==1 && show[k][8])
							model.add(ut[i][6] >= m2t[k][8]+MTIME || ut[k][8] >= m2t[i][6]+MTIME);
					}
				}
			}


			for (i=0; i<JOBNUM; i++){
					model.add(ut[i][0] >= 0);
					model.add(makespan >= lt[i][9]+LTIME);				
//					model.add(makespan >= m2t[i][9]+MTIME);
			}

		model.add(IloMinimize(env, makespan));


      IloCplex cplex(model);
		cplex.extract(model);
		cplex.exportModel("protolpex1.lp");  	    

	  if (cplex.solve()) {
		  cplex.out() << "objective value : " << cplex.getObjValue() << endl;

		  f = fopen("LP_output.txt","w");	
		  
		  float mspan,uval,mval,lval,pval,m2val;
		  mspan = cplex.getValue(makespan);
		  sprintf(buffer, "makespan = %f \n",mspan);
		  fputs(buffer,f);
		  sprintf(buffer, "var \t mark \t row \t col \t start \t dur \n");
		  fputs(buffer,f);
		  
		  for (i=0;i<JOBNUM;i++){
			for (j=0;j<10;j++){
				uval = cplex.getValue(ut[i][j]);
				sprintf(buffer, "ut[%d][%d]\t U \t %d \t %d \t %f \t %d \n",i,j,i,j,uval,UTIME);
				fputs(buffer,f);
				mval = cplex.getValue(mt[i][j]);
				sprintf(buffer, "mt[%d][%d]\t M \t %d \t %d \t %f \t %d \n",i,j,i,j,mval,MTIME);
				fputs(buffer,f);
				lval = cplex.getValue(lt[i][j]);
				sprintf(buffer, "lt[%d][%d]\t L \t %d \t %d \t %f \t %d \n",i,j,i,j,lval,LTIME);
				fputs(buffer,f);
				m2val = cplex.getValue(m2t[i][j]);
				sprintf(buffer, "m2t[%d][%d]\t m2 \t %d \t %d \t %f \t %d \n",i,j,i,j,m2val,MTIME);
				fputs(buffer,f);

				if (j<9) {
				pval = cplex.getValue(pt[i][j]);
				sprintf(buffer, "pt[%d][%d]\t P \t %d \t %d \t %f \t %f \n",i,j,i,j,pval,ptime[i][j]);
				fputs(buffer,f);
				}
			}
		  }
	  }

	  int alkdjfalkdfjald=1;
 /*   env.out() << "Solution status = " << cplex.getStatus() << endl;
      env.out() << "Solution value  = " << cplex.getObjValue() << endl;

      IloNumArray vals(env);
      cplex.getValues(vals, var);
      env.out() << "Values        = " << vals << endl;
      cplex.getSlacks(vals, con);
      env.out() << "Slacks        = " << vals << endl;  
		
   } 
 //  catch (IloException& e) {
//      cerr << "Concert exception caught: " << e << endl;
//   }
   catch (...) {
      cerr << "Unknown exception caught" << endl;
   }

   env.end();
   return 0;

}  // END main
*/



#include <ilcplex/ilocplex.h>
#include <ilconcert/iloalg.h>
#include <time.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
#include <stdlib.h>
#include <math.h>

using namespace std;
#define UTIME						11
#define MTIME						6
#define LTIME						8
#define JOBNUM						5

ILOSTLBEGIN

typedef IloArray<IloNumVarArray> NumVarMatrix;				// Ã·ÀÚ°¡ 2°³ÀÎ Real º¯¼ö¸¦ À§ÇÑ 2Â÷¿ø Çà·Ä 
typedef IloArray<IloNumArray> NumMatrix;

int
main (void) {
   IloEnv env;
   try {
      IloModel model(env);
  		char *buffer = NULL;
		buffer = new char[200];
		FILE *f;

		IloInt i,j,k;
		
		NumMatrix ptime(env, JOBNUM);

			ptime[0]=IloNumArray(env, 9, 35, 32, 30, 59, 31, 63, 36, 37, 35);			
			ptime[1]=IloNumArray(env, 9, 44, 45, 30, 33, 21, 38, 26, 42, 46);
			ptime[2]=IloNumArray(env, 9, 69, 32, 30, 59, 31, 33, 36, 37, 35);
			ptime[3]=IloNumArray(env, 9, 35, 32, 30, 59, 31, 23, 36, 37, 35);
			ptime[4]=IloNumArray(env, 9, 25, 42, 20, 29, 31, 15, 46, 27, 22);
		
			
//			ptime[10]=IloNumArray(env, 9, 45., 32., 30., 59., 31., 63., 36., 37., 9.);


		IloNumVar makespan(env, 0, IloInfinity);

		NumVarMatrix ut(env, JOBNUM);
		NumVarMatrix mt(env, JOBNUM);
		NumVarMatrix lt(env, JOBNUM);
		NumVarMatrix pt(env, JOBNUM);
		NumVarMatrix m2t(env, JOBNUM);
		for (i=0;i<JOBNUM;i++){
			ut[i]=IloNumVarArray(env, 10, 0, IloInfinity, ILOFLOAT);
			for(j=0;j<10;j++)
			{
				sprintf(buffer,"ut[%d][%d]",i,j);
				ut[i][j].setName(buffer);

			}
			mt[i]=IloNumVarArray(env, 10, 0, IloInfinity, ILOFLOAT);
			for(j=0;j<10;j++)
			{
				sprintf(buffer,"mt[%d][%d]",i,j);
				mt[i][j].setName(buffer);

			}
			lt[i]=IloNumVarArray(env, 10, 0, IloInfinity, ILOFLOAT);
			for(j=0;j<10;j++)
			{
				sprintf(buffer,"lt[%d][%d]",i,j);
				lt[i][j].setName(buffer);

			}
			m2t[i]=IloNumVarArray(env, 10, 0, IloInfinity, ILOFLOAT);
			for(j=0;j<10;j++)
			{
				sprintf(buffer,"m2t[%d][%d]",i,j);
				m2t[i][j].setName(buffer);

			}
			pt[i]=IloNumVarArray(env, 10, 0, IloInfinity, ILOFLOAT);
			for(j=0;j<10;j++)
			{
				sprintf(buffer,"pt[%d][%d]",i,j);
				pt[i][j].setName(buffer);

			}
		}
			
			
			for (i=0; i<JOBNUM; i++)							// conjunctive arc modeling
			{
				for (j=0; j<10; j++)
				{
					model.add(mt[i][j] >= ut[i][j]+UTIME);
					model.add(lt[i][j] >= mt[i][j]+MTIME);
					model.add(m2t[i][j] >= lt[i][j]+LTIME);
					model.add(pt[i][j] == lt[i][j]+LTIME);
				}

				for (j=0; j<9; j++){				// conjunctive arc unloading with/out no-wait
					if (j==1 || j==3 || j==5) {model.add(ut[i][j+1] == pt[i][j]+ptime[i][j]);}
					else model.add(ut[i][j+1] >= pt[i][j]+ptime[i][j]);
				}
			}
			
			for (j=0; j<9; j++) {					// type 1 (X-shape) disjunctive arc modeling
				for (i=0; i<JOBNUM-1; i++) {
					for (k=i+1; k<JOBNUM; k++) {
						model.add(ut[k][j] >= m2t[i][j+1]+MTIME || ut[i][j] >= m2t[k][j+1]+MTIME);
					}
				}
			}
			
			for (i=0; i<JOBNUM; i++) {			// type 2 (double arrow) disjunctive arc modeling
				for (k=0; k<JOBNUM; k++) {
					if (i != k) {
						model.add(ut[i][0] >= m2t[k][2]+MTIME || ut[k][2] >= m2t[i][0]+MTIME);
						model.add(ut[i][5] >= m2t[k][7]+MTIME || ut[k][7] >= m2t[i][5]+MTIME);
						model.add(ut[i][5] >= m2t[k][8]+MTIME || ut[k][8] >= m2t[i][5]+MTIME);
						model.add(ut[i][6] >= m2t[k][8]+MTIME || ut[k][8] >= m2t[i][6]+MTIME);
					}
				}
			}


			for (i=0; i<JOBNUM; i++){
					model.add(ut[i][0] >= 0);
					model.add(makespan >= lt[i][9]+LTIME);				
			}

		model.add(IloMinimize(env, makespan));


      IloCplex cplex(model);
		cplex.extract(model);
		cplex.exportModel("protolpex1.lp");  	    

	  if (cplex.solve()) {
		  cplex.out() << "objective value : " << cplex.getObjValue() << endl;

		  f = fopen("LP_output.txt","w");	
		  
		  float mspan,uval,mval,lval,pval,m2val;
		  mspan = cplex.getValue(makespan);
		  sprintf(buffer, "makespan = %f \n",mspan);
		  fputs(buffer,f);
		  sprintf(buffer, "var \t mark \t row \t col \t start \t dur \n");
		  fputs(buffer,f);
		  
		  for (i=0;i<JOBNUM;i++){
			for (j=0;j<10;j++){
				uval = cplex.getValue(ut[i][j]);
				sprintf(buffer, "ut[%d][%d]\t U \t %d \t %d \t %f \t %d \n",i,j,i,j,uval,UTIME);
				fputs(buffer,f);
				mval = cplex.getValue(mt[i][j]);
				sprintf(buffer, "mt[%d][%d]\t M \t %d \t %d \t %f \t %d \n",i,j,i,j,mval,MTIME);
				fputs(buffer,f);
				lval = cplex.getValue(lt[i][j]);
				sprintf(buffer, "lt[%d][%d]\t L \t %d \t %d \t %f \t %d \n",i,j,i,j,lval,LTIME);
				fputs(buffer,f);
				m2val = cplex.getValue(m2t[i][j]);
				sprintf(buffer, "m2[%d][%d]\t m2 \t %d \t %d \t %f \t %d \n",i,j,i,j,m2val,MTIME);
				fputs(buffer,f);

				if (j<9) {
				pval = cplex.getValue(pt[i][j]);
				sprintf(buffer, "pt[%d][%d]\t P \t %d \t %d \t %f \t %f \n",i,j,i,j,pval,ptime[i][j]);
				fputs(buffer,f);
				}
			}
		  }
	  }

 /*   env.out() << "Solution status = " << cplex.getStatus() << endl;
      env.out() << "Solution value  = " << cplex.getObjValue() << endl;

      IloNumArray vals(env);
      cplex.getValues(vals, var);
      env.out() << "Values        = " << vals << endl;
      cplex.getSlacks(vals, con);
      env.out() << "Slacks        = " << vals << endl;  */
		
   } 
	catch (IloException& e) {
      cerr << "Concert exception caught: " << e << endl;
   }
   catch (...) {
      cerr << "Unknown exception caught" << endl;
   }

   env.end();
   return 0;

}  // END main


