-
Notifications
You must be signed in to change notification settings - Fork 0
/
simplex.R
201 lines (153 loc) · 4.37 KB
/
simplex.R
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
############
### Example 1
############
#Cost vector
c <- c(-8, -4, 0, 0, 0)
#Matrix of restrictions
A<-matrix(nrow=3,ncol=5)
A[1,] <- c(5, -2, 1, 0, 0)
A[2,] <- c(8, -2, 0, 1, 0)
A[3,] <- c(8, 1, 0, 0, 1)
#Right sides
b<-c(0,1,2)
#Initial solution
B<-matrix(nrow=3,ncol=3)
B[1,] <- c(1, 0, 0)
B[2,] <- c(0, 1, 0)
B[3,] <- c(0, 0, 1)
solIndexes <- c(3,4,5)
############
### Example 2
############
#Cost vector
c <- c(7, 1, 5, -4, 0, 0, M, M)
#Matrix of restrictions
A<-matrix(nrow=3,ncol=8)
A[1,] <- c(-2, 2, -6, -6, 0, 0, 1, 0)
A[2,] <- c(-4, 2, 5, 1,-1, 0, 0, 1)
A[3,] <- c(-2,-1, -1, -7, 0, 1, 0, 0)
#Right sides
b<-c(8,6,2)
#Initial solution
B<-matrix(nrow=3,ncol=3)
B[1,] <- c(1, 0, 0)
B[2,] <- c(0, 1, 0)
B[3,] <- c(0, 0, 1)
solIndexes <- c(7,8,6)
############
### Example 3
############
#Cost vector
c <- c(7, 1, 5, -4, 0, 0, M, M)
#Matrix of restrictions
A<-matrix(nrow=3,ncol=8)
A[1,] <- c(-2, 2, -6, -6, 0, 0, 1, 0)
A[2,] <- c(-4, 2, 5, 1,-1, 0, 0, 1)
A[3,] <- c(-2,-1, -1, -7, 0, 1, 0, 0)
#Right sides
b<-c(8,6,2)
#Initial solution
B<-matrix(nrow=3,ncol=3)
B[1,] <- c(1, 0, 0)
B[2,] <- c(0, 1, 0)
B[3,] <- c(0, 0, 1)
solIndexes <- c(7,8,6)
###############
### Implementation
###############
simplex <- function(c,A,b,B,solIndexes){
i = 0
j = 1
sum = 0
max = -1
min = 1000000
entryVariable = -1
exitVariable = -1
entryVariable.relative = -1
exitVariable.relative = -1
cb <- c()
entryCriterion <- c()
#Step 1: initialization
invB=solve(B) #inverse of matrix B
xb <- invB %*% b #initial solution array
for(i in solIndexes){ #cb array
cb <- c(cb, c[i])
}
cb[is.na(cb)] <- 0
noSolIndexes <- c() #indexes of the candidate variables
for(i in 1:5){
if(!i %in% solIndexes){
noSolIndexes <- c(noSolIndexes,i)
}
}
#the algorithm iterates
while(TRUE){
#Step 2: entry criterion
for(i in noSolIndexes){ #criterion to decide which variable is going to enter in the solution is obtained
ac <- A[,i]
y <- invB %*% ac
candidateVariableCost = c[i]
if(is.na(candidateVariableCost)) candidateVariableCost = 0
entryCriterion <- c(entryCriterion, cb %*% y - candidateVariableCost)
}
for(i in entryCriterion){ #maximum (variable that is going to enter is obtained)
if(i<=0){
sum = sum+1
}
else if(i > max){
max = i
entryVariable.relative = j
}
j = j + 1
}
if(sum == length(entryCriterion)){ #an optimal solution has been found
print("[ Optimal solution ]")
break
}
entryVariable = noSolIndexes[entryVariable.relative] #the index of the entr variable is obtained
#Step 3: exit criterion
y <- c()
sum = 0
j=1
y <- invB %*% A[,entryVariable]
for(i in y){
if(i <= 0){
sum = sum + 1
}else if(xb[j]/i < min){
min = xb[j]/i
exitVariable.relative = j
}
j = j + 1
}
exitVariable = solIndexes[exitVariable.relative]
if(sum == length(A[,entryVariable])){
return("[ Unbounded problem ]")
}
#Step 4: solution is recalculated
B[,exitVariable.relative] = A[,entryVariable]
invB=solve(B) #inverse of the B matrix
xb <- invB %*% b #solution is obtained
solIndexes[exitVariable.relative] = entryVariable
noSolIndexes[which(noSolIndexes==entryVariable)] = exitVariable
cb[exitVariable.relative] = c[entryVariable]
if(is.na(cb[exitVariable.relative])) cb[exitVariable.relative] = 0
#temporal variables are cleaned
i = 0
j = 1
sum = 0
max = -1
min = 1000000
entryVariable = -1
exitVariable = -1
entryVariable.relative = -1
exitVariable.relative = -1
entryCriterion <- c()
}
#return of values
z = cb[i]%*%xb[i]
return(list("Values of the variables" = xb, "Minimun cost" = z, "Base" = solIndexes))
}
############
### Execution
############
simplex(c,A,b,B,solIndexes)