Originally posted by: zhangguangwei
My expressions only contain linear and quadrtic terms, just like the following code and formulas.
All variable is marked with yellow. I do not know where the error lies.
Please help me check the code, thank you very much
#include <ilcplex/ilocplex.h>
ILOSTLBEGIN
typedef IloArray<IloNumVarArray> NumVarMatrix;
const IloInt T = 24;
int
main (int argc, char **argv)
{
IloEnv env;
try {
IloModel model(env);
IloNumArray PG_Max(env),PG_Min(env),P_up(env),P_down(env),c_s(env),b_s(env),a_s(env),
load(env),
P_h(env),
P_c(env),
q(env),
m(env),
r_iniend(env),
r_maxmin(env),
C1234(env),
u0(env);
ifstream in("含抽水蓄能电站优化.dat");
in >>PG_Max>>PG_Min>>P_up>>P_down>>c_s>>b_s>>a_s>>load>>P_h>>P_c>>q>>m>>r_iniend>>r_maxmin>>C1234>>u0;
IloInt n = PG_Max.getSize();
NumVarMatrix PG(env, T);
for(IloInt t=0;t<T;t++)
{
PG[t] = IloNumVarArray(env, PG_Min , PG_Max, IloNumVar::Float);
}
NumVarMatrix u(env, T);
for(IloInt t=0;t<T;t++)
{ if(t==0)
{
u[t] = IloNumVarArray(env, u0,u0, IloNumVar::Bool);
}
else
{
u[t] = IloNumVarArray(env, n, 0, 1, IloNumVar::Bool);
}
}
IloNumVarArray Pg(env,T, P_h[1], P_h[0]);
IloNumVarArray Pp(env,T, P_c[1], P_c[0]);
IloNumVarArray qg(env,T, q[1], q[0]);
IloNumVarArray qp(env,T, m[1], m[0]);
IloNumVarArray r (env,T+1, r_maxmin[1], r_maxmin[0]);
//object
IloExpr expr(env);
for(IloInt t=0;t<T;t++)
{
for(IloInt j=0;j<n;j++)
{
expr += u[t][j]*(a_s[j]*PG[t][j]*PG[t][j]+b_s[j]*PG[t][j]+c_s[j]);
}
}
model.add(IloMinimize(env, expr));
expr.end();
//subject to
for(IloInt t=0;t<T;t++)
{
IloExpr expr1(env);
for(IloInt i=0;i<n;i++)
{
expr1 += u[t][i]*PG[t][i];
}
model.add(expr1 + Pg[t] - Pp[t] == load[t]);// (1)
model.add(Pg[t]==C1234[0]*qg[t]*qg[t] + C1234[1]*qg[t]);// (2)
model.add(Pp[t]==C1234[0]*qp[t]*qp[t] + C1234[1]*qp[t]);// (3)
expr1.end();
}
for(IloInt t=0;t<T;t++)
{
model.add(r[t+1]-r[t]+(qg[t]-qp[t]) == 0);// (4)
model.add(qg[t]*qp[t] == 0);// (7)
}
model.add(r[T] == r_iniend[1]);// (5)
model.add(r[0] == r_iniend[0]);// (6)
for(IloInt t=1;t<T;t++)
{
for(IloInt i=0;i<n;i++)
{
model.add(-P_down[i] <= u[t][i]*PG[t][i]-u[t-1][i]*PG[t-1][i] <= P_up[i]); // (8)
}
}
for(IloInt t=0;t<T;t++)
{
IloExpr expr2(env);
for(IloInt i=0;i<n;i++)
{
expr2 += u[t][i]*(P_up[i]-PG[t][i]);
}
model.add(expr2 >= 0.1*load[t]);// (9)
expr2.end();
}
IloCplex cplex(model);
cplex.exportModel("含抽水蓄能电站优化.lp");
if (cplex.solve()) {
cplex.out() << "Solution status: " << cplex.getStatus() << endl;
cplex.out() << "Total cost = " << cplex.getObjValue() << endl;
}
else cplex.out()<< "No solution" << endl;
}
catch (IloAlgorithm::CannotExtractException &e) {
IloExtractableArray &failed = e.getExtractables();
std::cerr << "Failed to extract:" << std::endl;
for (IloInt i = 0; i < failed.getSize(); ++i)
std::cerr << "\t" << failed[i] << std::endl;}
catch (IloException& e) {
cerr << "Concert exception caught: " << e << endl;
}
catch (...) {
cerr << "Unknown exception caught" << endl;
}
env.end();
return 0;
}
#CPLEXOptimizers#DecisionOptimization