🖥️ IT, 컴퓨터/🚀 최적화

[CPLEX] P-median 문제 코드 정리

김 홍시 2023. 10. 12.
반응형

CPLEX에서는 mod 파일과 dat 파일을 작성해야 한다.

 

mod 파일은 내가 만드는 모델의 틀을 만드는 곳이고,

dat 파일은 내가 모델에 넣을 데이터를 입력하는 곳이다.

 

모델 (mod) 파일 코드

 

// P-Median Probelm
main{   //메인 블록
	// Generating & Solving initial model
	thisOplModel.generate(); // OPL 모델을 만듦
		if (cplex.solve())
		{
		var ofile = new IloOplOutputFile("C:/Users/user/OneDrive - SNU/문서/cplex/P-Median-Problemanswer.txt");   //여기에 결과 저장 //대소문자 주의
		ofile.close();
		var obj = cplex.getObjValue();  //변수에 이름 지정. answer을 저장하는 obj
		writeln("The Value of the Objective Function Value is (Total Weighted Distance): ", obj);
		writeln("Solving CPU Elapsed Time in (Seconds): ", cplex.getCplexTime());
		}	
	else {
			writeln("No Solution");
 		}
	}
			
// indicies
{int} Warehouses =...; //...은 우리가 별도의 파일에 데이터를 저장했음을 의미함
{int} Customers =...;

// Parameters and Data
int MaxWarehousesP =...; //설치할 시설 개수
float Demand[Customers] =...;
float Distance[Warehouses][Customers]=...;

// Decision Variables
dvar boolean Open[Warehouses]; //Xj 1 or 0 
dvar boolean Assign[Warehouses][Customers]; //Yij 1 or 0

// Model

// Total demand weighted distance
minimize sum(w in Warehouses, c in Customers) Demand[c]*Distance[w][c]*Assign[w][c];   //목적함수 => sum Hi Di Yij

subject to{
  
  forall ( c in Customers)
	EachCustomersDemandMustBeMet:  // 제약조건 (1) 무조건 할당되어야 함
		sum( w in Warehouses ) Assign[w][c]==1;
		
	UseMaximum_P_Warehouses:	// 제약조건 (2) p개의 시설이 필요함 
	sum(w in Warehouses) Open[w]==MaxWarehousesP;
	
	forall (w in Warehouses, c in Customers) 
	CannotAssignCustomertoWH_UnlessItIsOpen: 	// 제약조건 (3) 특정 시설에만 수요 할당 가능
	Assign[w][c] <= Open[w];
}

 

데이터 (dat) 파일 코드

/*********************************************
 * OPL 22.1.0.0 Data
 * Author: user
 * Creation Date: 2022. 6. 3. at 오전 9:55:06
 *********************************************/



Warehouses = {Chicago,Atlanta,NewYork,StLouis,Detroit,Cincinnati,Pittsburgh,Charlotte,Boston}; // 현재 입지 후보지 9개
 Customers = {Chicago,AUanta,NewYork,StLouis,Detroit,Cincinnati,Pittsburgh,Charlotte,Boston}; // 여기에 수요가 있음
MaxWarehousesP = 3; //입지할 시설 개수 p

 Demand = [ 2870000,572000, 8450000	, 350000	, 901000 ,	333000 , 306000 , 723000 , 610000];
 Distance = [
	[0 ,	720 ,	790	,	297	,	283	,	296	,	461 ,	769	,	996]
	[720,  0,	884,	555,	722,	461,	685,	245,	1099]
	[790,	884,	0,	976,	614,	667,	371,	645,	219]
	[297,	555,	976,	0,	531,	359,	602,	715,	1217]
	[283,	722,	614,	531,	0,	263,	286,	629,	721]
	[296,	461,	667,	359,	263,	0,	288,	479,	907]
	[461,	685,	371,	602,	286,	288,	0,	448,	589]
	[769,	245,	645,	715,	629,	479,	448,	0,	867]
	[996,	1099,	219,	1217,	721,	907,	589,	867,	0]	];

 

참고

https://www.youtube.com/watch?v=XUFd0gLTrXk&t=983s 

 

반응형

댓글