代写小学数学作业-代写作业,XPRESSIVE TUTORIAL – DIRECTED LEARNING
Part A is for anyone with no prior experience of using the optimisation software Xpressive.
Part B is for more advanced users and is essential prior learning for the module Mathematical Programming Techniques.
You can download this software free from the specialist drive (common area S:/). Any queries, assistance advice with respect to thios should be directed towards the specialist BCL Technicians in B201.
To make efficient use of Xpressive variables are indexed and loops are constructed to enable the modelling of large problems (many variables).
For example:
X(1), X(2),.... etc
X(1,1), X(1,2).... etc
This handout will now take you through a number of examples of typical models representing problems in constrained linear optimisation.
PART A
The following example is modelled in XPRESS in several ways to demonstrate the alternatives ways in which a problem can be input into the package.
Example 1
This is based around CP example 1 the addition here being “indexed” variables, the relevant new section is shown in bold.
model ex1
uses "mmxprs"
! The next instructions create a set of indexed variables
declarations
x: dynamic array(1..2) of mpvar
! this declares variables x(1) and x(2)
end-declarations
! now create the indexed variables
forall(i in 1..2) create(x(i))
con1:=x(1)+2*x(2)<=20
con2:=3*x(1)+x(2)<=30
profit:=10*x(1)+12*x(2)
maximise(profit)
writeln("profit =",getobjval)
writeln("x1=",getsol(x(1)))
writeln("x2=",getsol(x(2)))
end-model
Example 2
Here coefficient tables/matrices are used to set up coefficients profits and right hand side values.
model ex2
uses "mmxprs"
declarations
! The following sets up arrays for the problem data.
x: dynamic array(1..2) of mpvar
! set up coefficient and right hand side matrices
T:array(1..2,1..2) of integer
P:array(1..2) of integer
end-declarations
forall(i in 1..2) create(x(i))
!set up coefficient and right hand side values
T::[1,2,3,1]
P::[50,12]
! Indexed variables now used to specify the model
con1:=T(1,1)*x(1)+T(1,2)*x(2)<=20
con2:=T(2,1)*x(1)+T(2,2)*x(2)<=30
profit:=P(1)*x(1)+P(2)*x(2)
maximise(profit)
writeln("profit =",getobjval)
writeln("x1=",getsol(x(1)))
writeln("x2=",getsol(x(2)))
end-model
Example 3
A new way to construct a constraint, not very interesting or helpful!
model ex3
uses "mmxprs"
declarations
x: dynamic array(1..2) of mpvar
T:array(1..2,1..2) of integer
P:array(1..2) of integer
B:array(1..2) of integer
end-declarations
forall(i in 1..2) create(x(i))
T::[1,2,3,1]
P::[50,12]
B::[20,30]
! The equation can be constructed in stages first set up string c
c:= sum(j in 1..2) T(1,j)*x(j)
con1:= c <=B(1)
con2:=T(2,1)*x(1)+T(2,2)*x(2)<=B(2)
profit:=P(1)*x(1)+P(2)*x(2)
maximise(profit)
writeln("profit =",getobjval)
forall(i in 1..2) do
writeln("x”, i ,”=",getsol(x(i)))
end-do
end-model
PART B
In larger problems it is always better to use commands to construct the model (equations and profit functions).
Example 4
Using “loops” to construct constraints and cost functions to set up and solve the problem used in example 1.
Model ex4
uses "mmxprs"
declarations
x: dynamic array(1..2) of mpvar
T:array(1..2,1..2) of integer
P:array(1..2) of integer
B:array(1..2) of integer
end-declarations
forall(i in 1..2) create(x(i))
T::[1,2,3,1]
P::[50,12]
B::[20,30]
! Here sum statements are used to set up the constraints
con1:= sum(j in 1..2) T(1,j)*x(j) <=B(1)
con2:=sum(j in 1..2) T(2,j)*x(j)<=B(2)
! Again a sum statement is used to construct the profit expression
profit:=sum(j in 1..2)P(j)*x(j)
maximise(profit)
writeln("profit =",getobjval)
writeln("x1=",getsol(x(1)))
writeln("x2=",getsol(x(2)))
end-model
note: if a problem contained 25 variables the “construct” part of the program would become:
! Here sum statements are used to set up the constraints
con1:= sum(j in 1..25) T(1,j)*x(j) <=B(1)
con2:=sum(j in 1..25) T(2,j)*x(j)<=B(2)
! Again a sum statement is used to construct the profit expression
profit:=sum(j in 1..25)P(j)*x(j)
when there are many constraints and many variables the “construction” of the model can be achieved using a double loop:
• loop 1 number of equations
• loop2 number of constraints
Example 5
Using the same problem, to illustrate this approach.
model ex5
uses "mmxprs"
declarations
x: dynamic array(1..2) of mpvar
T:array(1..3,1..2) of integer
P:array(1..2) of integer
B:array(1..3) of integer
end-declarations
forall(i in 1..2) create(x(i))
T::[1,2,3,1,1,1]
P::[10,12]
B::[20,30,13]
! Here sum statements are used to set up all the constraints in one statement
forall(i in 1..3) do
con(i):= sum(j in 1..2) T(i,j)*x(j) <=B(i)
end-do
! Again a sum statement is used to construct the profit expression
profit:=sum(j in 1..2)P(j)*x(j)
maximise(profit)
writeln("profit =",getobjval)
writeln("x1=",getsol(x(1)))
writeln("x2=",getsol(x(2)))
end-model
Note for a problem with 32 constraints and 26 variables the “construction” part of the program would become:
forall(i in 1..32) do
con(i):= sum(j in 1..26) T(i,j)*x(j) <=B(i)
end-do
Example 6 Transportation Problem
The Transportation problem:-
A B Supply
X 10
X(1) 12
X(2)
=20
Y 8
X(3) 14
X(4)
=30
Demand
=23
=27
Using the previous notation, P for costs B for capacities and T for coefficients here
P = [10 12 8 14]
B = [20 30 23 27]
T = [1 1 0 0;
0 0 1 1;
1 0 1 0;
0 1 0 1]
Can be solved in the following model where the “equations” are constructed using the blue coding.
model ex6
uses "mmxprs"
declarations
x: dynamic array(1..4) of mpvar
P:array(1..4) of integer
B:array(1..4) of integer
end-declarations
P::[10,12,8,14]
B::[20,30,23,27]
forall(i in 1..4) create(x(i))
! constructing the row and column constraint but in two stages
! ROWS
forall(i in 1..2) do
c(i):=sum(j in 1..2) x((i-1)*2+j)=B(i)
end-do
! COLUMNS
forall(i in 1..2) do
cc(i):=sum(j in 1..2) x((j-1)*2+i)=B(i+2)
end-do
maximise(profit)
writeln("profit =",getobjval)
forall(i in 1..4) do
writeln("x",i,"=",getsol(x(i)))
end-do
end-model
Note: if there were 7 rows and 9 columns these would become
! constructing the row and column constraint but in two stages
! ROWS
forall(i in 1..7) do
c(i):=sum(j in 1..9) x((i-1)*9+j)=B(i)
end-do
! COLUMNS
forall(i in 1..9) do
cc(i):=sum(j in 1..7) x((j-1)*9+i)=B(i+7)
end-do
Alternatively
model ex6_a
uses "mmxprs"
declarations
x: dynamic array(1..2,1..2) of mpvar
P:array(1..4) of integer
R:array(1..2) of integer
C:array[1..2]
end-declarations
P::[10,12,8,14]
R::[20,30]
C::[23,27]
forall(i in 1..2,1..2) create(x(i,j))
! constructing the row and column constraint but in two stages
! ROWS
forall(i in 1..2) do
c(i):=sum(j in 1..2) x(i,j)=R(i)
end-do
! COLUMNS
forall(j in 1..2) do
cc(i):=sum(i in 1..2) x(i,j)=C(j)
end-do
Introduction to Integer Variables (example 7)
This model produces both the LP and IP solution for a n variable problem. The LP stage is as before the IP section is added at the end (in red)
model ex7
uses "mmxprs"
declarations
x: dynamic array(1..2) of mpvar
end-declarations
forall(i in 1..2) create(x(i))
first:= 3*x(1) + 2*x(2) <= 19
second:= x(1) + 3*x(2) <= 14
profit:= x(1) + 2*x(2)
maximise(profit)
writeln("profit =",getobjval)
writeln("x1=",getsol(x(1)))
writeln("x2=",getsol(x(2)))
! solution x(1)=4.14286, x(2)= 3.28571, profit = 10.7143
! now the Integer solution, first declare the variables as being Integers.
!notice that this cannot be within the declarations, seems strange.
forall(i in 1..2)
x(i) is_integer
maximise(profit)
writeln("profit =",getobjval)
writeln("x1I=",getsol(x(1)))
writeln("x2I=",getsol(x(2)))
! solution x(1)=4, x(2)= 3, profit = 10
end-model
model ex7_a
uses "mmxprs"
declarations
x: dynamic array(1..2) of mpvar
end-declarations
forall(i in 1..2) create(x(i))
first:= 3*x(1) + 2*x(2) <= 19
second:= x(1) + 3*x(2) <= 14
profit:= x(1) + 2*x(2)
!LP solution
maximise(profit)
writeln("profit =",getobjval)
writeln("x1=",getsol(x(1)))
writeln("x2=",getsol(x(2)))
!IP solution
forall(i in 1..2)
x(i) is_integer
http://www.1daixie.com/daixiezuoye/maximise(profit)
writeln("profit =",getobjval)
writeln("x1I=",getsol(x(1)))
writeln("x2I=",getsol(x(2)))
!BV solution
forall(i in 1..2)
x(i) is_integer
forall(i in 1..2)
c(i):= x(i)<=1
maximise(profit)
writeln("profit =",getobjval)
writeln("x1BV=",getsol(x(1)))
writeln("x2BV=",getsol(x(2)))
代写文章end-model