Advertisement
Guest User

Xpress2

a guest
Sep 21st, 2015
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
XBasic 4.01 KB | None | 0 0
  1. ! This file is the mathematical model of the transportation problem
  2. ! Created by Henrik Andersson 2010-05-01, updated 2011-09-15
  3. ! Henrik Andersson, IØT, 735 93592, henrik.andersson@iot.ntnu.no
  4. ! ------------------------------------------------------------------------------
  5.  
  6. model TransportationProblem
  7. ! The model is given the name TransportationProblem
  8.  
  9. ! Compiler directives
  10. ! ------------------------------------------------------------------------------
  11.  
  12. options explterm
  13. ! This option means that all lines must end with a ;
  14. options noimplicit
  15. ! This option means that everything must be declared before it is used
  16.  
  17. uses "mmxprs";
  18. ! mmxprs is the library including the optimizer
  19.  
  20. ! Parameters
  21. ! ------------------------------------------------------------------------------
  22.  
  23. parameters
  24.     DataFile = 'ExampleData.txt';
  25. end-parameters
  26. ! Parameters are scalars (integer, real, boolean, string).
  27. ! Parameters can be changed during run time.
  28.  
  29. ! Body
  30. ! ------------------------------------------------------------------------------
  31.  
  32. declarations
  33.     nProducers:     integer;
  34.     nCustomers:     integer;
  35. end-declarations
  36. ! Data describing the size of the problem
  37.  
  38. initializations from DataFile
  39.     nProducers;
  40.     nCustomers;
  41. end-initializations
  42. ! The data is read from the file specified by DataFile
  43.  
  44. write('The instance has ', nProducers, ' producers, ');
  45. writeln(' and ', nCustomers, ' customers.');
  46. ! Information about the size of the instance solved is printed to the screen
  47. ! Use write to print without line break
  48. ! Writeln ends the printed string with a line break
  49.  
  50. declarations
  51.     Producers:      set of integer;
  52.     Customers:      set of integer;
  53. end-declarations
  54. ! Declare sets
  55.  
  56. Producers := 1 .. nProducers;
  57. Customers := 1 .. nCustomers;
  58. ! Define the sets based on the number of facilities
  59.  
  60. finalize(Producers);
  61. finalize(Customers);
  62. ! All sets are finalized. This means that it is no longer possible to add or
  63. ! remove elements from the sets
  64.  
  65. writeln('Producers : ', Producers);
  66. writeln('Customers : ', Customers);
  67. ! Print all elements of each set
  68.  
  69. declarations
  70.     Cost:           array(Producers, Customers)             of integer;
  71.     Supply:         array(Producers)                        of integer;
  72.     Demand:         array(Customers)                        of integer;
  73. end-declarations
  74. ! The data is declared. Based on the data and how the data is stored, different
  75. ! ways of defining the data can be used.
  76.  
  77. initializations from DataFile
  78.     Cost;
  79.     Supply;
  80.     Demand;
  81. end-initializations
  82. ! Data is read from the file specified by DataFile.
  83.  
  84. writeln(Cost);
  85. writeln(Supply);
  86. writeln(Demand);
  87.  
  88. declarations
  89.     Flow:           dynamic array(Producers, Customers)     of mpvar;
  90. end-declarations
  91. ! Dynamic arrays are used to declare indexed variables.
  92. ! All variables are by default >= 0
  93.  
  94. forall (pp in Producers, cc in Customers) do
  95.     create(Flow(pp,cc));
  96. end-do
  97. ! Variables declared in dynamic arrays must be created
  98.  
  99. write('There are ');
  100. write(count(pp in Producers, cc in Customers));
  101. writeln(' variables in total');
  102. ! The number of variables is printed
  103.  
  104. declarations
  105.     TotalCost:      linctr;
  106.     SupplyCon:      dynamic array(Producers)                of linctr;
  107.     DemandCon:      dynamic array(Customers)                of linctr;
  108. end-declarations
  109.  
  110. TotalCost :=
  111.     sum (pp in Producers, cc in Customers) Cost(pp,cc)*Flow(pp,cc);
  112.  
  113. forall (pp in Producers) do
  114.     SupplyCon(pp) :=
  115.         sum (cc in Customers) Flow(pp,cc) <= Supply(pp);
  116. end-do
  117.  
  118. forall (cc in Customers) do
  119.     DemandCon(cc) :=
  120.         sum (pp in Producers) Flow(pp,cc) = Demand(cc);
  121. end-do
  122.  
  123. minimize(TotalCost);
  124. ! Tells the optimizer to minimize the objective function TotalCost, all constraints are
  125. ! automatically included in the model
  126.  
  127. writeln;
  128. writeln('Optimal objective value : ', getobjval);
  129. ! getobjval is used to retreive the optimal objective value
  130.  
  131. writeln;
  132. forall (pp in Producers, cc in Customers | getsol(Flow(pp,cc)) > 0.01) do
  133.     write(strfmt(getsol(Flow(pp,cc)),4), ' is sent from producer ');
  134.     writeln(pp, ' to customer ', cc);
  135. end-do
  136. ! strfmt makes it possible to format the output
  137. ! getsol(Var) returns the value of the variable Var
  138.  
  139. end-model
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement