Advertisement
Diogo03

PO

Apr 19th, 2025
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.95 KB | None | 0 0
  1. /*---------Escalonamento---------*/
  2. #include <ilcplex/ilocplex.h>
  3. #include <iostream>
  4.  
  5. ILOSTLBEGIN
  6.  
  7. int main() {
  8.     IloEnv env;
  9.     try {
  10.         IloModel model(env);
  11.         int d[7] = {5, 7, 6, 8, 4, 7, 5};
  12.         int a[7][8] = {
  13.             {1, 0, 0, 0, 1, 1, 1, 1},
  14.             {1, 1, 0, 0, 0, 1, 1, 1},
  15.             {1, 1, 1, 0, 0, 0, 1, 1},
  16.             {1, 1, 1, 1, 0, 0, 0, 1},
  17.             {1, 1, 1, 1, 1, 0, 0, 0},
  18.             {0, 1, 1, 1, 1, 1, 0, 0},
  19.             {0, 0, 1, 1, 1, 1, 1, 0}
  20.         };
  21.  
  22.         IloNumVarArray x(env, 8, 0, IloInfinity, ILOINT);
  23.         for (int i = 0; i < 7; i++) {
  24.             IloExpr cover(env);
  25.             for (int s = 0; s < 8; s++) {
  26.                 cover += a[i][s] * x[s];
  27.             }
  28.             model.add(cover >= d[i]);
  29.             cover.end();
  30.         }
  31.  
  32.         IloExpr total_enfs(env);
  33.         for (int s = 0; s < 8; s++) {
  34.             total_enfs += x[s];
  35.         }
  36.         model.add(IloMinimize(env, total_enfs));
  37.         total_enfs.end();
  38.  
  39.         IloCplex cplex(model);
  40.         if (!cplex.solve()) {
  41.             std::cerr << "Sem solucao\n";
  42.             return 1;
  43.         }
  44.        
  45.         std::cout << "Resposta: " << cplex.getObjValue() << std::endl;
  46.     }
  47.  
  48.     catch (IloException& e) {
  49.         std::cerr << "Erro CPLEX: " << e.getMessage() << std::endl;
  50.     }
  51.     catch (...) {
  52.         std::cerr << "Erro desconhecido." << std::endl;
  53.     }
  54.  
  55.     env.end();
  56.     return 0;
  57. }
  58.  
  59. /*--------- Mochila---------*/
  60. #include <ilcplex/ilocplex.h>
  61. #include <iostream>
  62. #include <vector>
  63. ILOSTLBEGIN
  64.  
  65. int main() {
  66.     IloEnv env;
  67.     try {
  68.         IloModel model(env);
  69.  
  70.         int n = 10;
  71.         int W = 15;
  72.         std::vector<double> v = {10, 40, 30, 50, 35, 40, 30, 45, 25, 20};
  73.         std::vector<double> w = {1, 3, 4, 5, 2, 3, 2, 4, 1, 2};
  74.  
  75.         IloBoolVarArray x(env, n);
  76.        
  77.         IloExpr peso_total(env);
  78.         for (int i = 0; i < n; ++i) {
  79.             peso_total += w[i] * x[i];
  80.         }
  81.  
  82.         model.add(peso_total <= W);
  83.         peso_total.end();
  84.        
  85.         IloExpr valor_total(env);
  86.         for (int i = 0; i < n; ++i) {
  87.             valor_total += v[i] * x[i];
  88.         }
  89.  
  90.         model.add(IloMaximize(env, valor_total));
  91.         valor_total.end();
  92.  
  93.         IloCplex cplex(model);
  94.         if (!cplex.solve()) {
  95.             std::cerr << "Sem solucao\n";
  96.             return 1;
  97.         }
  98.        
  99.         std::cout << "Resposta: " << cplex.getObjValue() << std::endl;
  100.     }
  101.     catch (IloException& e) {
  102.         std::cerr << "Erro CPLEX: " << e.getMessage() << std::endl;
  103.     }
  104.     catch (...) {
  105.         std::cerr << "Erro desconhecido." << std::endl;
  106.     }
  107.  
  108.     env.end();
  109.     return 0;
  110. }
  111.  
  112. /*---------Plantio---------*/
  113. #include <ilcplex/ilocplex.h>
  114. #include <iostream>
  115. #include <string>
  116.  
  117. ILOSTLBEGIN
  118.  
  119. int main() {
  120.     IloEnv env;
  121.     try {
  122.         IloModel model(env);
  123.  
  124.         int area_fazenda[3] = {400, 650, 350};
  125.         int area_cultura[3] = {660, 880, 400};
  126.         int agua_fazenda[3] = {1800, 2200, 950};
  127.         double coef_agua[3] = {5.5,  4.0,  3.5};
  128.         int preco[3] = {5000, 4000, 1800};
  129.  
  130.         IloArray<IloNumVarArray> X(env, 3);
  131.         for (int i = 0; i < 3; i++) {
  132.             X[i] = IloNumVarArray(env, 3);
  133.             for (int j = 0; j < 3; j++) {
  134.                 std::string name = "X" + std::to_string(i+1) + (j==0?"m":j==1?"a":"f");
  135.                 X[i][j] = IloNumVar(env, 0.0, IloInfinity, ILOINT, name.c_str());
  136.             }
  137.         }
  138.  
  139.         IloExpr lucro(env);
  140.         for (int i = 0; i < 3; i++)
  141.             for (int j = 0; j < 3; j++)
  142.                 lucro += preco[j] * X[i][j];
  143.         model.add(IloMaximize(env, lucro));
  144.         lucro.end();
  145.  
  146.         for (int i = 0; i < 3; i++) {
  147.             IloExpr soma(env);
  148.             for (int j = 0; j < 3; j++)
  149.                 soma += X[i][j];
  150.             model.add(soma <= area_fazenda[i]);
  151.             soma.end();
  152.         }
  153.  
  154.         for (int j = 0; j < 3; j++) {
  155.             IloExpr soma(env);
  156.             for (int i = 0; i < 3; i++)
  157.                 soma += X[i][j];
  158.             model.add(soma <= area_cultura[j]);
  159.             soma.end();
  160.         }
  161.  
  162.         for (int i = 0; i < 3; i++) {
  163.             IloExpr uso(env);
  164.             for (int j = 0; j < 3; j++)
  165.                 uso += coef_agua[j] * X[i][j];
  166.             model.add(uso <= agua_fazenda[i]);
  167.             uso.end();
  168.         }
  169.  
  170.         IloExpr soma0(env), soma1(env), soma2(env);
  171.         for (int j = 0; j < 3; j++) {
  172.             soma0 += X[0][j];
  173.             soma1 += X[1][j];
  174.             soma2 += X[2][j];
  175.         }
  176.  
  177.         model.add(650.0 * soma0 == 400.0 * soma1);
  178.         model.add(350.0 * soma0 == 400.0 * soma2);
  179.         soma0.end(); soma1.end(); soma2.end();
  180.  
  181.         IloCplex cplex(model);
  182.         if (!cplex.solve()) {
  183.             std::cerr << "Sem solucao\n";
  184.             return 1;
  185.         }
  186.  
  187.         std::cout << "Lucro = " << cplex.getObjValue() << std::endl;
  188.         for (int i = 0; i < 3; i++) {
  189.             std::cout << "Fazenda " << (i + 1) << ": ";
  190.             std::cout << "Milho = "  << cplex.getValue(X[i][0]) << ", ";
  191.             std::cout << "Arroz = "  << cplex.getValue(X[i][1]) << ", ";
  192.             std::cout << "Feijao = " << cplex.getValue(X[i][2]) << std::endl;
  193.         }
  194.     }
  195.     catch (IloException& e) {
  196.         std::cerr << "Erro CPLEX: " << e.getMessage() << std::endl;
  197.     }
  198.     catch (...) {
  199.         std::cerr << "Erro desconhecido" << std::endl;
  200.     }
  201.     env.end();
  202.     return 0;
  203. }
  204.  
  205. /*---------TINTAS---------*/
  206.  
  207. #include <ilcplex/ilocplex.h>
  208. #include <iostream>
  209. #include <iomanip>
  210. #include <string>
  211.  
  212. ILOSTLBEGIN
  213.  
  214. int main() {
  215.     IloEnv env;
  216.     try {
  217.         IloModel model(env);
  218.  
  219.         int V[2] = {1000, 250};
  220.         double sec_min[2] = {0.25 * V[0], 0.20 * V[1]};
  221.         double cor_min[2] = {0.50 * V[0], 0.50 * V[1]};
  222.         double comp_sec[4] = {0.30, 0.60, 1.00, 0.00};
  223.         double comp_cor[4] = {0.70, 0.40, 0.00, 1.00};
  224.         double custo[4] = {1.50, 1.00, 4.00, 6.00};
  225.  
  226.         IloArray<IloNumVarArray> x(env, 2);
  227.         for (int p = 0; p < 2; ++p) {
  228.             x[p] = IloNumVarArray(env, 4, 0, IloInfinity, ILOINT);
  229.         }
  230.  
  231.         for (int p = 0; p < 2; ++p) {
  232.             IloExpr soma(env);
  233.             for (int i = 0; i < 4; ++i) soma += x[p][i];
  234.             model.add(soma == V[p]);
  235.             soma.end();
  236.         }
  237.  
  238.         for (int p = 0; p < 2; ++p) {
  239.             IloExpr sec(env);
  240.             for (int i = 0; i < 4; ++i) sec += comp_sec[i] * x[p][i];
  241.             model.add(sec >= sec_min[p]);
  242.             sec.end();
  243.         }
  244.  
  245.         for (int p = 0; p < 2; ++p) {
  246.             IloExpr cor(env);
  247.             for (int i = 0; i < 4; ++i) cor += comp_cor[i] * x[p][i];
  248.             model.add(cor >= cor_min[p]);
  249.             cor.end();
  250.         }
  251.  
  252.         IloExpr custo_tot(env);
  253.         for (int p = 0; p < 2; ++p)
  254.             for (int i = 0; i < 4; ++i)
  255.                 custo_tot += custo[i] * x[p][i];
  256.         model.add(IloMinimize(env, custo_tot));
  257.         custo_tot.end();
  258.  
  259.         IloCplex cplex(model);
  260.         cplex.setOut(env.getNullStream());
  261.         if (!cplex.solve()) {
  262.             std::cerr << "Sem solução\n";
  263.             env.end();
  264.             return 1;
  265.         }
  266.  
  267.         std::cout << std::fixed << std::setprecision(0);
  268.         const char* nomes[4] = {"SolA","SolB","SEC","COR"};
  269.         for (int p = 0; p < 2; ++p) {
  270.             std::string prod = (p == 0 ? "SR" : "SN");
  271.             std::cout << prod << ":\n";
  272.             for (int i = 0; i < 4; ++i) {
  273.                 int v = (int)std::round(cplex.getValue(x[p][i]));
  274.                 std::cout << "  " << nomes[i] << " = " << v << " L\n";
  275.             }
  276.         }
  277.     }
  278.     catch (IloException& e) {
  279.         std::cerr << "Erro CPLEX: " << e.getMessage() << std::endl;
  280.     }
  281.     catch (...) {
  282.         std::cerr << "Erro desconhecido." << std::endl;
  283.     }
  284.  
  285.     env.end();
  286.     return 0;
  287. }
  288.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement