Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ! This file is the mathematical model of the transportation problem
- ! Created by Henrik Andersson 2010-05-01, updated 2011-09-15
- ! Henrik Andersson, IØT, 735 93592, henrik.andersson@iot.ntnu.no
- ! ------------------------------------------------------------------------------
- model TransportationProblem
- ! The model is given the name TransportationProblem
- ! Compiler directives
- ! ------------------------------------------------------------------------------
- options explterm
- ! This option means that all lines must end with a ;
- options noimplicit
- ! This option means that everything must be declared before it is used
- uses "mmxprs";
- ! mmxprs is the library including the optimizer
- ! Parameters
- ! ------------------------------------------------------------------------------
- parameters
- DataFile = 'ExampleData.txt';
- end-parameters
- ! Parameters are scalars (integer, real, boolean, string).
- ! Parameters can be changed during run time.
- ! Body
- ! ------------------------------------------------------------------------------
- declarations
- nProducers: integer;
- nCustomers: integer;
- end-declarations
- ! Data describing the size of the problem
- initializations from DataFile
- nProducers;
- nCustomers;
- end-initializations
- ! The data is read from the file specified by DataFile
- write('The instance has ', nProducers, ' producers, ');
- writeln(' and ', nCustomers, ' customers.');
- ! Information about the size of the instance solved is printed to the screen
- ! Use write to print without line break
- ! Writeln ends the printed string with a line break
- declarations
- Producers: set of integer;
- Customers: set of integer;
- end-declarations
- ! Declare sets
- Producers := 1 .. nProducers;
- Customers := 1 .. nCustomers;
- ! Define the sets based on the number of facilities
- finalize(Producers);
- finalize(Customers);
- ! All sets are finalized. This means that it is no longer possible to add or
- ! remove elements from the sets
- writeln('Producers : ', Producers);
- writeln('Customers : ', Customers);
- ! Print all elements of each set
- declarations
- Cost: array(Producers, Customers) of integer;
- Supply: array(Producers) of integer;
- Demand: array(Customers) of integer;
- end-declarations
- ! The data is declared. Based on the data and how the data is stored, different
- ! ways of defining the data can be used.
- initializations from DataFile
- Cost;
- Supply;
- Demand;
- end-initializations
- ! Data is read from the file specified by DataFile.
- writeln(Cost);
- writeln(Supply);
- writeln(Demand);
- declarations
- Flow: dynamic array(Producers, Customers) of mpvar;
- end-declarations
- ! Dynamic arrays are used to declare indexed variables.
- ! All variables are by default >= 0
- forall (pp in Producers, cc in Customers) do
- create(Flow(pp,cc));
- end-do
- ! Variables declared in dynamic arrays must be created
- write('There are ');
- write(count(pp in Producers, cc in Customers));
- writeln(' variables in total');
- ! The number of variables is printed
- declarations
- TotalCost: linctr;
- SupplyCon: dynamic array(Producers) of linctr;
- DemandCon: dynamic array(Customers) of linctr;
- end-declarations
- TotalCost :=
- sum (pp in Producers, cc in Customers) Cost(pp,cc)*Flow(pp,cc);
- forall (pp in Producers) do
- SupplyCon(pp) :=
- sum (cc in Customers) Flow(pp,cc) <= Supply(pp);
- end-do
- forall (cc in Customers) do
- DemandCon(cc) :=
- sum (pp in Producers) Flow(pp,cc) = Demand(cc);
- end-do
- minimize(TotalCost);
- ! Tells the optimizer to minimize the objective function TotalCost, all constraints are
- ! automatically included in the model
- writeln;
- writeln('Optimal objective value : ', getobjval);
- ! getobjval is used to retreive the optimal objective value
- writeln;
- forall (pp in Producers, cc in Customers | getsol(Flow(pp,cc)) > 0.01) do
- write(strfmt(getsol(Flow(pp,cc)),4), ' is sent from producer ');
- writeln(pp, ' to customer ', cc);
- end-do
- ! strfmt makes it possible to format the output
- ! getsol(Var) returns the value of the variable Var
- end-model
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement