Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*---------Escalonamento---------*/
- #include <ilcplex/ilocplex.h>
- #include <iostream>
- ILOSTLBEGIN
- int main() {
- IloEnv env;
- try {
- IloModel model(env);
- int d[7] = {5, 7, 6, 8, 4, 7, 5};
- int a[7][8] = {
- {1, 0, 0, 0, 1, 1, 1, 1},
- {1, 1, 0, 0, 0, 1, 1, 1},
- {1, 1, 1, 0, 0, 0, 1, 1},
- {1, 1, 1, 1, 0, 0, 0, 1},
- {1, 1, 1, 1, 1, 0, 0, 0},
- {0, 1, 1, 1, 1, 1, 0, 0},
- {0, 0, 1, 1, 1, 1, 1, 0}
- };
- IloNumVarArray x(env, 8, 0, IloInfinity, ILOINT);
- for (int i = 0; i < 7; i++) {
- IloExpr cover(env);
- for (int s = 0; s < 8; s++) {
- cover += a[i][s] * x[s];
- }
- model.add(cover >= d[i]);
- cover.end();
- }
- IloExpr total_enfs(env);
- for (int s = 0; s < 8; s++) {
- total_enfs += x[s];
- }
- model.add(IloMinimize(env, total_enfs));
- total_enfs.end();
- IloCplex cplex(model);
- if (!cplex.solve()) {
- std::cerr << "Sem solucao\n";
- return 1;
- }
- std::cout << "Resposta: " << cplex.getObjValue() << std::endl;
- }
- catch (IloException& e) {
- std::cerr << "Erro CPLEX: " << e.getMessage() << std::endl;
- }
- catch (...) {
- std::cerr << "Erro desconhecido." << std::endl;
- }
- env.end();
- return 0;
- }
- /*--------- Mochila---------*/
- #include <ilcplex/ilocplex.h>
- #include <iostream>
- #include <vector>
- ILOSTLBEGIN
- int main() {
- IloEnv env;
- try {
- IloModel model(env);
- int n = 10;
- int W = 15;
- std::vector<double> v = {10, 40, 30, 50, 35, 40, 30, 45, 25, 20};
- std::vector<double> w = {1, 3, 4, 5, 2, 3, 2, 4, 1, 2};
- IloBoolVarArray x(env, n);
- IloExpr peso_total(env);
- for (int i = 0; i < n; ++i) {
- peso_total += w[i] * x[i];
- }
- model.add(peso_total <= W);
- peso_total.end();
- IloExpr valor_total(env);
- for (int i = 0; i < n; ++i) {
- valor_total += v[i] * x[i];
- }
- model.add(IloMaximize(env, valor_total));
- valor_total.end();
- IloCplex cplex(model);
- if (!cplex.solve()) {
- std::cerr << "Sem solucao\n";
- return 1;
- }
- std::cout << "Resposta: " << cplex.getObjValue() << std::endl;
- }
- catch (IloException& e) {
- std::cerr << "Erro CPLEX: " << e.getMessage() << std::endl;
- }
- catch (...) {
- std::cerr << "Erro desconhecido." << std::endl;
- }
- env.end();
- return 0;
- }
- /*---------Plantio---------*/
- #include <ilcplex/ilocplex.h>
- #include <iostream>
- #include <string>
- ILOSTLBEGIN
- int main() {
- IloEnv env;
- try {
- IloModel model(env);
- int area_fazenda[3] = {400, 650, 350};
- int area_cultura[3] = {660, 880, 400};
- int agua_fazenda[3] = {1800, 2200, 950};
- double coef_agua[3] = {5.5, 4.0, 3.5};
- int preco[3] = {5000, 4000, 1800};
- IloArray<IloNumVarArray> X(env, 3);
- for (int i = 0; i < 3; i++) {
- X[i] = IloNumVarArray(env, 3);
- for (int j = 0; j < 3; j++) {
- std::string name = "X" + std::to_string(i+1) + (j==0?"m":j==1?"a":"f");
- X[i][j] = IloNumVar(env, 0.0, IloInfinity, ILOINT, name.c_str());
- }
- }
- IloExpr lucro(env);
- for (int i = 0; i < 3; i++)
- for (int j = 0; j < 3; j++)
- lucro += preco[j] * X[i][j];
- model.add(IloMaximize(env, lucro));
- lucro.end();
- for (int i = 0; i < 3; i++) {
- IloExpr soma(env);
- for (int j = 0; j < 3; j++)
- soma += X[i][j];
- model.add(soma <= area_fazenda[i]);
- soma.end();
- }
- for (int j = 0; j < 3; j++) {
- IloExpr soma(env);
- for (int i = 0; i < 3; i++)
- soma += X[i][j];
- model.add(soma <= area_cultura[j]);
- soma.end();
- }
- for (int i = 0; i < 3; i++) {
- IloExpr uso(env);
- for (int j = 0; j < 3; j++)
- uso += coef_agua[j] * X[i][j];
- model.add(uso <= agua_fazenda[i]);
- uso.end();
- }
- IloExpr soma0(env), soma1(env), soma2(env);
- for (int j = 0; j < 3; j++) {
- soma0 += X[0][j];
- soma1 += X[1][j];
- soma2 += X[2][j];
- }
- model.add(650.0 * soma0 == 400.0 * soma1);
- model.add(350.0 * soma0 == 400.0 * soma2);
- soma0.end(); soma1.end(); soma2.end();
- IloCplex cplex(model);
- if (!cplex.solve()) {
- std::cerr << "Sem solucao\n";
- return 1;
- }
- std::cout << "Lucro = " << cplex.getObjValue() << std::endl;
- for (int i = 0; i < 3; i++) {
- std::cout << "Fazenda " << (i + 1) << ": ";
- std::cout << "Milho = " << cplex.getValue(X[i][0]) << ", ";
- std::cout << "Arroz = " << cplex.getValue(X[i][1]) << ", ";
- std::cout << "Feijao = " << cplex.getValue(X[i][2]) << std::endl;
- }
- }
- catch (IloException& e) {
- std::cerr << "Erro CPLEX: " << e.getMessage() << std::endl;
- }
- catch (...) {
- std::cerr << "Erro desconhecido" << std::endl;
- }
- env.end();
- return 0;
- }
- /*---------TINTAS---------*/
- #include <ilcplex/ilocplex.h>
- #include <iostream>
- #include <iomanip>
- #include <string>
- ILOSTLBEGIN
- int main() {
- IloEnv env;
- try {
- IloModel model(env);
- int V[2] = {1000, 250};
- double sec_min[2] = {0.25 * V[0], 0.20 * V[1]};
- double cor_min[2] = {0.50 * V[0], 0.50 * V[1]};
- double comp_sec[4] = {0.30, 0.60, 1.00, 0.00};
- double comp_cor[4] = {0.70, 0.40, 0.00, 1.00};
- double custo[4] = {1.50, 1.00, 4.00, 6.00};
- IloArray<IloNumVarArray> x(env, 2);
- for (int p = 0; p < 2; ++p) {
- x[p] = IloNumVarArray(env, 4, 0, IloInfinity, ILOINT);
- }
- for (int p = 0; p < 2; ++p) {
- IloExpr soma(env);
- for (int i = 0; i < 4; ++i) soma += x[p][i];
- model.add(soma == V[p]);
- soma.end();
- }
- for (int p = 0; p < 2; ++p) {
- IloExpr sec(env);
- for (int i = 0; i < 4; ++i) sec += comp_sec[i] * x[p][i];
- model.add(sec >= sec_min[p]);
- sec.end();
- }
- for (int p = 0; p < 2; ++p) {
- IloExpr cor(env);
- for (int i = 0; i < 4; ++i) cor += comp_cor[i] * x[p][i];
- model.add(cor >= cor_min[p]);
- cor.end();
- }
- IloExpr custo_tot(env);
- for (int p = 0; p < 2; ++p)
- for (int i = 0; i < 4; ++i)
- custo_tot += custo[i] * x[p][i];
- model.add(IloMinimize(env, custo_tot));
- custo_tot.end();
- IloCplex cplex(model);
- cplex.setOut(env.getNullStream());
- if (!cplex.solve()) {
- std::cerr << "Sem solução\n";
- env.end();
- return 1;
- }
- std::cout << std::fixed << std::setprecision(0);
- const char* nomes[4] = {"SolA","SolB","SEC","COR"};
- for (int p = 0; p < 2; ++p) {
- std::string prod = (p == 0 ? "SR" : "SN");
- std::cout << prod << ":\n";
- for (int i = 0; i < 4; ++i) {
- int v = (int)std::round(cplex.getValue(x[p][i]));
- std::cout << " " << nomes[i] << " = " << v << " L\n";
- }
- }
- }
- catch (IloException& e) {
- std::cerr << "Erro CPLEX: " << e.getMessage() << std::endl;
- }
- catch (...) {
- std::cerr << "Erro desconhecido." << std::endl;
- }
- env.end();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement