Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  concert exception caught: not enough memory

    Posted 09/11/17 10:58 PM

    Originally posted by: zhangguangwei


    I use vs2010 with cplex12.60. I want solve a MIP problem.This is my code. When I run my project, the screen is displayed " concert exception caught: not enough memory". How to deal with this error?

              

     

     

             

    #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_low(env), c_s(env), b_s(env), a_s(env),
                      load(env), P_hk(env),  P_ck(env),  q_k(env), m_k(env),  rk_iniend(env), r_k(env),
                      C1234(env), u0(env);
          ifstream  in("含抽水蓄能电站优化.dat");
          in >>PG_Max>>PG_Min>>P_up>>P_low>>c_s>>b_s>>a_s>>load>>P_hk>>P_ck>>q_k>>m_k
               >> rk_iniend>>r_k>>C1234>>u0;
          IloInt  generators = 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, generators, 0, 1, IloNumVar::Bool);
                 }
             }
          IloNumVarArray Pgk(env,T, P_hk[1], P_hk[0]);
          IloNumVarArray Ppk(env,T, P_ck[1], P_ck[0]);
          IloNumVarArray qgk(env,T, q_k[1], q_k[0]);
          IloNumVarArray qpk(env,T, m_k[1], m_k[0]);
          IloNumVarArray r(env,T+1, r_k[1], r_k[0]);

         

          //object


          IloExpr expr(env);
          for(IloInt t=0;t<T;t++)
             {
                 for(IloInt j=0;j<generators;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


          IloExpr expr1(env);
          for(IloInt t=0;t<T;t++)
            {
                for(IloInt i=0;i<generators;i++)
                 {
                      expr +=u[t][i]*PG[t][i];
                 }
                model.add(expr + Pgk[t] - Ppk[t] == load[t]);//系统总功率平衡方程
                model.add(Pgk[t]==C1234[0]*qgk[t]*qgk[t] + C1234[1]*qgk[t]);//抽水蓄能电站功率-水量平衡方程
                model.add(Ppk[t]==C1234[0]*qpk[t]*qpk[t] + C1234[1]*qpk[t]);
            }
          for(IloInt t=0;t<T;t++)
           {
               model.add(r[t+1]-r[t]+(qgk[t]-qpk[t])==0);//水库容动态平衡
               model.add(qgk[t]*qpk[t]==0);//抽水发电功率互补
           }
          model.add(r[T]==rk_iniend[1]);
          model.add(r[0]==rk_iniend[0]);
          for(IloInt t=1;t<T;t++)
            {
                for(IloInt i=0;i<generators;i++)
                 {
                      model.add(-P_low[i] <= u[t][i]*PG[t][i]-u[t-1][i]*PG[t-1][i] <= P_up[i]);
                 }
            }
     IloExpr expr2;
     for(IloInt t=0;t<T;t++)
            {
                for(IloInt i=0;i<generators;i++)
                {
                      expr2 +=u[t][i]*(P_up[i]-PG[t][i]);
                 }
                model.add(expr2 >= 0.1*load[t]);
            }

          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 (IloException& e) {
          cerr << "Concert exception caught: " << e << endl;
       }
       catch (...) {
          cerr << "Unknown exception caught" << endl;
       }

       env.end();
       return 0;
    }

     

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: concert exception caught: not enough memory

    Posted 09/12/17 02:22 AM

    You need to fix your code. In line 35 you have

    if ( t = 0 )

    This resets t to 0 every time the statement is executed, resulting in an endless loop that will eventually exhaust memory. You probably meant to write 't == 0' instead.

    I also suggest to enable/crank up compiler warnings because the compiler will warn you about mistakes like this.

    There are more blatant errors in your code, for example in line 71 you use 'expr' although you already end()ed that in line 65.


    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  回复:Re: concert exception caught: not enough memory

    Posted 09/12/17 08:53 AM

    Originally posted by: zhangguangwei


        Thank you for your reply,  I carefully checked my code and corrected these errors. But when I run, there is another problem, and the screen is displayed " concert exception caught: IloAlgorithm cannot extract extractables".

        I uploaded my code and data files, please help me check it, I am very grateful for you.

        This is my constraints and objective:


    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  回复:Re: concert exception caught: not enough memory

    Posted 09/12/17 09:56 AM

    You can dig deeper as follows:

    1. Catch the exception (it should be of type CannotExtractException)
    2. Use the exception's getExtractables() method to get the extractables that could not be extracted
    3. Print those extractables to the screen (std::cout)

    This should tell you what exceptions could not be extracted and from there it should hopefully be simple to figure out why they could not be extracted.


    #CPLEXOptimizers
    #DecisionOptimization


  • 5.  回复:Re: concert exception caught: not enough memory

    Posted 09/12/17 10:15 AM

    Originally posted by: zhangguangwei


    I know what you mean,and I did it like this. Although  the exceptions was printed out, but I did not know how to deal with the error? So.......哭泣哭泣哭泣

     

    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;

       }

     

     

     

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 6.  回复:Re: concert exception caught: not enough memory

    Posted 09/12/17 10:58 AM

    The image is rather blurry for me, so I am not sure: do you expressions contain the product of 3 or more decision variables? That is not supported. Only linear or quadratic terms are allowed in expressions that are handled by IloCplex.


    #CPLEXOptimizers
    #DecisionOptimization


  • 7.  回复:Re: concert exception caught: not enough memory

    Posted 09/12/17 09:27 PM

    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


  • 8.  回复:Re: concert exception caught: not enough memory

    Posted 09/13/17 01:16 AM

    No, your formulas and code do not contain only linear and quadratic terms.

    You do have products of 3 variables, even in the mathematical formulation of your model.

    In your code you then have for example:

    expr += u[t][j]*(a_s[j]*PG[t][j]*PG[t][j]+b_s[j]*PG[t][j]+c_s[j]);

    That clearly contains a product of 3 decision variables: u[t][j] * PG[t][j] * PG[t][j]. That term is neither linear nor quadratic.


    #CPLEXOptimizers
    #DecisionOptimization