Advertisement
Guest User

Untitled

a guest
Mar 31st, 2015
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.57 KB | None | 0 0
  1. QT += core
  2. QT -= gui
  3.  
  4. TARGET = SimpelConsole
  5. CONFIG += console
  6. CONFIG -= app_bundle
  7.  
  8. TEMPLATE = app
  9. SOURCES += main.cpp
  10.  
  11. INCLUDEPATH += C:ILOGCPLEX123cplexinclude
  12. INCLUDEPATH += C:ILOGCPLEX123concertinclude
  13. INCLUDEPATH += C:Program Files (x86)Microsoft Visual Studio 10.0VCinclude
  14. DEFINES += IL_STD
  15.  
  16. #// cplex123.lib library
  17. win32: LIBS += -L$$PWD/../../../../../ILOG/CPLEX123/cplex/lib/x64_windows_vs2010/stat_mda/ -lcplex123
  18. INCLUDEPATH += $$PWD/../../../../../ILOG/CPLEX123/cplex/lib/x64_windows_vs2010/stat_mda
  19. DEPENDPATH += $$PWD/../../../../../ILOG/CPLEX123/cplex/lib/x64_windows_vs2010/stat_mda
  20.  
  21. #// ilocplex library
  22. win32: LIBS += -L$$PWD/../../../../../ILOG/CPLEX123/cplex/lib/x64_windows_vs2010/stat_mda/ -lilocplex
  23. INCLUDEPATH += $$PWD/../../../../../ILOG/CPLEX123/cplex/lib/x64_windows_vs2010/stat_mda
  24. DEPENDPATH += $$PWD/../../../../../ILOG/CPLEX123/cplex/lib/x64_windows_vs2010/stat_mda
  25.  
  26. #// concert.lib library
  27. win32: LIBS += -L$$PWD/../../../../../ILOG/CPLEX123/concert/lib/x64_windows_vs2010/stat_mda/ -lconcert
  28. INCLUDEPATH += $$PWD/../../../../../ILOG/CPLEX123/concert/lib/x64_windows_vs2010/stat_mda
  29. DEPENDPATH += $$PWD/../../../../../ILOG/CPLEX123/concert/lib/x64_windows_vs2010/stat_mda
  30.  
  31. #include <QCoreApplication>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <time.h>
  35. #include <ilcplex/ilocplex.h>
  36. #include <ilcplex/cplex.h>
  37.  
  38. int main(int argc, char *argv[]) {
  39. QCoreApplication app(argc, argv);
  40.  
  41. double elapsed_time; clock_t start_time;
  42. start_time= clock();
  43. int i, j;
  44. int nbCities = 5;
  45. int nbOvens = 2;
  46.  
  47. double d[2][5];
  48. d[0][0]= 130; d[0][1]= 70; d[0][2]= 50; d[0][3]= 100; d[0][4]= 150;
  49. d[1][0]= 90; d[1][1]= 70; d[1][2]= 250; d[1][3]= 130; d[1][4]= 200;
  50. double a[5];
  51. a[0]=240000; a[1]=240000; a[2]=240000; a[3]=240000; a[4]=240000;
  52. double c[2];
  53. c[0]=500000; c[1]=15000000;
  54. double f[2];
  55. f[0] = 80; f[1] = 100;
  56. int cost = 1; // transport cost
  57.  
  58. IloEnv env; // create the environment
  59. try{
  60. IloModel model(env); // create a model
  61. IloNumVarArray variables(env); // create variable set
  62. IloRangeArray constraints(env); // create constraint set
  63. IloObjective objective = IloMinimize(env); // create objective function
  64.  
  65. // Build variables
  66. char jchar[3], name[15];
  67. for (j=0; j!=nbCities*nbOvens; j++){
  68. strcpy(name, "x_");
  69. itoa(j, jchar, 10);
  70. strcat(name, jchar);
  71. IloNumVar xVar(env, 0, 1, ILOINT, name);
  72. variables.add(xVar);
  73. }
  74.  
  75. // Build constraints
  76. for (i=0; i!=nbOvens; i++){
  77. strcpy(name, "cap_restr_");
  78. itoa(i, jchar, 10);
  79. strcat(name, jchar);
  80. constraints.add(IloRange(env, -IloInfinity, c[i], name));
  81. }
  82. for (i=0; i!=nbCities; i++){
  83. strcpy(name, "flow_restr_");
  84. itoa(i, jchar, 10);
  85. strcat(name, jchar);
  86. constraints.add(IloRange(env, 1, 1, name));
  87. }
  88.  
  89. // Set constraints coefficients
  90. for (i=0; i!=nbOvens; i++){
  91. for (j= 0; j!= nbCities; j++){
  92. constraints[i].setLinearCoef(variables[j+i*nbCities], a[j]); // cannot exceed capacity - constraint
  93. }
  94. }
  95. for (i=0; i!=nbCities; i++){
  96. for (j=0; j!=nbOvens; j++){ constraints[nbOvens+i].setLinearCoef(variables[i+j*nbCities], 1); // flow constraint
  97. }
  98. }
  99.  
  100. // Build objective function
  101. for (i=0; i!=nbOvens; i++){
  102. for (j=0; j!=nbCities; j++){
  103. objective.setLinearCoef(variables[i*nbCities+j], cost*a[j]*d[i][j]+f[i]*a[j]);
  104. }
  105. }
  106.  
  107. model.add(objective);
  108. model.add(constraints); // variables are included implicitly
  109. IloCplex cplex(model);
  110. cplex.exportModel("model.lp"); // write the model to a file
  111. cplex.solve();
  112.  
  113. double intTolerance = cplex.getParam(IloCplex::EpInt);
  114.  
  115. IloNumArray values(env);
  116. double obj = cplex.getObjValue();
  117. printf("Objective value: %3.2fn",obj);
  118. cplex.getValues(values, variables);
  119. int total = 0;
  120. for (j=0; j!=nbOvens*nbCities; j++){
  121. if (values[j] > intTolerance){
  122. printf("Object %d n", j);
  123. }
  124. }
  125.  
  126. }catch(IloException &e){
  127. std::cerr << "Concert exception caught: " << e << std::endl;
  128. }
  129. env.end(); // destroy the environment (to free memory)
  130.  
  131. printf("Press Enter to continue...n");
  132. getchar();
  133.  
  134. return app.exec();
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement