-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlpSolveAPI
More file actions
96 lines (50 loc) · 3.04 KB
/
lpSolveAPI
File metadata and controls
96 lines (50 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
In this type of Linear Programming Problem I'm gonna show you how to model and calculate the optimization problem in R
The facility has 4 machines of type 1, five of type 2, three of type 3 and seven of type 4. Each machine operates 40 hours per week.
The problem is to determine the optimum weekly production quantities for the products. The goal is to maximize total profit. In
constructing a model, the first step is to define the decision variables; the next step is to write the constraints and objective
function in terms of these variables and the problem data. In the problem statement, phrases like "at least," "no greater than,"
"equal to," and "less than or equal to" imply one or more constraints. Quantity of Machine1 is 4, Machine2 5, Machine3 3 and Machine4 7.
Machine Quantity Product1 Product2 Product3 Product4 Product5
M1 4 1.2h 1.3h 0.7h 0.0h 0.5h
M2 5 0.7h 2.2h 1.6h 0.5h 1.0h
M3 3 0.9h 0.7h 1.3h 1.0h 0.8h
M4 7 1.4h 2.8h 0.5h 1.2h 0.6h
Profit,$ —— 18 25 10 12 15
#First off, we should formulate Objective function which will give max or min otimal solution. there are 5 products so we have 5decision
#variables P1(Product1),P2(Product2),P3(Product3),P4(Product4),P5(Product5). One unit of P1 is 18$, P2 25$, P3 10, P4 12$, and P5 15$
#so the objective function will be:
#Maximize Z = 18P1+ 25P2 + 10P3 + 12P4 + 15P5
#Then we should formulate constraints. For each machine the number of hours available is 40 hours. For P1 we spend 1.2 hours on M1
#All the constraints are dimensioned in hours. For machine 1, for example, we have 40 hrs./machine ¥ 4 machines = 160 hrs.
M1 : 1.2P1 + 1.3P2 + 0.7P3 + 0.0P4 + 0.5P5 <= 160
M2 : 0.7P1 + 2.2P2 + 1.6P3 + 0.5P4 + 1.0P5 <= 200
M3 : 0.9P1 + 0.7P2 + 1.3P3 + 1.0P4 + 0.8P5 <= 120
M4 : 1.4P1 + 2.8P2 + 0.5P3 + 1.2P4 + 0.6P5 <= 280
#That's the model of example let's solve it in R
#Firstly, we should install lpSolveAPI:
install.packages("lpSolveAPI")
require(package_lpSolveAPI)
#The list of constraints makes it clear that the MACHINE model needs to have 4 rows and 5 columns.
To build this model using lpSolveAPI, we write the following code in R;
MACHINE <- make.lp(4,5)
#In order to set the columns there should be issued following commands.
set.column(MACHINE, 1, c(1.2,0.7,0.9,1.4))
set.column(MACHINE, 2, c(1.3,2.2,0.7,2.8))
set.column(MACHINE, 3, c(0.7,1.6,1.3,0.5))
set.column(MACHINE, 4, c(0,0.5,1,1.2))
set.column(MACHINE, 5, c(0.5,1,0.8,0.6))
#After that type the direction of constraints
set.constr.type(MACHINE, c("<=","<=","<=","<=","<="))
#Right-hand side of constraint
set.rhs(MACHINE, c(160,200,120,280))
##Maximize Z = 18P1+ 25P2 + 10P3 + 12P4 + 15P5
set.objfn(MACHINE, c(18,25,10,12,15))
lp.control(MACHINE, sense="max")
solve(MACHINE)
get.objective(MACHINE)
get.variables(MACHINE)
#ANSWER will be
get.objective(MACHINE)
[1] 2988.727
get.variables(MACHINE)
[1] 58.96137 62.63458 0.00000 10.57631 15.64281