Skip to main content

Create a array of binaries variables should I use Gurobi C++

Answered

Comments

6 comments

  • Official comment
    Simranjit Kaur
    • Gurobi Staff Gurobi Staff
    This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum. Or why not try our AI Gurobot?.
  • Matthias Miltenberger
    • Gurobi Staff Gurobi Staff

    Hi Alessandra,

    It's perfectly fine to use our C++ API to construct your model. Why would you think otherwise? We just recommend using Python for its usability.

    Please refer to our documentation on addVars() and our numerous C++ examples to get an idea of how to implement this.

    Cheers,
    Matthias

    0
  • Alessandra Vieira
    • Gurobi-versary
    • Conversationalist
    • First Question

    Hi Matthias,

     I want to get a sum of two variables multiplied over many elements in a loop, it seems that I am just getting one single element of this multiplication. 

    GRBVar P_[T][N_GEN] ;   // (integer, LB, UP)
    GRBVar w_t_i[T][N_GEN];  // ( binary, Lb_0,UP_1)
    GRBQuadExpr expr[t]={0};

    for (t=0,t<T,t++){
    for(gen=0,gen<N_GEN,gen++){
    expr[t] +=P_[t][gen]*w_t_i{t][gen];
    }
    }

    It seems the syntax is correct for C++ but not for Gurobi because it just one single element but not the sum. What I am doing wrong ? The result is zero when it should be >0 , as all w_t_i are 1 , and the P_ > 0 . When I add the constraint then my model becomes infeasible.

    Any help? 

    Thanks
    Alessandra. 

    0
  • Matthias Miltenberger
    • Gurobi Staff Gurobi Staff

    Hi Alessandra,

    Please excuse me for not coming back to you earlier. Have you already been able to make your code work in the meantime?

    Something's definitely off with your indices. You are using "t" to declare the "GRBQaudExpr expr" instead of "T". Do you intend to add all terms to a single expression?

    Cheers,
    Matthias

    0
  • Alessandra Vieira
    • Gurobi-versary
    • Conversationalist
    • First Question

    Hi Matthias,

    Thanks for your answer!

    What I want now is that the expression that will be minimized besides the coefficient ( C_), also be multiplied for two variables Gurobi, integer P_, and other binary w_t_i. I expect that way that the model has both the Gurobi variable as part of the decision in the optimization process. The OBJ function is to minimize the sum of little expressions the same as :

    //the expression takes the value of variables for many members at time t, to minimize the cost per hour
    for (int t = 0; t < T; t++) {
    for (int i = 0; i < N_GEN; i++) {
    exprObj1 += C_[i] * P_[t][i]*w_t_i[t][i];
    for (int bess = 0; i < N_BESS; bess++) {
    exprObj2 += Cssd_[bess]*P_discharge[t][bess]*ssd_[t][bess];
    }
    }
    }


     I got these messages, and failed to build my object file! 

    > error C2679: binary '+=': no operator found which takes a right-hand operand of type 'GRBQuadExpr' (or there is no acceptable conversion)
    1>could be 'void GRBLinExpr::operator +=(const GRBLinExpr &)'
    1>\HolisticSolver_rm.cpp(487,53): message: while trying to match the argument list '(GRBLinExpr, GRBQuadExpr)'

    I tried already the overloaded operator as examples in Gurobi documentation, but it does not work.

    Any help I will very happy!

    0
  • Matthias Miltenberger
    • Gurobi Staff Gurobi Staff

    Hey Alessandra,

    It looks like you are trying to add a quadratic expression to a LinExpr object. Please make sure, that both exprObj1 and exprObj2 are of type QuadExpr.

    You may also try using addTerms() as suggested in the documentation about quadratic expressions.

    Cheers,
    Matthias

    0

Post is closed for comments.