### Setting a Starting Basis in C++ (Concert Technology) Source: https://www.ibm.com/docs/en/cofz/22.1.2?topic=optimization-overview Example demonstrating how to set a starting basis manually using the Concert Technology API in C++. ```cpp #include #include #include // ... (rest of the C++ code for ilolpex6.cpp) ``` -------------------------------- ### Setting a Starting Basis in C Source: https://www.ibm.com/docs/en/cofz/12.10.0?topic=optimization-overview This C example demonstrates how to set a starting basis manually for optimization problems using the Callable Library. It is useful when you have prior knowledge about the optimal basis. ```c #include #include #include int main(int argc, char **argv) { CPXENVptr env = CPXopenCPLEXfile(&env, "cplex.env"); if (env == NULL) { fprintf(stderr, "Could not open CPLEX environment.\n"); return 1; } CPXLPptr lp = CPXcreateprob(env, "myLP"); if (lp == NULL) { fprintf(stderr, "Could not create LP problem.\n"); CPXcloseCPLEXfile(env); return 1; } // Add variables (example: 3 variables) char var_names[3][10]; int num_vars = 3; for (int i = 0; i < num_vars; ++i) { sprintf(var_names[i], "x[%d]", i); } double lower_bounds[3] = {0.0, 0.0, 0.0}; double upper_bounds[3] = {CPX_INFBOUND, CPX_INFBOUND, CPX_INFBOUND}; double obj_coeffs[3] = {10.0, 20.0, 30.0}; CPXnewcols(lp, num_vars, obj_coeffs, lower_bounds, upper_bounds, NULL, var_names); // Add constraints (example: 3 constraints) int num_constraints = 3; char constraint_names[3][10]; for (int i = 0; i < num_constraints; ++i) { sprintf(constraint_names[i], "c[%d]", i); } char sense[3] = {'L', 'L', 'L'}; // Less than or equal to double rhs[3] = {100.0, 150.0, 120.0}; // Constraint 1: x[0] + x[1] + x[2] <= 100 int ind1[] = {0, 1, 2}; double val1[] = {1.0, 1.0, 1.0}; CPXaddrows(lp, 1, 3, rhs, sense, ind1, val1, NULL, constraint_names); // Constraint 2: 2*x[0] + x[1] + x[2] <= 150 int ind2[] = {0, 1, 2}; double val2[] = {2.0, 1.0, 1.0}; CPXaddrows(lp, 1, 3, &rhs[1], &sense[1], ind2, val2, NULL, &constraint_names[1]); // Constraint 3: x[0] + 2*x[1] + x[2] <= 120 int ind3[] = {0, 1, 2}; double val3[] = {1.0, 2.0, 1.0}; CPXaddrows(lp, 1, 3, &rhs[2], &sense[2], ind3, val3, NULL, &constraint_names[2]); // Set objective to maximize CPXchgobjsen(lp, CPX_MAX); // Set a starting basis (example: all basic) int *cplexbasis = (int *) calloc(num_vars, sizeof(int)); if (cplexbasis == NULL) { fprintf(stderr, "Failed to allocate memory for basis.\n"); CPXfreeprob(env, &lp); CPXcloseCPLEXfile(env); return 1; } for (int i = 0; i < num_vars; ++i) { cplexbasis[i] = CPX_BASIC; } CPXsetbasis(lp, cplexbasis); free(cplexbasis); // Solve the problem int status = CPXlpopt(lp); if (status == 0) { double objval; CPXgetobjval(lp, &objval); printf("Solution found: Objective value = %f\n", objval); double *x_values = (double *) calloc(num_vars, sizeof(double)); if (x_values == NULL) { fprintf(stderr, "Failed to allocate memory for solution values.\n"); } else { CPXgetx(lp, x_values, 0, num_vars - 1); printf("x = [ "); for (int i = 0; i < num_vars; ++i) { printf("%f ", x_values[i]); } printf("]\n"); free(x_values); } } else { fprintf(stderr, "Optimization failed with status %d.\n", status); } // Clean up CPXfreeprob(env, &lp); CPXcloseCPLEXfile(env); return 0; } ``` -------------------------------- ### Setting a Starting Basis in C++ Source: https://www.ibm.com/docs/en/cofz/12.10.0?topic=optimization-overview This C++ example demonstrates how to set a starting basis manually for optimization problems using Concert Technology. It is useful when you have prior knowledge about the optimal basis. ```cpp #include #include #include #include int main(int argc, char **argv) { IloEnv env; try { IloModel model(env); IloCplex cplex(env); IloNumVarArray x(env, 3, 0.0, IloInfinity); // Variables x[0], x[1], x[2] model.add(x); // Objective: Maximize 10*x[0] + 20*x[1] + 30*x[2] IloObjective obj = IloMaximize(env, IloScalProd(x, IloNumArray(env, 3, 10.0, 20.0, 30.0))); model.add(obj); // Constraints model.add(IloLessEq(env, IloScalProd(x, IloNumArray(env, 3, 1.0, 1.0, 1.0)), 100.0)); // x[0] + x[1] + x[2] <= 100 model.add(IloLessEq(env, IloScalProd(x, IloNumArray(env, 3, 2.0, 1.0, 1.0)), 150.0)); // 2*x[0] + x[1] + x[2] <= 150 model.add(IloLessEq(env, IloScalProd(x, IloNumArray(env, 3, 1.0, 2.0, 1.0)), 120.0)); // x[0] + 2*x[1] + x[2] <= 120 // Set up the CPLEX solver cplex.extract(model); // Set a starting basis IloCplex::BasisStatusArray cplexbasis(env, cplex.getSize()); cplex.getBasis(cplexbasis); // Example: Set a specific basis status for variables (e.g., all basic) for (int i = 0; i < cplex.getSize(); ++i) { cplexbasis[i] = IloCplex::Basic; } cplex.setBasis(cplexbasis); // Solve the problem if (cplex.solve()) { env.out() << "Solution found: " << cplex.getObjValue() << std::endl; IloNumArray vals(env); cplex.getValues(vals, x); env.out() << "x = " << vals << std::endl; vals.end(); } else { env.out() << "No solution found." << std::endl; } } catch (IloException& e) { env.cerr() << "Concert Exception caught: " << e << std::endl; } env.end(); return 0; } ``` -------------------------------- ### QCP Example Files Source: https://www.ibm.com/docs/en/cofz/12.9.0?topic=qcp-examples Locate these sample applications for solving quadratically constrained programs in your CPLEX installation directory. ```text qcpex1.c ``` ```text iloqcpex1.cpp ``` ```text QCPex1.java ``` ```text QCPex1.cs ``` -------------------------------- ### Setting an Incumbent Solution (Callable Library) Source: https://www.ibm.com/docs/en/cofz/12.9.0?topic=concepts-branch-cut-in-cplex Demonstrates how to set a starting incumbent solution using the CPLEX Callable Library routine `CPXcopymipstart`. This can help guide the solver. ```c CPXcopymipstart ``` -------------------------------- ### CPXXcallbackgetfunc and CPXcallbackgetfunc Source: https://www.ibm.com/docs/en/cofz/12.10.0?topic=api-c Get the currently installed generic callback. ```APIDOC ## CPXXcallbackgetfunc and CPXcallbackgetfunc ### Description Get the currently installed generic callback. ### Method Not applicable (Callback function) ### Endpoint Not applicable (Callback function) ### Parameters None explicitly documented. ### Request Example None provided. ### Response None explicitly documented. ``` -------------------------------- ### Get MIP Start Index in C Source: https://www.ibm.com/docs/en/cofz/12.10.0?topic=api-cpxxgetmipstartindex-cpxgetmipstartindex Use this snippet to find the index of a MIP start by its name. Ensure the CPLEX environment and problem objects are initialized, and a valid MIP start name is provided. ```c status = CPXgetmipstartindex (env, lp, "mipstart6", &rowindex); ``` -------------------------------- ### Example MST file content Source: https://www.ibm.com/docs/en/cofz/12.10.0?topic=cplex-mst-file-format-mip-starts This is an example of an MST file, which specifies MIP start values for variables. It includes header information and variable assignments. ```xml
``` -------------------------------- ### CPXcopystart Routine Example Source: https://www.ibm.com/docs/en/cofz/22.1.2?topic=program-cpxxcopystart-cpxcopystart This snippet demonstrates the basic syntax for calling the CPXcopystart routine. Ensure all parameters are correctly defined before execution. ```c status = CPXcopystart (env, lp, cstat, rstat, cprim, rprim, cdual, rdual); ``` -------------------------------- ### Get Number of MIP Starts Source: https://www.ibm.com/docs/en/cofz/12.10.0?topic=api-cpxxgetnummipstarts-cpxgetnummipstarts Retrieves the number of MIP starts from the CPLEX problem object. Ensure the CPLEX environment and problem object are valid. ```c numsolns = CPXgetnummipstarts (env, lp); ``` -------------------------------- ### Example: Creating, Optimizing, and Solving a QP Source: https://www.ibm.com/docs/en/cofz/12.9.0?topic=optimization-solving-problems-quadratic-objective-qp Demonstrates the process of creating a quadratic program, invoking the optimizer, and finding a solution using CPLEX. ```c #include int main (int argc, char **argv) { IloEnv env; try { IloModel model (env); IloNumVarArray x (env, 2, 0.0, 10.0); model.add (IloMinimize (env, (x[0] * x[0] + x[1] * x[1]))); model.add (x[0] + x[1] >= 1.0); model.add (x[0] + 2.0 * x[1] <= 3.0); IloCplex cplex (model); cplex.setParam (IloCplex::Param::Simplex::Display, 0); cplex.setParam (IloCplex::Param::MIP::Display, 0); if (cplex.solve ()) { env.out () << "Solution status = " << cplex.getStatus () << endl; env.out () << "Solution value = " << cplex.getObjValue () << endl; env.out () << "x = " << cplex.getValues (x) << endl; } } catch (IloException &e) { env.out () << "Concert Exception caught: " << e << endl; } env.end (); return 0; } ``` -------------------------------- ### Get Callback Type Example Source: https://www.ibm.com/docs/en/cofz/22.1.2?topic=model-cpxxgetcallbackctype-cpxgetcallbackctype This snippet demonstrates how to call the CPXgetcallbackctype function to get the type of the current callback. It shows the assignment of the return status to a variable. ```c status = CPXgetcallbackctype (env, cbdata, wherefrom, prectype, 0, precols-1); ``` -------------------------------- ### Setting a Starting Basis in C (Callable Library) Source: https://www.ibm.com/docs/en/cofz/12.9.0?topic=optimization-overview Illustrates the process of manually setting a starting basis using the Callable Library API in C for LP optimization. ```c lpex6.c ``` -------------------------------- ### Get MIP Relative Gap Example Source: https://www.ibm.com/docs/en/cofz/12.10.0?topic=api-cpxxgetmiprelgap-cpxgetmiprelgap Example of how to call the CPXgetmiprelgap routine to retrieve the MIP relative gap. Ensure the environment and problem pointers are valid. ```c status = CPXgetmiprelgap (env, lp, &gap); ``` -------------------------------- ### Example: Reading a QP from a File (C API) Source: https://www.ibm.com/docs/en/cofz/12.9.0?topic=optimization-solving-problems-quadratic-objective-qp Demonstrates reading quadratic program data from a file and solving it using the C API of CPLEX. ```c #include #include #include #include #include int main (int argc, char **argv) { int error = 0; CPXENVptr env = CPXopenCPLEX (&error); if (error) { fprintf (stderr, "Failed to open CPLEX environment, error code %d.\n", error); return 1; } int ret = CPXreadcopyprob (env, "qpex2.lp", NULL); if (ret) { fprintf (stderr, "Failed to read problem file, error code %d.\n", ret); goto TERMINATE; } ret = CPXqpopt (env, NULL); if (ret) { fprintf (stderr, "Failed to optimize QP, error code %d.\n", ret); goto TERMINATE; } int status = CPXgetstat (env, NULL); if (status != CPX_QPSTAT_OPTIMAL) { fprintf (stderr, "Problem is not solved to optimality. Status: %d\n", status); goto TERMINATE; } double objval; ret = CPXgetobjval (env, &objval); if (ret) { fprintf (stderr, "Failed to get objective value, error code %d.\n", ret); goto TERMINATE; } printf ("Objective value: %f\n", objval); TERMINATE: CPXcloseCPLEX (&env); return error; } ``` -------------------------------- ### Informational Callback Examples (C API) Source: https://www.ibm.com/docs/en/cofz/12.10.0?topic=callbacks-where-find-examples-informational Find sample applications for creating and using informational callbacks in C. These examples are located in the CPLEX installation directory. ```c xmipex4.c ``` ```c mipex4.c ``` -------------------------------- ### Retrieve MIP Start Names Source: https://www.ibm.com/docs/en/cofz/12.10.0?topic=g-cpxxgetmipstartname-cpxgetmipstartname Use this snippet to get the names of MIP starts from a CPLEX problem. Ensure the 'store' array is sufficiently sized to avoid errors. ```c status = CPXgetmipstartname (env, lp, name, store, storesz, &surplus, 0, cur_nummipstarts-1); ``` -------------------------------- ### Displaying the entire problem Source: https://www.ibm.com/docs/en/cofz/12.10.0?topic=problem-verifying-display-command This example shows the full problem description as displayed by the `display all` command, including the objective function, constraints, and variable bounds. ```text Maximize obj: x1 + 2 x2 + 3 x3 Subject To c1: - x1 + x2 + x3 <= 20 c2: x1 - 3 x2 + x3 <= 30 Bounds 0 <= x1 <= 40 All other variables are >= 0. ``` -------------------------------- ### Command-line Execution Example Source: https://www.ibm.com/docs/en/cofz/12.10.0?topic=ilolpex2cpp-overview This command shows how to execute the ilolpex2 program, specifying the input model file and the optimizer to use. It reads 'example.mps' and solves it using the dual simplex optimizer. ```bash ilolpex2 example.mps d ``` -------------------------------- ### Get Callback Type Example Source: https://www.ibm.com/docs/en/cofz/12.10.0?topic=crbicclca-cpxxgetcallbackctype-cpxgetcallbackctype This snippet demonstrates how to use the CPXgetcallbackctype function to get the type of the current callback. It requires the CPLEX environment, callback data, and the 'wherefrom' indicator. ```c status = CPXgetcallbackctype (env, cbdata, wherefrom, precbtype, 0, precols-1); ``` -------------------------------- ### Example Output: Dual Prices Source: https://www.ibm.com/docs/en/cofz/22.1.2?topic=problem-displaying-post-solution-information This output displays the constraint names and their corresponding dual prices (shadow prices) for the optimal solution. ```text Constraint Name Dual Price c1 2.750000 c2 0.250000 ``` -------------------------------- ### CPXcallbackgetfunc Source: https://www.ibm.com/docs/en/cofz/12.9.0?topic=cpxxcallbackgetfunc-cpxcallbackgetfunc Gets the currently installed generic callback function and its associated user handle. ```APIDOC ## CPXcallbackgetfunc ### Description Gets the currently installed generic callback function and its associated user handle. ### Method ```c int CPXcallbackgetfunc( CPXCENVptr env, CPXCLPptr lp, CPXLONG contextmask_p, CPXCALLBACKFUNC **callback_p, void ** cbhandle_p ) ``` ### Arguments * **env** (CPXCENVptr) - A pointer to the CPLEX environment. * **lp** (CPXCLPptr) - A pointer to a CPLEX problem object. * **contextmask_p** (CPXLONG *) - Pointer to store the bitmask specifying in which contexts the generic callback is invoked. * **callback_p** (CPXCALLBACKFUNC **) - Pointer to store the callback function of the currently installed generic callback. * **cbhandle_p** (void **) - Pointer to store the user handle of the currently installed generic callback. ### Return Returns 0 if successful, nonzero otherwise. ``` -------------------------------- ### Read LP file and run populate Source: https://www.ibm.com/docs/en/cofz/12.10.0?topic=pool-example-calling-populate Reads an LP file into the Interactive Optimizer and then invokes the populate procedure to find solutions. This is the initial setup before running populate. ```console read location.lp populate ``` -------------------------------- ### Copy Starting Information for Simplex Optimizer (Fortran) Source: https://www.ibm.com/docs/en/cofz/12.10.0?topic=api-cpxxcopystart-cpxcopystart Use this routine to provide starting information for a subsequent call to a simplex optimization routine. Ensure that arrays not being provided are passed as NULL pointers. ```fortran int CPXcopystart( CPXCENVptr env, CPXLPptr lp, int const * cstat, int const * rstat, double const * cprim, double const * rprim, double const * cdual, double const * rdual ) ``` -------------------------------- ### Get and Write MIP Starts in Callable Library (C API) Source: https://www.ibm.com/docs/en/cofz/22.1.2?topic=pool-model-changes-solution Accesses and writes MIP starts from the solution pool before model changes using the Callable Library (C API). ```c CPXgetmipstarts and CPXwritemipstarts in the Callable Library (C API) ``` -------------------------------- ### Get help for a specific CPLEX command Source: https://www.ibm.com/docs/en/cofz/12.10.0?topic=tutorial-using-help To get detailed information about a specific command, type 'help' followed by the command name or its unique abbreviation. For example, 'help primopt' or 'h p'. ```console help primopt ``` ```console h p ``` -------------------------------- ### C++ Example: ilolpex6.cpp Source: https://www.ibm.com/docs/en/cofz/12.9.0?topic=optimization-example-ilolpex6cpp This C++ code demonstrates starting an LP optimization with an advanced basis. It uses populatebycolumn to construct the problem and cplex.setBasisStatuses to set the initial basis. The iteration count is printed to show the effect of the advanced basis. ```cpp #include #include ILOSTLBEGIN int main(int argc, char **argv) { try { ILмолчаenv env; IloModel model(env); IloCplex cplex(env); IloNumVarArray x(env); IloObjective obj(env); IloRangeArray rng(env); // Populate model by column populatebycolumn(model, x, obj, rng); // Set the initial basis statuses IloBasisStatusArray cstat(env, x.getSize()); IloBasisStatusArray rstat(env, rng.getSize()); // Set all statuses to basic for (IloInt j = 0; j < x.getSize(); ++j) { cstat[j] = IloBasisStatus::Basic; } for (IloInt i = 0; i < rng.getSize(); ++i) { rstat[i] = IloBasisStatus::Basic; } cplex.setBasisStatuses(cstat, rstat); // Optimize the problem cplex.solve(); // Print the iteration count std::cout << "Iterations: " << cplex.getIterations() << std::endl; env.end(); } catch (IloException& e) { std::cerr << "Concert Exception caught: " << e << std::endl; } catch (...) { std::cerr << "Unknown exception caught" << std::endl; } return 0; } ``` -------------------------------- ### Get MIP Start Names Source: https://www.ibm.com/docs/en/cofz/12.9.0?topic=mmsiccla-cpxxgetmipstartname-cpxgetmipstartname Use this function to retrieve the names of MIP starts. Ensure that the 'name' buffer is large enough to hold all names. The 'surplus' parameter will indicate if the buffer was too small. ```c status = CPXgetmipstartname (env, lp, name, store, storesz, &surplus, 0, cur_nummipstarts-1); ``` -------------------------------- ### Informational Callback Examples (C/C++/Java/Python/C#) Source: https://www.ibm.com/docs/en/cofz/22.1.2?topic=callbacks-where-find-examples-informational Find sample applications for informational callbacks in various programming languages. These examples are located in the _yourCPLEXhome_/examples/src directory. ```text xmipex4.c and mipex4.c ilomipex4.cpp IloMIPex4.java MIPex4.cs MIPex4.vb mipex4.py ``` -------------------------------- ### Starting MIP Optimization from LP Solution (iloadmipex6.cpp) Source: https://www.ibm.com/docs/en/cofz/22.1.2?topic=c-examples Shows how to start a Mixed Integer Program (MIP) optimization from a Linear Program (LP) solution in C++. ```cpp iloadmipex6.cpp ``` -------------------------------- ### CPXXchgmipstarts / CPXchgmipstarts Source: https://www.ibm.com/docs/en/cofz/12.9.0?topic=cpxxchgmipstarts-cpxchgmipstarts The routine CPXXchgmipstarts/CPXchgmipstarts modifies or extends multiple MIP starts. If an existing MIP start has no value for the variable x[j], for example, and the call to CPXXchgmipstarts/CPXchgmipstarts specifies a start value, then the specified value is added to the MIP start. If the existing MIP start already has a value for x[j], then the new value replaces the old. If the MIP starts to be changed do not exist, CPXXchgmipstarts/CPXchgmipstarts will not create them and will return an error, CPXERR_INDEX_RANGE, instead. Start values may be specified for both integer and continuous variables. ```APIDOC ## CPXXchgmipstarts / CPXchgmipstarts ### Description Modifies or extends multiple MIP starts. If an existing MIP start has no value for a variable, the specified value is added. If a value already exists, it is replaced. Non-existent MIP starts are not created and will result in an error. ### Function Signature ```c int CPXXchgmipstarts( CPXCENVptr env, CPXLPptr lp, int mcnt, int const * mipstartindices, CPXNNZ nzcnt, CPXNNZ const * beg, CPXDIM const * varindices, double const * values, int const * effortlevel ); int CPXchgmipstarts( CPXCENVptr env, CPXLPptr lp, int mcnt, int const * mipstartindices, int nzcnt, int const * beg, int const * varindices, double const * values, int const * effortlevel ); ``` ### Arguments * **env** (CPXCENVptr*) - Pointer to the CPLEX environment. * **lp** (CPXLPptr*) - Pointer to the CPLEX problem object. * **mcnt** (int) - The number of MIP starts to be changed. This determines the length of the `mipstartindices` array. * **mipstartindices** (int const *) - An array of length `mcnt` containing the numeric indices of the MIP starts to be modified. * **nzcnt** (CPXNNZ) - The number of entries to be changed. This determines the length of the `varindices` and `values` arrays. * **beg** (CPXNNZ const *) - An array of length `mcnt`. `beg[i]` specifies the starting position in `varindices` and `values` for the i-th MIP start. `beg[0]` must be 0. Elements for MIP start `i` are from `beg[i]` to `beg[i+1]-1` (or `nzcnt-1` if `i` is the last MIP start). Can be NULL. * **varindices** (CPXDIM const *) - An array of length `nzcnt` containing the numeric indices of the columns (variables) for which starting values are being assigned. Can be NULL. * **values** (double const *) - An array of length `nzcnt` containing the values for the MIP starts. `values[j]` is the value for the variable `varindices[j]`. A value greater than or equal to `CPX_INFBOUND` indicates no value is set for that variable. Can be NULL. * **effortlevel** (int const *) - An array of length `mcnt`. `effortlevel[i]` specifies the effort level for the i-th MIP start. If NULL, CPLEX assigns `CPX_MIPSTART_AUTO` to all MIP starts. ### Error Handling * `CPXERR_INDEX_RANGE`: Returned if any of the specified MIP start indices do not exist. ``` -------------------------------- ### Example Usage Source: https://www.ibm.com/docs/en/cofz/22.1.2?topic=manual-cpxxcheckcopyqpsep-cpxcheckcopyqpsep Example of how to call the CPXcheckcopyqpsep routine. ```APIDOC ## Example ```c status = CPXcheckcopyqpsep (env, lp, qsepvec); ``` ```