// (c) 2024-2025 Fair Isaac Corporation #include #include // For throwing exceptions using namespace xpress; using namespace xpress::objects; using xpress::objects::utils::sum; /* Shows the use of a matrix of variables (i.e. x[i][j]) and different ways to specify contraints*/ /* Contract Allocation example. Given a number of districts, we have to produce enough * of a certain resource to fulfill all contracts. Each district has a maximum amount of * resource they can produce. Minimize costs to fulfill contracts. */ const int NDISTRICT = 6; // Number of districts const int NCONTRACT = 10; // Number of contracts const std::vector output = { 50, 40, 10, 20, 70, 50 }; // Max. output per district const std::vector cost = { 50, 20, 25, 30, 45, 40 }; // Cost per unit per district // Required volume of resources by contracts: const std::vector volume = { 20, 10, 30, 15, 20, 30, 10, 50, 10, 20 }; int main() { try { // Create a problem instance with verbose messages printed to Console XpressProblem prob; prob.callbacks.addMessageCallback(XpressProblem::console); /* VARIABLES */ // Variables indicating whether a project is chosen std::vector> x = prob.addVariables(NDISTRICT, NCONTRACT) .withType(ColumnType::Binary) .withName("x_d%d_c%d") .toArray(); // Quantities allocated to contractors std::vector> q = prob.addVariables(NDISTRICT, NCONTRACT) .withType(ColumnType::SemiContinuous) .withUB([&](int d, int){ return output[d]; }) .withLimit(5) .withName("q_d%d_c%d") .toArray(); /* CONSTRAINTS */ // "Size": Produce the required volume of resource for each contract // for all c in [0,NCONTRACT] // sum(d in [0,NDISTRICT]) q[d][c] >= volume[c] prob.addConstraints(NCONTRACT, [&](int c) { SumExpression coveredVolume = sum(NDISTRICT, [&](int d) { return q[d][c]; }); return coveredVolume.geq(volume[c]).setName("Size_" + std::to_string(c)); }); // "Min": at least 2 districts per contract // for all c in [0,NCONTRACT] // sum(d in [0,NDISTRICT]) x[d][c] >= 2 prob.addConstraints(NCONTRACT, [&](int c) { LinExpression districtsPerContract = LinExpression::create(); for (int d=0; d sol = prob.getSolution(); std::cout << "*** Solution ***" << std::endl; std::cout << "Objective value: " << prob.attributes.getObjVal() << std::endl; for (std::vector q_d : q) { for (Variable q_dc : q_d) { if (q_dc.getValue(sol) > 0.0) { std::cout << q_dc.getName() << ": " << q_dc.getValue(sol) << ", "; } } std::cout << std::endl; } return 0; } catch (std::exception& e) { std::cout << "Exception: " << e.what() << std::endl; return -1; } }