Global AI and Data Science

 View Only
  • 1.  Replacing objective function

    Posted Thu June 20, 2024 02:07 PM

    Hi,

    I'm using c++ api for cplex, and I have a function:

    void build_model(IloEnv &env, IloModel &model, IloCplex &cplex, int nships) {

      int i;

      IloNumVarArray waiting(env, nships);

      for(i = 0; i < nships; ++i)
        {
          sprintf(_name, "%s_%03d", "w", i + 1);
          wait_variable[i] = IloNumVar(env, 0, 1, ILOINT, _name);
        }

      objective_function = IloSum(wait_variable);
      for(i = 0; i < nships; ++i)
        {
          objective_function += sbta.get_rejection_cost() * sbta.service_time(i) * (1 - served_variable[i]);
        }
      model.add(IloObjective(IloMinimize(env, objective_function, "obj")));

    }

    }

    Another function is calling it:

    #include <ilcplex/ilocplex.h>

     int main()
    {
      // Initialize cplex environment

      int nships = 10;
      IloEnv env;
      IloModel model(env);
      IloCplex cplex(env);

      // Build generic model
      build_model(env, model, cplex, nships);

    // Customize the model

    exit(1);

    }

    I want to modify the model in main function:

    How can I define a new objective function?

    If build_model includes a constraint in the model "wait_3" how can I delete it in main function.

    Thanks, in advance.

    #AI and DS Skills

    #Decision Optimization



    ------------------------------
    Manuel Munoz-Marquez
    ------------------------------

    #AIandDSSkills


  • 2.  RE: Replacing objective function

    Community Leadership
    Posted Mon July 22, 2024 04:15 PM

    Hi Manuel,

    I am following up on some outstanding questions, did you ever get a response from someone or were you able to resolve the issue? It also may be more effective to post your question in the Decision Optimization Community.

    Thanks very much,

    Nick



    ------------------------------
    Nick Plowden
    AI Community Engagement
    IBM
    ------------------------------



  • 3.  RE: Replacing objective function

    Posted Mon July 22, 2024 04:34 PM

     

     

    1/

     

    <ilcplex/ilocplex.h>

     

    void build_model(IloEnv &env, IloModel &model, IloCplex &cplex, int nships) {

      // Your existing code here

    }

     

    int main() {

      int nships = 10;

      IloEnv env;

      IloModel model(env);

      IloCplex cplex(env);

     

      // Build the model

      build_model(env, model, cplex, nships);

     

      // Access the existing objective function

      IloObjective currentObjective = model.getObjective();

     

      // Define a new objective function (for example, maximize instead of minimize)

      IloObjective newObjective = IloMaximize(env, currentObjective.getExpr());

     

      // Update the model with the new objective function

      model.remove(currentObjective);  // Remove the old objective

      model.add(newObjective);         // Add the new objective

     

      // Now model has the new objective function

     

      // Solve the modified model (if needed)

      cplex.extract(model);

      cplex.solve();

     

      // Other operations with the modified model...

     

      // Clean up

      env.end();

     

      return 0;

    }

     

    2/

    <ilcplex/ilocplex.h>

     

    void build_model(IloEnv &env, IloModel &model, IloCplex &cplex, int nships) {

      // Your existing code here

      IloRange wait_3_constraint = ...; // Assume you have added this constraint

      wait_3_constraint.setName("wait_3");

      model.add(wait_3_constraint); // Adding the constraint

    }

     

    int main() {

      int nships = 10;

      IloEnv env;

      IloModel model(env);

      IloCplex cplex(env);

     

      // Build the model

      build_model(env, model, cplex, nships);

     

      // Delete a constraint named "wait_3" from the model

      IloRangeArray constraints = model.getRangeArray();

      for (IloInt i = 0; i < constraints.getSize(); ++i) {

        if (constraints[i].getName() == "wait_3") {

          model.remove(constraints[i]);

          break; // Assuming "wait_3" is unique, we can break after removing it

        }

      }

     

      // Now "wait_3" constraint is removed from the model

     

      // Solve the modified model (if needed)

      cplex.extract(model);

      cplex.solve();

     

      // Other operations with the modified model...

     

      // Clean up

      env.end();

     

      return 0;

    }