Esempio n. 1
0
 /** Destructor. */
 ~Worker() {
    if ( handle ) {
       handle.kill();
       handle.join();
    }
    if ( cplex.getImpl() ) {
       cplex.userfunction(USERACTION_REMOVECALLBACK, 0, NULL, 0, 0, NULL);
       cplex.end();
    }
    rng.end();
    x.end();
    obj.end();
    model.end();
 }
Esempio n. 2
0
int main()
{
	clock_t start,finish;
	double totaltime;
	start=clock();	
	
	try{
/************************************变量初始化******************************************/		
		for(IloInt i=0;i<NG;++i)
		{
			P[i]=IloNumVarArray(env,NT,0,IloInfinity,ILOFLOAT);
			S3[i]=IloNumVarArray(env,NT,0,IloInfinity,ILOFLOAT);
			deta[i]=10;
		}		
/************************************数据读取******************************************/
		ifstream input_wind("wind_scenarios.txt",ios::in);//误差场景,由蒙特卡洛方法生成
		output<<endl<<"error sceanrios:"<<endl;
		for(IloInt w=0;w<NW;++w)
		{
			Pwind[w]=IloNumArray(env,NT);
			for(IloInt t=0;t<NT;++t)
			{
				input_wind>>Pwind[w][t];
				output<<Pwind[w][t]<<"   ";
			}
			output<<endl;
		}
		input_wind.close();

		ifstream input_commitment("uc.txt",ios::in);//机组启停状态
		output<<endl<<"Unit State:"<<endl;
		if(!input_commitment)
		{
			cerr<<"uc.txt is not exist!!!"<<endl;
		}
		for(IloInt i=0;i<NG;++i)
		{
			I[i]=IloNumArray(env,NT);
			for(IloInt t=0;t<NT;++t)
			{
				input_commitment>>I[i][t];
				output<<I[i][t]<<"  ";
			}
			output<<endl;
		}
		input_commitment.close();
		
		ifstream input_preoutput("Ppower.txt",ios::in);//预测场景下出力
		output<<endl<<"power of forecast sceanrios:"<<endl;
		if(!input_preoutput)
		{
			cerr<<"Ppower.txt is not exist"<<endl;
		}
		for(IloInt i=0;i<NG;++i)
		{
			Pre[i]=IloNumArray(env,NT);
			for(IloInt t=0;t<NT;++t)
			{
				input_preoutput>>Pre[i][t];
				output<<Pre[i][t]<<"  ";
			}
			output<<endl;
		}
		input_preoutput.close();

		ifstream input_load("load.txt",ios::in);//读取负荷信息
		output<<endl<<"Load:"<<endl;
		if(!input_load)
		{
			cerr<<"load.txt is not exist!!!"<<endl;
		}
		for(IloInt t=0; t<NT; ++t)
		{
			input_load>>Pload[t];
			output<<Pload[t]<<"   ";
		}
		input_load.close();
				
		ifstream input_BFile("Brach_File.txt",ios::in);//读取风电支路信息:起点,终点,电阻,电抗以及潮流上限
		output<<endl<<"Information of Branch:"<<endl;
		if(!input_BFile)
		{	
			output<<"Brach_File.txt is not exist!!!"<<endl;
		}
		for(IloInt k=0; k<Branch; ++k)
		{	
			Info_Branch[k]=IloNumArray(env,5);
			for(IloInt h=0; h<5; ++h)
			{
				input_BFile>>Info_Branch[k][h];
				output<<Info_Branch[k][h]<<"   ";
			}
			output<<endl;
		}
		input_BFile.close();
		
		ifstream input_unitinfo("unit_information.txt",ios::in);//读入机组信息
		output<<endl<<"Unit Information:"<<endl;
		if(!input_unitinfo)
		{
			cerr<<"unit_information.txt is not exist!!!"<<endl;
		}
		for(IloInt i=0;i<NG;++i)
		{
			Unit[i]=IloNumArray(env,12);//12条信息:结点、Pmin、Pmax、MinOn、MinOff、RU、RD、Startup Cost、c、b、a、price;
			for(IloInt k=0; k<12; ++k)
			{
				input_unitinfo>>Unit[i][k];
				output<<Unit[i][k]<<"   ";
			}
			output<<endl;
		}
		input_unitinfo.close();
		
		output<<endl<<endl<<"deta:"<<endl;
		for(IloInt i=0;i<NG;i++)
		{
			deta[i]=Unit[i][5]/10;
			output<<deta[i]<<"   ";
		}
		
		
		ifstream input_wind_locate("wind_locate.txt",ios::in);//风场分布
		output<<endl<<"wind locate:"<<endl;
		if(!input_wind_locate)
		{
			cerr<<"wind_locate is not exist!!!"<<endl;
		}
		for(IloInt k=0; k<Node; ++k)
		{
			input_wind_locate>>Sw[k];
			output<<Sw[k]<<"   ";
		}
		input_wind_locate.close();
		
		ifstream input_load_locate("load_locate.txt",ios::in);//负荷分布
		output<<endl<<"load locate:"<<endl;
		if(!input_load_locate)
		{
			cerr<<"load_locate is not exist!!!"<<endl;
		}
		for(IloInt k=0; k<Node; ++k)
		{
			input_load_locate>>Sw[k];
			output<<Sw[k]<<"   ";
		}
		input_load_locate.close();
		/////给矩阵B0赋值
		for(IloInt k=0; k<Node-1; ++k)//非对角线
		{
			B0[k]=IloNumArray(env,Node-1);
			for(IloInt h=0; h<Node-1; ++h)
			{
				if(k==h)
					continue;
				else
				{
					IloInt temp;
					IloInt b=0;
					for(; b<Branch; ++b)
					{
						if((Info_Branch[b][0]-1==k && Info_Branch[b][1]-1==h)
								|| (Info_Branch[b][0]-1==h && Info_Branch[b][1]-1==k))
						{
							temp=b;
							break;
						}
					}
					if(b==Branch)
						B0[k][h]=0;
					else
						B0[k][h]= -1.0/Info_Branch[temp][3];
				}
			}
		}
		for(IloInt k=0; k<Node-1; ++k)//对角线
		{
			B0[k][k]=0;
			for(IloInt h=0; h<Node-1; ++h)
			{
				if(k!=h)
				{
					B0[k][k]+=B0[k][h];
				}
			}
			IloNum last=0;
			IloInt b=0;
			IloInt temp;
			for(;b<Branch;++b)
			{
				if((Info_Branch[b][0]-1==k && Info_Branch[b][1]-1==Node-1)
								|| (Info_Branch[b][0]-1==Node-1 && Info_Branch[b][1]-1==k))
				{
					temp=b;
					break;
				}
			}
			if(b==Branch)
				last=0;
			else
				last=-1.0/Info_Branch[temp][3];
			B0[k][k]+=last;
			B0[k][k]=-1*B0[k][k];
		}
		/////赋值完毕	
		for(IloInt k=0; k<Node-1; ++k)
			B0l[k]=IloNumArray(env,Node-1);
			
		IloInvert(B0,B0l,Node-1);//求逆矩阵
/************************************模型******************************************/		
		IloNumExpr Cost(env);
		for(IloInt t=0;t<NT;++t)
		{
			Cost+=(S1[t]+S2[t]);
			for(IloInt i=0;i<NG;++i)			
			{
				Cost+=S3[i][t];
			}
		}
		for(IloInt h=0;h<Branch;++h)
		{
			Cost+=(S4[h]+S5[h]);
		}
		Check_Model.add(IloMinimize(env,Cost));//目标函数
		
		for(IloInt i=0; i<NG; ++i)//机组出力上下限约束
			for(IloInt t=0; t<NT; ++t)
			{
				Check_Model.add(P[i][t]<=Unit[i][2]*I[i][t]);
				Check_Model.add(P[i][t]>=Unit[i][1]*I[i][t]);
			}
		
		// for(IloInt i=0; i<NG; ++i)//机组爬坡约束,应该可以不用考虑
			// for(IloInt t=1; t<NT; ++t)
			// {
				// Check_Model.add(P[i][t]-P[i][t-1]<=(1-I[i][t]*(1-I[i][t-1]))*Unit[i][5]+I[i][t]*(1-I[i][t-1])*Unit[i][1]);
				// Check_Model.add(P[i][t-1]-P[i][t]<=(1-I[i][t-1]*(1-I[i][t]))*Unit[i][6]+I[i][t-1]*(1-I[i][t])*Unit[i][1]);
			// }
		
		for(IloInt t=0; t<NT; ++t)//机组功率平衡约束
		{
			IloNumExpr fire(env);
			IloNum wind(0);
			for(IloInt i=0; i<NG; ++i)
			{
				fire+=P[i][t];
			}
			for(IloInt w=0;w<NW;++w)
			{
				wind+=Pwind[w][t];
			}
			Check_Model.add(fire+S1[t]-S2[t]==Pload[t]-wind);
			fire.end();
		}

		for(IloInt i=0;i<NG;++i)//机组出力爬坡约束
			for(IloInt t=0;t<NT;++t)
			{
				Check_Model.add(P[i][t]-Pre[i][t]-S3[i][t]<=deta[i]);
				Check_Model.add(P[i][t]-Pre[i][t]+S3[i][t]>=-deta[i]);
			}
/************************************安全约束******************************************/		
		for(IloInt t=0; t<NT; ++t)
		{
			IloExprArray Pf(env,Branch);//潮流
			IloExprArray Psp(env,Node-1);//注入功率
			IloExprArray Theta(env,Node);//相角
			
			Theta[Node-1]=IloExpr(env);
			
			for(IloInt k=0; k<Node-1; ++k)
			{
				Psp[k]=IloExpr(env);
				IloInt i=0;
				for(;i<NG;++i)
				{
					if(Unit[i][0]-1==k)break;
				}
				if(i<NG)
				{
					Psp[k] +=P[i][t];
				}
				if(Sw[k]>=0)
				{
					Psp[k] +=Pwind[ Sw[k] ][t];
				}
				Psp[k] -= Sl[k]*Pload[t];
			}
			IloMutiply(B0l,Psp,Theta,Node-1);
			
			//计算潮流
			for(IloInt h=0; h<Branch; ++h)
			{
				Pf[h]=IloExpr(env);			
				Pf[h]+=(Theta[(IloInt)Info_Branch[h][0]-1]-Theta[(IloInt)Info_Branch[h][1]-1])/Info_Branch[h][3];
				Check_Model.add(Pf[h]-S4[h]<=Info_Branch[h][4]);
				Check_Model.add(Pf[h]+S5[h]>=-Info_Branch[h][4]);
			}
			Pf.end();
			Psp.end();
			Theta.end();
		}
/************************************模型求解及输出******************************************/
		Check_Cplex.solve();
		if(Check_Cplex.getStatus() == IloAlgorithm::Infeasible)
			output<< "No Solution" << endl;
		
		output<<endl<<"Cost:"<<Check_Cplex.getObjValue()<<endl;
		// for(IloInt i=0; i<NG; ++i)
		// {
			// for(IloInt t=0; t<NT; ++t)
			// {
				// output<<"机组 "<<i+1<<" 第 "<<t+1<<" 时段状态:"<<Check_Cplex.getValue(I[i][t])<<"   出力:";
				// output<<Check_Cplex.getValue(P[i][t])<<endl;
			// }
			// output<<endl;
		// }
		Check_Model.end();
		Check_Cplex.end();
		env.end();
	}
	catch(IloException& ex)//异常捕获
	{
		output<<"Error: "<<ex<<endl;
	}
	catch(...)
	{
		output<<"Error: Unknown exception caught!" << endl;
	}
	
	finish=clock();
	totaltime=(double)(finish-start)/CLOCKS_PER_SEC;
	output<<"totaltime: "<<totaltime<<"s"<<endl<<endl;
	output.close();	
	return 0;
}
Esempio n. 3
0
int main()
{
	clock_t start,finish;
	double totaltime;
	start=clock();

	try
	{
		output<<">>>>>>>>>>>>>>数据区<<<<<<<<<<<<<<<"<<endl;		
		define_data(env);//首先初始化全局变量
		output<<">>>>>>>>>>>>>>数据区结束<<<<<<<<<<<<<<<"<<endl;	
/*************************************************************目标函数*******************************************************/
		IloNumExpr Cost(env);

		for(IloInt i=0; i<NG; ++i) //运行成本及其线性化(青华姐论文)
			for(IloInt l=0; l<NL; ++l)
				for(IloInt t=0; t<NT; ++t)
				{
					Master_Model.add(Deta[i][l][t]<=Z[i][l+1]-Z[i][l]);
				}
		for(IloInt i=0; i<NG; ++i)
			for(IloInt t=0; t<NT; ++t)
			{
				IloNumExpr Deta_Sum(env);
				for(IloInt l=0; l<NL; ++l)
				{
					Deta_Sum+=Deta[i][l][t];
				}
				Master_Model.add(P[i][t]==Deta_Sum+Unit[i][1]*I[i][t]);
				Deta_Sum.end();
			}			
		for(IloInt i=0; i<NG; ++i)
		{
			for(IloInt t=0; t<NT; ++t)
			{
				IloNumExpr f1(env);
				f1+=(Unit[i][10]+Unit[i][9]*Unit[i][1]+Unit[i][8]*Unit[i][1]*Unit[i][1])*I[i][t];
				for(IloInt l=0; l<NL; ++l)
					f1+=Betaa[i][l]*Deta[i][l][t];
				Cost+=f1*Unit[i][11];
				f1.end();
			}
		}

		for(IloInt i=0; i<NG; ++i)//开机成本
			for(IloInt t=1; t<NT; ++t)
			{
				Cost+=Unit[i][11]*Unit[i][7]*I[i][t]*(1-I[i][t-1]);
			}

		for(IloInt i=0; i<NG; ++i)//停机成本
			for(IloInt t=1; t<NT; ++t)
			{
				Cost+=Unit[i][11]*Unit[i][7]*I[i][t-1]*(1-I[i][t]);
			}
		Master_Model.add(IloMinimize(env,Cost));
/********************************************************机组出力上下限约束**************************************************/
		for(IloInt i=0; i<NG; ++i)
			for(IloInt t=0; t<NT; ++t)
			{
				Master_Model.add(P[i][t]<=Unit[i][2]*I[i][t]);//最好分开写
				Master_Model.add(P[i][t]>=Unit[i][1]*I[i][t]);
			}
/*********************************************************机组爬坡约束******************************************************/
		for(IloInt i=0; i<NG; ++i)
			for(IloInt t=1; t<NT; ++t)
			{
				Master_Model.add(P[i][t]-P[i][t-1]<=(1-I[i][t]*(1-I[i][t-1]))*Unit[i][5]+I[i][t]*(1-I[i][t-1])*Unit[i][1]);
				Master_Model.add(P[i][t-1]-P[i][t]<=(1-I[i][t-1]*(1-I[i][t]))*Unit[i][6]+I[i][t-1]*(1-I[i][t])*Unit[i][1]);
			}
/*********************************************************机组功率平衡约束**************************************************/
		for(IloInt t=0; t<NT; ++t)
		{
			IloNumExpr fire(env);
			IloNum wind(0);
			for(IloInt i=0; i<NG; ++i)
			{
				fire+=P[i][t];
			}
			
			for(IloInt w=0;w<NW;++w)
			{
				wind+=Pwind[t][w];
			}
			
			Master_Model.add(fire==Pload[t]-wind);
			fire.end();
		}
/**********************************************************备用约束********************************************************/
		for(IloInt t=0; t<NT; ++t) //条件不全
		{
			IloNumExpr expr(env);
			for(IloInt i=0; i<NG; ++i)
			{
				expr+=Unit[i][2]*I[i][t];
			}
			Master_Model.add(expr>=R[t]+Pload[t]);
		}
/**********************************************************安全约束********************************************************/
		////求每个节点的注入功率
		for(IloInt t=0; t<NT; ++t)
		{
			IloExprArray Pf(env,Branch);//潮流
			IloExprArray Psp(env,Node-1);//注入功率
			IloExprArray Theta(env,Node);//相角
			
			Theta[Node-1]=IloExpr(env);
			
			for(IloInt k=0; k<Node-1; ++k)
			{
				Psp[k]=IloExpr(env);
				IloInt i=0;
				for(;i<NG;++i)
				{
					if(Unit[i][0]-1==k)break;
				}
				if(i<NG)
				{
					Psp[k] +=P[i][t];
				}
				if(Sw[k]>=0)
					Psp[k] += Pwind[t][ Sw[k] ] ;
				Psp[k] -= Sl[k]*Pload[t];
			}
			IloMutiply(B0l,Psp,Theta,Node-1);
			
			//计算潮流
			for(IloInt h=0; h<Branch; ++h)
			{
				Pf[h]=IloExpr(env);			
				Pf[h]+=(Theta[(IloInt)Info_Branch[h][0]-1]-Theta[(IloInt)Info_Branch[h][1]-1])/Info_Branch[h][3];
				Master_Model.add(Pf[h]<=Info_Branch[h][4]);
				Master_Model.add(Pf[h]>=-Info_Branch[h][4]);
			}
			Pf.end();
			Psp.end();
			Theta.end();
		}
/************************************************************最小开机时间约束*****************************************************/
		for(IloInt i=0; i<NG; ++i)
		{
			IloNumExpr expr1(env);
			for(IloInt t=0; t<=Unit[i][3]-1; ++t)
			{
				expr1+=I[i][t];
			}
			Master_Model.add((expr1-I[i][0]*Unit[i][3])>=0);
			expr1.end();

			for(IloInt t=1; t<=NT-Unit[i][3]; ++t)
			{
				IloNumExpr expr2(env);
				for(IloInt k=t; k<=t+Unit[i][3]-1; ++k)
				{
					expr2+=I[i][k];
				}
				Master_Model.add(expr2>=Unit[i][3]*(I[i][t]-I[i][t-1]));
				expr2.end();
			}
			
			for(IloInt t=NT-(IloInt)Unit[i][3]+1; t<=NT-1; ++t)
			{
				IloNumExpr expr2(env);
				for(IloInt h=t; h<=NT-1; ++h)
				{
					expr2+=(I[i][h]-(I[i][t]-I[i][t-1]));
				}
				Master_Model.add(expr2>=0);
				expr2.end();
			}
		}
/*******************************************************最小停机时间约束******************************************************/
		for(IloInt i=0; i<NG; ++i)
		{
			IloNumExpr expr1(env);
			for(IloInt t=0; t<=Unit[i][4]-1; ++t)
			{
				expr1+=(1-I[i][t]);
			}
			Master_Model.add((expr1-(1-I[i][0])*Unit[i][4])>=0);
			expr1.end();

			for(IloInt t=1; t<=NT-Unit[i][4]; ++t)
			{
				IloNumExpr expr1(env);
				for(IloInt k=t; k<=t+Unit[i][4]-1; ++k)
				{
					expr1+=(1-I[i][k]);
				}
				Master_Model.add(expr1>=Unit[i][4]*(I[i][t-1]-I[i][t]));
				expr1.end();
			}
			
			for(IloInt t=NT-(IloInt)Unit[i][4]+1; t<=NT-1; ++t)
			{
				IloNumExpr expr2(env);
				for(IloInt h=t; h<=NT-1; ++h)
				{
					expr2+=(1-I[i][h]-(I[i][t-1]-I[i][t]));
				}
				Master_Model.add(expr2>=0);
				expr2.end();
			}
		}

/*******************************************************以下是场景相关约束******************************************************/		
		for(IloInt s=0;s<Set;++s)
		{
			for(IloInt t=0;t<NT;++t)//功率平衡约束
			{
				IloNumExpr fire(env);
				for(IloInt i=0; i<NG; ++i)
				{
					fire+=Ps[s][i][t];
				}
				
				IloNum wind(0);
				for(IloInt w=0;w<NW;++w)
					wind+=Pswind[s][t][w];
					
				Master_Model.add(fire==Pload[t]-wind);
				fire.end();
			}
			
			for(IloInt i=0; i<NG; ++i)//机组出力上下限约束
				for(IloInt t=0; t<NT; ++t)
				{
					Master_Model.add(Ps[s][i][t]<=Unit[i][2]*I[i][t]);//最好分开写
					Master_Model.add(Ps[s][i][t]>=Unit[i][1]*I[i][t]);
				}
			
			for(IloInt i=0; i<NG; ++i)//建议行为约束
				for(IloInt t=0; t<NT; ++t)
				{
					Master_Model.add(Ps[s][i][t]-P[i][t]<=ddtt);
					Master_Model.add(-Ps[s][i][t]+P[i][t]<=ddtt);
				}
			
			for(IloInt t=0; t<NT; ++t)//安全约束
			{
				IloExprArray Pf(env,Branch);//潮流
				IloExprArray Psp(env,Node-1);//注入功率
				IloExprArray Theta(env,Node);//相角
				
				Theta[Node-1]=IloExpr(env);
				for(IloInt k=0; k<Node-1; ++k)
				{
					Psp[k]=IloExpr(env);
					
					IloInt i=0;
					for(;i<NG;++i)
					{
						if(Unit[i][0]-1==k)break;
					}
					if(i<NG)
					{
						Psp[k] +=Ps[s][i][t];
					}
					if(Sw[k]>=0)
						Psp[k] += Pswind[s][t][ Sw[k] ];
					Psp[k] -= Sl[k]*Pload[t];
				}
				IloMutiply(B0l,Psp,Theta,Node-1);
				
				//计算潮流
				for(IloInt h=0; h<Branch; ++h)
				{
					Pf[h]=IloExpr(env);			
					Pf[h]+=(Theta[(IloInt)Info_Branch[h][0]-1]-Theta[(IloInt)Info_Branch[h][1]-1])/Info_Branch[h][3];
					Master_Model.add(Pf[h]<=Info_Branch[h][4]);
					Master_Model.add(Pf[h]>=-Info_Branch[h][4]);
				}
				Pf.end();
				Psp.end();
				Theta.end();
			}			
		}		
		Master_Cplex.solve();                        
/************************************************************输出显示过程**************************************************/
		if (Master_Cplex.getStatus() == IloAlgorithm::Infeasible)//输出结果
			output << "No Solution" << endl;

		output<<"Cost:"<<Master_Cplex.getObjValue()<<endl;
		for(IloInt i=0; i<NG; ++i)
		{
			for(IloInt t=0; t<NT; ++t)
			{
				output<<"机组 "<<i+1<<" 第 "<<t+1<<" 时段状态:"<<Master_Cplex.getValue(I[i][t])<<"   出力:";
				output<<Master_Cplex.getValue(P[i][t])<<endl;
			}
			output<<endl;
		}
		
		Master_Model.end();
		Master_Cplex.end();
		env.end();
	}
	catch(IloException& ex)//异常捕获
	{
		cerr<<"Error: "<<ex<<endl;
	}
	catch(...)
	{
		cerr << "Error: Unknown exception caught!" << endl;
	}

	finish=clock();
	totaltime=(double)(finish-start)/CLOCKS_PER_SEC;
	output<<"totaltime: "<<totaltime<<"s"<<endl<<endl;
	
	output.close();
	return 0;
}
/********************************************程序入口**********************************************/
int main()
{
	clock_t start,finish;
	double totaltime;
	start=clock();
	time_t nowTime=time(0);
	struct tm* nowTimeStruct=localtime(&nowTime);//时间统计及打印系统时间
	output<<"系统当前时间:"<<1900+nowTimeStruct->tm_year<<"."<<nowTimeStruct->tm_mon+1<<"."<<
		nowTimeStruct->tm_mday<<"  "<<nowTimeStruct->tm_hour<<":"<<nowTimeStruct->tm_min<<":"
		<<nowTimeStruct->tm_sec<<endl;

	try
	{
		output<<">>>>>>>>>>>>>>>>>>>>>>>>>数据区<<<<<<<<<<<<<<<<<<<<"<<endl;
		define_data(env);//首先初始化全局变量
		output<<">>>>>>>>>>>>>>>>>>>>>>>>>/数据区<<<<<<<<<<<<<<<<<<<"<<endl<<endl;
/******************************************主问题目标函数*****************************************************/
		IloNumExpr Cost(env);
		
		for(IloInt i=0;i<NG;++i)//运行成本及其线性化
			for(IloInt l=0;l<NL;++l)
				for(IloInt t=0;t<NT;++t)
				{
					Master_Model.add(Deta[i][l][t]<=Z[i][l+1]-Z[i][l]);
				}
		for(IloInt i=0;i<NG;++i)
			for(IloInt t=0;t<NT;++t)
			{
				IloNumExpr Deta_Sum(env);
				for(IloInt l=0;l<NL;++l)
				{
					Deta_Sum+=Deta[i][l][t];
				}
				Master_Model.add(P[i][t]==Deta_Sum+Unit[i][1]*I[i][t]);
				Deta_Sum.end();
			}
		for(IloInt i=0;i<NG;++i)
		{
			for(IloInt t=0;t<NT;++t)
			{
				IloNumExpr f1(env);
				f1+=(Unit[i][10]+Unit[i][9]*Unit[i][1]+Unit[i][8]*Unit[i][1]*Unit[i][1])*I[i][t];
				for(IloInt l=0;l<NL;++l)
					f1+=Betaa[i][l]*Deta[i][l][t];
				Cost+=f1*Unit[i][11];
				f1.end();
			}
		}
		
		for(IloInt i=0; i<NG; ++i)//开机成本
			for(IloInt t=1; t<NT; ++t)
			{
				Cost+=Unit[i][11]*Unit[i][7]*I[i][t]*(1-I[i][t-1]);
			}
			
		for(IloInt i=0; i<NG; ++i)//停机成本
			for(IloInt t=1; t<NT; ++t)
			{
				Cost+=Unit[i][11]*Unit[i][7]*I[i][t-1]*(1-I[i][t]);
			}
		
		Master_Model.add(IloMinimize(env,Cost));
		Cost.end();
/*******************************************机组出力上下限约束*********************************************/
		for(IloInt i=0; i<NG; ++i)
			for(IloInt t=0; t<NT; ++t)
			{
				Master_Model.add(P[i][t]<=Unit[i][2]*I[i][t]);
				Master_Model.add(P[i][t]>=Unit[i][1]*I[i][t]);
			}
/*********************************************机组爬坡约束**************************************************/
		for(IloInt i=0; i<NG; ++i)
			for(IloInt t=1; t<NT; ++t)
			{
				Master_Model.add(P[i][t]-P[i][t-1]<=(1-I[i][t]*(1-I[i][t-1]))*
											Unit[i][5]+I[i][t]*(1-I[i][t-1])*Unit[i][1]);
				Master_Model.add(P[i][t-1]-P[i][t]<=(1-I[i][t-1]*(1-I[i][t]))*
											Unit[i][6]+I[i][t-1]*(1-I[i][t])*Unit[i][1]);
			}
/**********************************************机组功率平衡约束**********************************************/
		for(IloInt t=0; t<NT; ++t)
		{
			IloNumExpr fire(env);
			IloNum wind(0);
			for(IloInt i=0; i<NG; ++i)
			{
				fire+=P[i][t];
			}
			for(IloInt w=0;w<NW;++w)
			{
				wind+=Pwind[t][w];
			}
			Master_Model.add(fire==Pload[t]-wind);
			fire.end();
		}
/*************************************************备用约束************************************************/
		for(IloInt t=0; t<NT; ++t)
		{
			IloNumExpr expr(env);
			for(IloInt i=0;i<NG;++i)
			{
				expr+=Unit[i][2]*I[i][t];
			}
			Master_Model.add(expr>=Pload[t]+R[t]);
			expr.end();
		}
/*******************************************最小开机时间约束********************************************/
		for(IloInt i=0; i<NG; ++i)
		{
			IloNumExpr expr1(env);
			for(IloInt t=0; t<=Unit[i][3]-1; ++t)
			{
				expr1+=I[i][t];
			}
			Master_Model.add((expr1-I[i][0]*Unit[i][3])>=0);
			expr1.end();
			
			for(IloInt t=1; t<=NT-Unit[i][3]; ++t)
			{
				IloNumExpr expr2(env);
				for(IloInt k=t; k<=t+Unit[i][3]-1; ++k)
				{
					expr2+=I[i][k];
				}
				Master_Model.add(expr2>=Unit[i][3]*(I[i][t]-I[i][t-1]));
				expr2.end();
			}
			for(IloInt t=NT-(IloInt)Unit[i][3]+1; t<=NT-1; ++t)
			{
				IloNumExpr expr2(env);
				for(IloInt h=t; h<=NT-1; ++h)
				{
					expr2+=(I[i][h]-(I[i][t]-I[i][t-1]));
				}
				Master_Model.add(expr2>=0);
				expr2.end();
			}
		}
/***********************************************最小停机时间约束******************************************/
		for(IloInt i=0; i<NG; ++i)
		{
			IloNumExpr expr1(env);
			for(IloInt t=0; t<=Unit[i][4]-1; ++t)
			{
				expr1+=(1-I[i][t]);
			}
			Master_Model.add((expr1-(1-I[i][0])*Unit[i][4])>=0);
			expr1.end();
			
			for(IloInt t=1; t<=NT-Unit[i][4]; ++t)
			{	
				IloNumExpr expr1(env);
				for(IloInt k=t; k<=t+Unit[i][4]-1; ++k)
				{
					expr1+=(1-I[i][k]);
				}
				Master_Model.add(expr1>=Unit[i][4]*(I[i][t-1]-I[i][t]));
				expr1.end();
			}
			for(IloInt t=NT-(IloInt)Unit[i][4]+1; t<=NT-1; ++t)
			{
				IloNumExpr expr2(env);
				for(IloInt h=t; h<=NT-1; ++h)
				{
					expr2+=(1-I[i][h]-(I[i][t-1]-I[i][t]));
				}
				Master_Model.add(expr2>=0);
				expr2.end();
			}						
		}		
/*************************************************安全约束*********************************************/
		IloInvert(env,B0,B0l,Node-1);//求逆矩阵
		for(int t=0;t<NT;++t)//校验所有时段的所有支路的潮流
		{
			IloNumExprArray Psp(env,Node-1);//注入功率
			IloExprArray Theta(env,Node);//相角				
			
			Theta[Node-1]=IloExpr(env);
				
			for(IloInt k=0; k<Node-1; ++k)//校验了所有结点的潮流
			{
				Psp[k]=IloNumExpr(env);
				
				IloInt i=0;
				for(;i<NG;++i)
				{
					if(Unit[i][0]-1==k)break;
				}
				if(i<NG)
					Master_Model.add(P_dual[t][k]==P[i][t]);	
				else
					Master_Model.add(P_dual[t][k]==0);

				Psp[k] += P_dual[t][k];
				if(Sw[k]>=0)
					Psp[k] += Pwind[t][ Sw[k] ];

				Psp[k] -= Sl[k]*Pload[t];
			}
			
			IloMutiply(env,B0l,Psp,Theta,Node-1);
			//计算潮流
			for(IloInt h=0; h<Branch; ++h)
			{
				Master_Model.add(PL[t][h]==(Theta[(IloInt)Info_Branch[h][0]-1]-
									Theta[(IloInt)Info_Branch[h][1]-1])/Info_Branch[h][3]);
				Master_Model.add(PL[t][h]<=Info_Branch[h][4]);
				Master_Model.add(-PL[t][h]<=Info_Branch[h][4]);
			}
			Psp.end();
			Theta.end();
		}
		
		// Master_Cplex.extract(Master_Model);
		Master_Cplex.solve();
		if (Master_Cplex.getStatus() == IloAlgorithm::Infeasible)//输出结果
		{
			output<<"Master Problem Have No Solution"<<endl;
			goto lable2;
		}
/**********************************************输出显示过程**********************************************/
		output<<"Cost:"<<Master_Cplex.getObjValue()<<endl;
		for(IloInt t=0;t<NT;++t)
		{
			output<<"时段 "<<t+1<<" 机组的启停(I):"; 
			output<<endl;
			for(IloInt i=0;i<NG;++i)
			{
				output<<Master_Cplex.getValue(I[i][t])<<"   ";
			}
			output<<endl;
			output<<"时段 "<<t+1<<" 机组的出力(P):";
			output<<endl;
			for(IloInt i=0;i<NG;++i)
			{
				output<<Master_Cplex.getValue(P[i][t])<<"   ";
			}
			output<<endl;
			output<<"时段 "<<t+1<<" 线路潮流(PL):";
			output<<endl;
			for(IloInt b=0;b<Branch;++b)
			{
				output<<Master_Cplex.getValue(PL[t][b])<<"   ";
			}
			output<<endl<<endl<<endl;
		}
		Master_Model.end();
		Master_Cplex.end();
		env.end();
	}
	catch(IloException& ex)//异常捕获
	{
		output<<"Error: "<<ex<<endl;
	}
	catch(...)
	{
		output<<"Error: Unknown exception caught!" << endl;
	}
	
	lable2:
	finish=clock();
	totaltime=(double)(finish-start)/CLOCKS_PER_SEC;
	output<<"totaltime: "<<totaltime<<"s"<<endl<<endl;

	output.close();	
	return 0;
}
Esempio n. 5
0
/**************************************************程序入口****************************************************/
int main()
{
	clock_t start,finish;
	double totaltime;
	start=clock();
	
	try
	{
		define_data(env);//首先初始化全局变量		
		IloInvert(env,B0,B0l,Node-1);//求逆矩阵
/*************************************************************主问题目标函数*******************************************************/
		IloNumExpr Cost(env);
		for(IloInt w=0;w<NW;++w)
		{
			Cost+=detaPw[w];
		}		
		 Master_Model.add(IloMinimize(env,Cost));
		//Master_Model.add(IloMaximize(env,Cost));//目标函数二先一
		Cost.end();
/********************************************************机组出力上下限约束**************************************************/
		IloNumExpr expr1(env),expr2(env);
		for(IloInt i=0;i<NG;++i)
		{
			expr1+=detaP[i];
		}
		for(IloInt w=0;w<NW;++w)
		{
			expr2+=detaPw[w];
		}
		Master_Model.add(expr1==expr2);
		expr1.end();
		expr2.end();
		
		for(IloInt i=0;i<NG;++i)
		{
			Master_Model.add(detaP[i]>=Unit[i][1]*u[i]-P1[i]);
			Master_Model.add(detaP[i]<=Unit[i][2]*u[i]-P1[i]);
			
			Master_Model.add(detaP[i]>=-detaa[i]);
			Master_Model.add(detaP[i]<=detaa[i]);
		}
		
		IloNumExprArray detaP_node(env,Node-1),detaPw_node(env,Node-1);
		IloNumExprArray Theta(env,Node);
		
		for(IloInt b=0;b<Node-1;++b)
		{
			detaP_node[b]=IloNumExpr(env);
			detaPw_node[b]=IloNumExpr(env);
			IloInt i=0;
			for(;i<NG;++i)
			{
				if(Unit[i][0]==b-1)break;
			}
			if(i<NG)
			{
				detaP_node[b]+=detaP[i];
			}
			else
			{
				detaP_node[b]+=0;
			}
			
			
			if(Sw[b]>=0)
			{
				detaPw_node[b]+=detaPw[ Sw[b] ];
			}
			else
			{
				detaPw_node[b]+=0;
			}
		}
		
		for(IloInt b=0;b<Node-1;++b)
		{
			Theta[b]=IloNumExpr(env);
			for(IloInt k=0;k<Node-1;++k)
			{			
				Theta[b]+=B0l[b][k]*(detaP_node[k]+detaPw_node[k]);	
			}
		}
		Theta[Node-1]=IloNumExpr(env);
		
		for(IloInt h=0;h<Branch;++h)
		{
			IloNumExpr exprTheta(env);//莫明其妙的错误
			exprTheta+=(Theta[(IloInt)Info_Branch[h][0]-1]-Theta[(IloInt)Info_Branch[h][1]-1]);
			
			Master_Model.add(exprTheta<=Info_Branch[h][3]*(Info_Branch[h][4]-PL[h]));			
			Master_Model.add(exprTheta>=Info_Branch[h][3]*(-Info_Branch[h][4]-PL[h]));
			exprTheta.end();
			//两个相减的节点顺序没有影响么?
		}
		Theta.end();
		detaP_node.end();
		detaPw_node.end();
		
		Master_Cplex.extract(Master_Model);
		Master_Cplex.solve();
		if (Master_Cplex.getStatus() == IloAlgorithm::Infeasible)//输出结果
		{
			output<<"Master Problem Have No Solution"<<endl;
			goto lable2;
		}
		
/************************************************************输出显示过程**************************************************/
		output<<endl<<"Min/Max:"<<Master_Cplex.getObjValue()<<endl;
		
		lable2:		
		Master_Model.end();
		Master_Cplex.end();
		env.end();
	}
	catch(IloException& ex)//异常捕获
	{
		output<<"Error: "<<ex<<endl;
	}
	catch(...)
	{
		output<<"Error: Unknown exception caught!" << endl;
	}
	
	finish=clock();
	totaltime=(double)(finish-start)/CLOCKS_PER_SEC;
	output<<"totaltime: "<<totaltime<<"s"<<endl<<endl;
	output.close();	
	return 0;
}
Esempio n. 6
0
/**************************************************程序入口****************************************************/
int main()
{
	clock_t start,finish;
	double totaltime;
	start=clock();
	
	try
	{
		define_data(env);//首先初始化全局变量
/*************************************************************主问题目标函数*******************************************************/
		IloNumExpr Cost(env);
		
		for(IloInt i=0;i<NG;++i)//运行成本及其线性化
			for(IloInt l=0;l<NL;++l)
				for(IloInt t=0;t<NT;++t)
				{
					Master_Model.add(Deta[i][l][t]<=Z[i][l+1]-Z[i][l]);
				}
		for(IloInt i=0;i<NG;++i)
			for(IloInt t=0;t<NT;++t)
			{
				IloNumExpr Deta_Sum(env);
				for(IloInt l=0;l<NL;++l)
				{
					Deta_Sum+=Deta[i][l][t];
				}
				Master_Model.add(P[i][t]==Deta_Sum+Unit[i][1]*I[i][t]);
				Deta_Sum.end();
			}
		for(IloInt i=0;i<NG;++i)
		{
			for(IloInt t=0;t<NT;++t)
			{
				IloNumExpr f1(env);
				f1+=(Unit[i][10]+Unit[i][9]*Unit[i][1]+Unit[i][8]*Unit[i][1]*Unit[i][1])*I[i][t];
				for(IloInt l=0;l<NL;++l)
					f1+=Betaa[i][l]*Deta[i][l][t];
				Cost+=f1*Unit[i][11];
				f1.end();
			}
		}
		
		for(IloInt i=0; i<NG; ++i)//开机成本
			for(IloInt t=1; t<NT; ++t)
			{
				Cost+=Unit[i][11]*Unit[i][7]*I[i][t]*(1-I[i][t-1]);
			}
			
		for(IloInt i=0; i<NG; ++i)//停机成本
			for(IloInt t=1; t<NT; ++t)
			{
				Cost+=Unit[i][11]*Unit[i][7]*I[i][t-1]*(1-I[i][t]);
			}
		
		Master_Model.add(IloMinimize(env,Cost));
		Cost.end();
/********************************************************机组出力上下限约束**************************************************/
		for(IloInt i=0; i<NG; ++i)
			for(IloInt t=0; t<NT; ++t)
			{
				Master_Model.add(P[i][t]<=Unit[i][2]*I[i][t]);
				Master_Model.add(P[i][t]>=Unit[i][1]*I[i][t]);
			}
/*********************************************************机组爬坡约束******************************************************/
		for(IloInt i=0; i<NG; ++i)
			for(IloInt t=1; t<NT; ++t)
			{
				Master_Model.add(P[i][t]-P[i][t-1]<=(1-I[i][t]*(1-I[i][t-1]))*Unit[i][5]+I[i][t]*(1-I[i][t-1])*Unit[i][1]);
				Master_Model.add(P[i][t-1]-P[i][t]<=(1-I[i][t-1]*(1-I[i][t]))*Unit[i][6]+I[i][t-1]*(1-I[i][t])*Unit[i][1]);
			}
/*********************************************************机组功率平衡约束**************************************************/
		for(IloInt t=0; t<NT; ++t)
		{
			IloNumExpr fire(env);
			IloNum wind(0);
			for(IloInt i=0; i<NG; ++i)
			{
				fire+=P[i][t];
			}
			for(IloInt w=0;w<NW;++w)
			{
				wind+=Pwind[t][w];
			}
			Master_Model.add(fire==Pload[t]-wind);
			fire.end();
		}
/**********************************************************备用约束********************************************************/
		for(IloInt t=0; t<NT; ++t) 
		{
			IloNumExpr expr(env);
			for(IloInt i=0;i<NG;++i)
			{
				expr+=Unit[i][2]*I[i][t];
			}
			Master_Model.add(expr>=Pload[t]+R[t]);
			expr.end();
		}
/***********************************************************最小开机时间约束*************************************************/
		for(IloInt i=0; i<NG; ++i)
		{
			IloNumExpr expr1(env);
			for(IloInt t=0; t<=Unit[i][3]-1; ++t)
			{
				expr1+=I[i][t];
			}
			Master_Model.add((expr1-I[i][0]*Unit[i][3])>=0);
			expr1.end();
			
			for(IloInt t=1; t<=NT-Unit[i][3]; ++t)
			{
				IloNumExpr expr2(env);
				for(IloInt k=t; k<=t+Unit[i][3]-1; ++k)
				{
					expr2+=I[i][k];
				}
				Master_Model.add(expr2>=Unit[i][3]*(I[i][t]-I[i][t-1]));
				expr2.end();
			}
			for(IloInt t=NT-(IloInt)Unit[i][3]+1; t<=NT-1; ++t)
			{
				IloNumExpr expr2(env);
				for(IloInt h=t; h<=NT-1; ++h)
				{
					expr2+=(I[i][h]-(I[i][t]-I[i][t-1]));
				}
				Master_Model.add(expr2>=0);
				expr2.end();
			}			
		}
/*******************************************************最小停机时间约束**************************************************/
		for(IloInt i=0; i<NG; ++i)
		{
			IloNumExpr expr1(env);
			for(IloInt t=0; t<=Unit[i][4]-1; ++t)
			{
				expr1+=(1-I[i][t]);
			}
			Master_Model.add((expr1-(1-I[i][0])*Unit[i][4])>=0);
			expr1.end();
			
			for(IloInt t=1; t<=NT-Unit[i][4]; ++t)
			{	
				IloNumExpr expr1(env);
				for(IloInt k=t; k<=t+Unit[i][4]-1; ++k)
				{
					expr1+=(1-I[i][k]);
				}
				Master_Model.add(expr1>=Unit[i][4]*(I[i][t-1]-I[i][t]));
				expr1.end();
			}
			for(IloInt t=NT-(IloInt)Unit[i][4]+1; t<=NT-1; ++t)
			{
				IloNumExpr expr2(env);
				for(IloInt h=t; h<=NT-1; ++h)
				{
					expr2+=(1-I[i][h]-(I[i][t-1]-I[i][t]));
				}
				Master_Model.add(expr2>=0);
				expr2.end();
			}						
		}
/************************************************************开始整个迭代过程***************************************************************/
		IloInvert(B0,B0l,Node-1);//求逆矩阵

		IloInt while_interator=0;
		IloBool first_cut_is_add;
		
		while(1)
		{
			first_cut_is_add=IloFalse;
			output<<"主问题迭代次数:"<<while_interator++<<endl;
			Master_Cplex.extract(Master_Model);			
			Master_Cplex.solve();
			
			if (Master_Cplex.getStatus() == IloAlgorithm::Infeasible)//输出结果
				output<<"Master Problem Have No Solution"<<endl;
			else 
				output<<"Master Problem Have Solution, the objective value is: "<<Master_Cplex.getObjValue()<<endl<<endl;
				
			for(IloInt i=0; i<NG; ++i)//保存机组启停状态
			{
				Master_Cplex.getValues(I[i],u[i]);
				Master_Cplex.getValues(P[i],P1[i]);
			}
/*****************************************************主问题安全约束********************************************************/
			for(IloInt t=0;t<NT;++t)//主问题安全约束的校验
			{
				output<<"Master Problem Security Constraint Check:"<<t+1<<endl;
				IloNumVarArray C1(env,Branch,0,IloInfinity,ILOFLOAT),C2(env,Branch,0,IloInfinity,ILOFLOAT);
				IloNumExprArray PL(env,Branch);//各条支路的潮流
				IloNumVarArray P_dual(env,Node-1);
				IloRangeArray range(env,Node-1);
				for(IloInt h=0;h<Branch;++h)
				{
					C1[h]=IloNumVar(env);
					C2[h]=IloNumVar(env);
					PL[h]=IloNumVar(env);
				}				
				IloModel Master_Model_Security(env);
				
				IloNumExpr obj(env);//目标函数
				for(IloInt h=0;h<Branch;++h)
				{
					obj+=(C1[h]+C2[h]);
				}
				Master_Model_Security.add(IloMinimize(env,obj));
				obj.end();
				
				IloNumExprArray Psp(env,Node-1);//注入功率
				IloExprArray Theta(env,Node);//相角
				
				Theta[Node-1]=IloExpr(env);
					
				for(IloInt k=0; k<Node-1; ++k)//校验了所有结点的潮流
				{
					P_dual[k]=IloNumVar(env);
					Psp[k]=IloNumExpr(env);
					
					IloInt i=0;
					for(;i<NG;++i)
					{
						if(Unit[i][0]-1==k)break;
					}
					if(i<NG)
						range[k]=IloRange::IloRange(env,P1[i][t],P_dual[k],P1[i][t]);	
					else
						range[k]=IloRange::IloRange(env,0,P_dual[k],0);

					Master_Model_Security.add(range[k]);
					Psp[k] += P_dual[k];
					if(Sw[k]>=0)
						Psp[k] += Pwind[t][ Sw[k] ];
					Psp[k] -= Sl[k]*Pload[t];
				}				
				IloMutiply(B0l,Psp,Theta,Node-1);
				//计算潮流
				for(IloInt h=0; h<Branch; ++h)
				{
					PL[h]=IloNumExpr(env);
					PL[h]+=(Theta[(IloInt)Info_Branch[h][0]-1]-Theta[(IloInt)Info_Branch[h][1]-1])/Info_Branch[h][3];
					Master_Model_Security.add(PL[h]-C1[h]<=Info_Branch[h][4]);
					Master_Model_Security.add(-PL[h]-C2[h]<=Info_Branch[h][4]);
				}
				Psp.end();
				Theta.end();
				
				IloCplex Master_Security_Cplex(Master_Model_Security);
				Master_Security_Cplex.solve();
					
				if (Master_Security_Cplex.getStatus() == IloAlgorithm::Infeasible)//输出结果
					output << "Master_Security_Problem Have No Solution"<<endl;
				else
					output<<"Master_Security_Problem Have Solution, the objective value is: "<<Master_Security_Cplex.getObjValue()<<endl<<endl;
				
				if(Master_Security_Cplex.getObjValue()>limit)
				{
					IloNumExpr cut(env);
					cut+=Master_Security_Cplex.getObjValue();
					for(IloInt i=0;i<NG;++i)
					{
						if(Unit[i][0]-1>=Node-1)//最后一个结点没算
							continue;
						else
							cut+=Master_Security_Cplex.getDual(range[(IloInt)Unit[i][0]-1])*(P[i][t]-P1[i][t]);//+(I[i][t]-u[i][t]));
																//仅仅对功率进行调整
					}
					Master_Model.add(cut<=0);
					cut.end();

					first_cut_is_add=IloTrue;
				}
				C1.end();//清理
				C2.end();
				PL.end();
				P_dual.end();
				range.end();
				Master_Model_Security.end();
				Master_Security_Cplex.end();
			}
			
			if(first_cut_is_add==IloTrue)
			{
				output<<"主问题安全约束越限,返回主问题>>>"<<endl;
				continue;//如果有添加割,则重新开始
			}
/****************************************************************场景的校验****************************************************************/			
			IloInt s=0;
			IloBool second_cut_is_add=IloFalse;
			for(; s<set; ++s)//场景的校验
			{	
				output<<"Num_Senario_Iterator(场景):"<<s+1<<endl;
				
				IloNumVarArray S1S(env,NT,0,IloInfinity,ILOFLOAT),S2S(env,NT,0,IloInfinity,ILOFLOAT);//第二阶段松弛变量
								//S3S(env,NT,0,IloInfinity,ILOFLOAT),S4S(env,NT,0,IloInfinity,ILOFLOAT);
				IloArray<IloNumVarArray> S5S(env,NG);
				for(IloInt i=0; i<NG; ++i)
					S5S[i]=IloNumVarArray(env,NT,0.0,IloInfinity,ILOFLOAT);
				
				second_cut_is_add=IloFalse;

				IloModel Sub_Model(env,"Sub_Model");
/********************************子问题目标函数**************************************/
				IloNumExpr VS(env);
				for(IloInt t=0; t<NT; ++t)
				{
					VS+=(S1S[t]+S2S[t]);					
					for(IloInt i=0; i<NG; ++i)
						VS+=S5S[i][t];
				}
				Sub_Model.add(IloMinimize(env,VS));
				VS.end();
/*****************************子问题系统平衡约束*********************************/
				for(IloInt t=0; t<NT; ++t)
				{
					IloNumExpr expr1(env);
					for(IloInt i=0; i<NG; ++i)
						expr1+=PS[i][t];
					expr1+=S1S[t]-S2S[t];
					
					IloNum wind(0);
					for(IloInt w=0;w<NW;++w)
						wind+=Pswind[s][t][w];
						
					Sub_Model.add(expr1==Pload[t]-wind);
					expr1.end();
				}
/***************************子问题建议行为约束*************************************/
				IloArray<IloRangeArray> ramp1(env,NG),ramp2(env,NG); 
				for(IloInt i=0; i<NG; ++i)//绝对值转化为两个约束
				{
					ramp1[i]=IloRangeArray(env,NT);
					ramp2[i]=IloRangeArray(env,NT);
					for(IloInt t=0; t<NT; ++t)
					{
						ramp1[i][t]=IloRange::IloRange(env,-IloInfinity,PS[i][t]-S5S[i][t],detaa+P1[i][t],"ramp1");
						ramp2[i][t]=IloRange::IloRange(env,-IloInfinity,-PS[i][t]-S5S[i][t],detaa-P1[i][t],"ramp2");
						
						Sub_Model.add(ramp1[i][t]);
						Sub_Model.add(ramp2[i][t]);
					}
				}
/**************************子问题机组出力上下限约束*******************************/
				IloArray<IloRangeArray> range_limit1(env,NG),range_limit2(env,NG);
				for(IloInt i=0; i<NG; ++i)
				{
					range_limit1[i]=IloRangeArray(env,NT);
					range_limit2[i]=IloRangeArray(env,NT);
					for(IloInt t=0; t<NT; ++t)
					{
						range_limit1[i][t]=IloRange::IloRange(env,-IloInfinity,PS[i][t],Unit[i][2]*u[i][t],"power_limits1");
						range_limit2[i][t]=IloRange::IloRange(env,-IloInfinity,-PS[i][t],-Unit[i][1]*u[i][t],"power_limits2");
						Sub_Model.add(range_limit1[i][t]);
						Sub_Model.add(range_limit2[i][t]);
					}
				}
/*************************************添加割************************************/
				IloCplex Sub_Cplex(Sub_Model);
				label1:
				Sub_Cplex.extract(Sub_Model);
				Sub_Cplex.solve();
				
				IloNum objvalue(Sub_Cplex.getObjValue());
				if(Sub_Cplex.getStatus() == IloAlgorithm::Infeasible)//输出结果
				{
					output<< "Senario_Sub_Problem No Solution" << endl;
					output << "场景 "<< s+1 <<" 无解,跳出循环,主问题无解……"<<endl;
				}
				else 
					output<<"Senario_Sub_Problem Have Solution, and objective is: "<<objvalue<<endl<<endl;
				
				if(objvalue>limit)//返回主问题
				{
					IloNumExpr expr(env);
					expr+=objvalue;
					for(IloInt i=0;i<NG;++i)
						for(IloInt t=0;t<NT;++t)
						{
							expr+=/*0.1**/(Sub_Cplex.getDual(range_limit1[i][t])*Unit[i][2]-Sub_Cplex.getDual(range_limit2[i][t])*Unit[i][1])
									*(I[i][t]-u[i][t]);//+/*0.1**//*(Sub_Cplex.getDual(ramp1[i][t])-Sub_Cplex.getDual(ramp2[i][t]))*(P[i][t]-P1[i][t]);*/
						}
					Master_Model.add(expr<=0);
					range_limit1.end();
					range_limit2.end();
					ramp1.end();
					ramp2.end();
					expr.end();
					output<<"场景校验子问题不满足,返回主问题>>>"<<endl;
					break;//跳出场景校验子问题
				}
				else//否则开始安全校验
				{
					IloNumArray2 PS1(env,NG);//提取相应场景下的出力
					for(IloInt i=0;i<NG;++i)
					{
						PS1[i]=IloNumArray(env,NT);
						Sub_Cplex.getValues(PS[i],PS1[i]);
					}
					
					for(IloInt t=0;t<NT;++t)//对所有时段进行安全校核
					{
						output<<"Sub Problem Security Constraint Check:"<<t+1<<endl;
						IloNumVarArray C1(env,Branch,0,IloInfinity,ILOFLOAT),C2(env,Branch,0,IloInfinity,ILOFLOAT);
						IloNumExprArray PL(env,Branch);
						IloNumVarArray P_dual(env,Node-1);
						IloRangeArray range(env,Node-1);
						for(IloInt l=0;l<Branch;++l)
						{
							C1[l]=IloNumVar(env);
							C2[l]=IloNumVar(env);
							PL[l]=IloNumExpr(env);
						}						
						IloModel SubModel_Security(env);//只是局部变量
						
						IloNumExpr obj(env);//目标函数
						for(IloInt l=0;l<Branch;++l)
							obj+=C1[l]+C2[l];
						SubModel_Security.add(IloMinimize(env,obj));
						obj.end();
						
						IloNumExprArray Psp(env,Node-1);//注入功率
						IloExprArray Theta(env,Node);//相角
						
						Theta[Node-1]=IloExpr(env);
							
						for(IloInt k=0; k<Node-1; ++k)//求Psp
						{
							P_dual[k]=IloNumVar(env);
							Psp[k]=IloNumExpr(env);
							
							IloInt i=0;
							for(;i<NG;++i)
							{
								if(Unit[i][0]-1==k)break;
							}
							if(i<NG)
								range[k]=IloRange::IloRange(env,PS1[i][t],P_dual[k],PS1[i][t]);
							else
								range[k]=IloRange::IloRange(env,0,P_dual[k],0);
								
							SubModel_Security.add(range[k]);
							Psp[k] += P_dual[k];
							if(Sw[k]>=0)
								Psp[k] += Pswind[s][t][ Sw[k] ];
							Psp[k] -= Sl[k]*Pload[t];
						}						
						IloMutiply(B0l,Psp,Theta,Node-1);
						//计算潮流
						for(IloInt h=0; h<Branch; ++h)
						{
							PL[h]=IloNumExpr(env);			
							PL[h]+=(Theta[(IloInt)Info_Branch[h][0]-1]-Theta[(IloInt)Info_Branch[h][1]-1])/Info_Branch[h][3];							
							SubModel_Security.add(PL[h]-C1[h]<=Info_Branch[h][4]);
							SubModel_Security.add(-PL[h]-C2[h]<=Info_Branch[h][4]);
						}
						IloCplex Senario_Security_Cplex(SubModel_Security);
						Senario_Security_Cplex.solve();
						IloNum security_objvalue(Senario_Security_Cplex.getObjValue());
						Psp.end();
						Theta.end();
						PL.end();
						
						if (Senario_Security_Cplex.getStatus() == IloAlgorithm::Infeasible)//输出结果
							output<<"Senario_Security_Problem one No Solution" << endl;
						else 
							output<<"Senario_Security_Problem Have Solution, and objective is: "<<security_objvalue<<endl<<endl;

						if(security_objvalue>limit)
						{
							IloNumExpr cut(env);
							cut+=security_objvalue;
							
							for(IloInt i=0;i<NG;++i)
							{
								if(Unit[i][0]-1>=Node-1)
									continue;
								else
									cut+=Senario_Security_Cplex.getDual(range[(IloInt)Unit[i][0]-1])*(PS[i][t]-PS1[i][t]);//安全约束割
							}
							Sub_Model.add(cut<=0);
							cut.end();							
							second_cut_is_add=IloTrue;
						}
						C1.end();
						C2.end();
						PL.end();
						P_dual.end();
						range.end();
						SubModel_Security.end();
						Senario_Security_Cplex.end();
					}
					
					if(second_cut_is_add==IloTrue)//返回当前场景校验
					{
						output<<"goto label1,场景校验子问题安全约束越限,返回当前子问题>>>"<<endl;
						second_cut_is_add=IloFalse;
						goto label1;
					}
					PS1.end();
				}
				Sub_Model.end();
				Sub_Cplex.end();
			}
			if(s==set)break;
		}
/************************************************************输出显示过程**************************************************/
		output<<"Cost:"<<Master_Cplex.getObjValue()<<endl;
		for(IloInt i=0; i<NG; ++i)
		{
			for(IloInt t=0; t<NT; ++t)
			{
				output<<"机组 "<<i+1<<" 第 "<<t+1<<" 时段状态:"<<Master_Cplex.getValue(I[i][t])<<"   出力:";
				output<<Master_Cplex.getValue(P[i][t])<<endl;
			}
			output<<endl;
		}
		Master_Model.end();
		Master_Cplex.end();
		env.end();
	}
	catch(IloException& ex)//异常捕获
	{
		output<<"Error: "<<ex<<endl;
	}
	catch(...)
	{
		output<<"Error: Unknown exception caught!" << endl;
	}
	
	finish=clock();
	totaltime=(double)(finish-start)/CLOCKS_PER_SEC;
	output<<"totaltime: "<<totaltime<<"s"<<endl<<endl;
	output.close();	
	return 0;
}
Esempio n. 7
0
   /** Create a new worker.
    * The constructor mainly does the following:
    * - create an IloCplex instance that refers to a remote worker,
    * - load the model in <code>modelfile</code>,
    * - setup parameters depending on this worker's index,
    * - start an asynchronous solve.
    * If anything fails then an exception will be thrown.
    * @param env The environment used for instantiating Ilo* objects.
    * @param i The index of the worker to be created. This also
    *          determines the parameter settings to use in this worker.
    * @param s  A pointer to the global solve state.
    * @param transport  The transport name for the IloCplex constructor.
    * @param argc       The argument count for the IloCplex constructor.
    * @param argv       The array of transport arguments for the IloCplex
    *                   constructor.
    * @param modelfile  Name of the model to be loaded into the worker.
    * @param output     The output mode.
    * @param objdiff    The minimal difference between so that two
    *                   consecutive objective function values are considered
    *                   different.
    */
   Worker(IloEnv env, int i, SolveState *s, char const *transport,
          int argc, char const **argv, char const *modelfile,
          OUTPUT output, double objdiff)
      : idx(i), state(s), model(env), cplex(0), handle(0),
        primal(IloInfinity), dual(-IloInfinity),
        obj(env), x(env), rng(env), infoHandler(this), outb(idx), outs(&outb)
   {
      try {
         // Create remote object, setup output and load the model.
         cplex = IloCplex(model, transport, argc, argv);
         switch (output) {
         case OUTPUT_SILENT:
            // Disable output on the output and warning stream.
            cplex.setOut(env.getNullStream());
            cplex.setWarning(env.getNullStream());
            break;
         case OUTPUT_PREFIXED:
            // Redirect output to our custom stream.
            cplex.setOut(outs);
            cplex.setWarning(outs);
            break;
         case OUTPUT_LOG:
            // Nothing to do here. By default output is enabled.
            break;
         }
         cplex.importModel(model, modelfile, obj, x, rng);
         if ( obj.getSense() == IloObjective::Minimize ) {
            primal = -IloInfinity;
            dual = IloInfinity;
         }

         // We set the thread count for each solver to 1 so that we do not
         // run into problems if multiple solves are performed on the same
         // machine.
         cplex.setParam(IloCplex::Param::Threads, 1);
         // Each worker runs with a different random seed. This way we
         // get different paths through the tree even if the other
         // parameter settings are the same.
         cplex.setParam(IloCplex::Param::RandomSeed, idx);
         // Apply parameter settings.
         for (class ParamValue const *vals = settings[idx % NUMSETTINGS].values;
              vals->isValid(); ++vals)
            vals->apply(cplex);

         // Install callback and set objective change.
         int status = cplex.userfunction (USERACTION_ADDCALLBACK,
                                          0, NULL, 0, 0, NULL);
         if ( status )
            throw status;
         IloCplex::Serializer s;
         s.add(objdiff);
         status = cplex.userfunction (USERACTION_CHANGEOBJDIFF,
                                      s.getRawLength(), s.getRawData(),
                                      0, 0, NULL);
         if ( status )
            throw status;

         // Register the handler that will process info messages sent
         // from the worker.
         cplex.setRemoteInfoHandler(&infoHandler);

         // Everything is setup. Launch the asynchronous solve.
         handle = cplex.solve(true);
      } catch (...) {
         // In case of an exception we need to take some special
         // cleanup actions. Note that if we get here then the
         // solve cannot have been started and we don't need to
         // kill or join the asynchronous solve.
         if ( cplex.getImpl() )
            cplex.end();
         rng.end();
         x.end();
         obj.end();
         model.end();
         throw;
      }
   }
/**************************************************程序入口****************************************************/
int main()
{
	clock_t start,finish;
	double totaltime;
	start=clock();
	time_t nowTime=time(0);
	struct tm* nowTimeStruct=localtime(&nowTime);//打印系统时间
	// output<<"系统当前时间:"<<1900+nowTimeStruct->tm_year<<"."<<nowTimeStruct->tm_mon+1<<"."<<
		// nowTimeStruct->tm_mday<<"  "<<nowTimeStruct->tm_hour<<":"<<nowTimeStruct->tm_min<<":"<<nowTimeStruct->tm_sec<<endl;
	
	try
	{
		// output<<">>>>>>>>>>>>>>>>>>>>>>>>>数据区<<<<<<<<<<<<<<<<<<<<"<<endl;
		define_data(env);//首先初始化全局变量
		// output<<">>>>>>>>>>>>>>>>>>>>>>>>>/数据区<<<<<<<<<<<<<<<<<<<"<<endl<<endl;
		
		IloInvert(env,B0,B0l,Node-1);//求逆矩阵
		
		//在此创建两种形式的目标函数
		IloNumExpr Cost(env);//目标函数			
		for(IloInt w=0;w<NW;++w)
		{
			Cost+=detaPw[w];
		}
		IloObjective min(env,Cost,IloObjective::Sense::Minimize,"min");
		IloObjective max(env,Cost,IloObjective::Sense::Maximize,"max");	
		Master_Model.add(min);
		// Master_Model.add(IloMaximize(env,Cost));//目标函数二选一
		Cost.end();

		IloNumExpr expr1(env),expr2(env);//功率平衡约束
		for(IloInt i=0;i<NG;++i)
		{
			expr1+=detaP[i];
		}
		for(IloInt w=0;w<NW;++w)
		{
			expr2+=detaPw[w];
		}
		Master_Model.add(expr1+expr2==0);
		expr1.end();
		expr2.end();
		
		for(IloInt i=0;i<NG;++i)//机组可调节范围
		{
			Master_Model.add(detaP[i]>=Unit[i][1]*u[i]-P1[i]);
			Master_Model.add(detaP[i]<=Unit[i][2]*u[i]-P1[i]);
			
			Master_Model.add(detaP[i]>=-detaa[i]);
			Master_Model.add(detaP[i]<=detaa[i]);
		}
		
		IloNumExprArray detaP_node(env,Node-1),detaPw_node(env,Node-1);//安全约束,实际上安全约束影响不大
		IloNumExprArray Theta(env,Node);
		
		for(IloInt b=0;b<Node-1;++b)
		{
			detaP_node[b]=IloNumExpr(env);
			detaPw_node[b]=IloNumExpr(env);
			IloInt i=0;
			for(;i<NG;++i)
			{
				if(Unit[i][0]==b-1)break;
			}
			
			if(i<NG)
			{
				detaP_node[b]+=detaP[i];
			}	
			
			if(Sw[b]>=0)
			{
				detaPw_node[b]+=detaPw[ Sw[b] ];
			}
		}
		
		for(IloInt b=0;b<Node-1;++b)
		{
			Theta[b]=IloNumExpr(env);
			for(IloInt k=0;k<Node-1;++k)
			{			
				Theta[b]+=B0l[b][k]*(detaP_node[k]+detaPw_node[k]);	
			}
		}
		Theta[Node-1]=IloNumExpr(env);
		
		for(IloInt h=0;h<Branch;++h)
		{
			IloNumExpr exprTheta(env);//莫明其妙的错误
			exprTheta+=(Theta[(IloInt)Info_Branch[h][0]-1]-Theta[(IloInt)Info_Branch[h][1]-1]);
			
			Master_Model.add(exprTheta<=Info_Branch[h][3]*(Info_Branch[h][4]-PL[h]));			
			Master_Model.add(exprTheta>=Info_Branch[h][3]*(-Info_Branch[h][4]-PL[h]));
			exprTheta.end();
			//两个相减的节点顺序没有影响么?
		}
		Theta.end();
		detaP_node.end();
		detaPw_node.end();
		
		Master_Cplex.extract(Master_Model);
		Master_Cplex.solve();
		if (Master_Cplex.getStatus() == IloAlgorithm::Infeasible)//输出结果
		{
			output<<"Master Problem Have No Solution"<<endl;
			goto lable2;
		}
		
/************************************************************输出显示过程**************************************************/
		
		// output/*<<endl<<"Min:"*/<<Master_Cplex.getObjValue()<<endl;
		
		Master_Model.remove(min);
		Master_Model.add(max);
		Master_Cplex.extract(Master_Model);
		Master_Cplex.solve();
		if (Master_Cplex.getStatus() == IloAlgorithm::Infeasible)//输出结果
		{
			output<<"Master Problem Have No Solution"<<endl;
			goto lable2;
		}
		output/*<<endl<<"Max:"*/<<Master_Cplex.getObjValue()<<endl<<endl;;
		
		// output<<endl<<"常规机组出力调整量:"<<endl;
		// for(IloInt i=0;i<NG;++i)
		// {
			// output<<Master_Cplex.getValue(detaP[i])<<"  ";
		// }
		// output<<endl<<"风电机组出力调整量:"<<endl;
		// for(IloInt i=0;i<NW;++i)
		// {
			// output<<Master_Cplex.getValue(detaPw[i])<<"  ";
		// }
		// output<<endl;
		
		lable2:
		Master_Model.end();
		Master_Cplex.end();
		env.end();
	}
	catch(IloException& ex)//异常捕获
	{
		output<<"Error: "<<ex<<endl;
	}
	catch(...)
	{
		output<<"Error: Unknown exception caught!" << endl;
	}
	
	finish=clock();
	totaltime=(double)(finish-start)/CLOCKS_PER_SEC;
	output<<"totaltime: "<<totaltime<<"s"<<endl<<endl;
	output.close();	
	return 0;
}