forked from jfrchicanog/TallerUAL2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nrp.R
239 lines (216 loc) · 8.27 KB
/
nrp.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
readNrpInstance <- function(file) {
numbers <- scan(file)
index <- 1
requirementCosts <- numeric()
# Read requirement costs
levels <- numbers[index]
index <- index+1
for (level in 1:levels) {
numberOfRequirementsInLevel <- numbers[index]
requirementCosts <- c(requirementCosts,numbers[(1:numberOfRequirementsInLevel)+index])
index <- index+numberOfRequirementsInLevel+1
}
# Read dependencies
numberOfDependencies <- numbers[index]
if (numberOfDependencies > 0) {
dependencies <- matrix(numbers[index+1:(2*numberOfDependencies)],ncol = 2,byrow=TRUE)
} else {
dependencies <- matrix(0,nrow=0,ncol=2)
}
index <- index+2*numberOfDependencies+1;
# Read stakeholders
numberOfStakeholders <- numbers[index]
index <- index+1;
stakeholdersWeights <- numeric()
stakeholdersRequirements <- list()
for (stakeholder in 1:numberOfStakeholders) {
stakeholdersWeights[stakeholder] <- numbers[index]
index <- index+1
requirementsOfStakeholder <- numbers[index]
stakeholdersRequirements[[stakeholder]] <- numbers[(1:requirementsOfStakeholder)+index]
index <- index + requirementsOfStakeholder + 1;
}
result <- list(requirementCosts, dependencies, stakeholdersWeights, stakeholdersRequirements)
names(result) <- c("requirementCosts", "dependencies", "stakeholdersWeights", "stakeholdersRequirements")
return(result)
}
ilpModel <- function(nrpInstance, budgetLimitFraction) {
n <- length(nrpInstance$requirementCosts)
m <- length(nrpInstance$stakeholdersWeights)
budget <- budgetLimitFraction * sum(nrpInstance$requirementCosts)
# Set objective expression
objectiveVector <- c(rep(0,n),nrpInstance$stakeholdersWeights)
# Set cost constraint
constraints <- c(nrpInstance$requirementCosts,rep(0,times=m))
nrows <- 1
# Set dependencies constraints
if (dim(nrpInstance$dependencies)[1] > 0) {
for (dep in 1:dim(nrpInstance$dependencies)[1]) {
row <- rep(0,n+m)
row[nrpInstance$dependencies[dep,1]] <- -1
row[nrpInstance$dependencies[dep,2]] <- 1
constraints <- c(constraints,row)
nrows <- nrows+1;
}
}
# Set stakeholders constraints
for (stakeholder in 1:length(nrpInstance$stakeholdersRequirements)) {
for (requirement in nrpInstance$stakeholdersRequirements[[stakeholder]]) {
row <- rep(0,n+m)
row[requirement] <- -1
row[stakeholder+n] <- 1
constraints <- c(constraints,row)
nrows <- nrows+1;
}
}
# Prepare the output
constraintMatrix <- matrix(constraints,nrow=nrows,byrow=TRUE)
direction <- rep("<=",nrows);
rightHandSide <- c(budget,rep(0,nrows-1))
types <- rep("B", times=n+m)
result <- list(objectiveVector, constraintMatrix, direction, rightHandSide, types, TRUE)
names(result) <- c("obj", "mat", "dir", "rhs", "types", "max")
return(result)
}
ilpModelWithComponents <- function(nrpInstance, budgetLimitFraction, stakeholders=NULL) {
if (is.null(stakeholders)) {
stakeholders = 1:length(nrpInstance$stakeholdersWeights)
#print (stakeholders)
}
n <- length(nrpInstance$requirementCosts)
m <- length(stakeholders)
budget <- budgetLimitFraction * sum(nrpInstance$requirementCosts)
# Set objective expression
objectiveVector <- c(rep(0,n),nrpInstance$stakeholdersWeights[stakeholders])
# Set cost constraint
constraints <- c(nrpInstance$requirementCosts,rep(0,times=m))
nrows <- 1
# Set dependencies constraints
if (dim(nrpInstance$dependencies)[1] > 0) {
for (dep in 1:dim(nrpInstance$dependencies)[1]) {
row <- rep(0,n+m)
row[nrpInstance$dependencies[dep,1]] <- -1
row[nrpInstance$dependencies[dep,2]] <- 1
constraints <- c(constraints,row)
nrows <- nrows+1;
}
}
# Set stakeholders constraints
for (stakeholder in 1:m) {
for (requirement in nrpInstance$stakeholdersRequirements[[stakeholders[stakeholder]]]) {
row <- rep(0,n+m)
row[requirement] <- -1
row[stakeholder+n] <- 1
constraints <- c(constraints,row)
nrows <- nrows+1;
}
}
# Prepare the output
constraintMatrix <- matrix(constraints,nrow=nrows,byrow=TRUE)
direction <- rep("<=",nrows);
rightHandSide <- c(budget,rep(0,nrows-1))
types <- rep("B", times=n+m)
result <- list(objectiveVector, constraintMatrix, direction, rightHandSide, types, TRUE, stakeholders)
names(result) <- c("obj", "mat", "dir", "rhs", "types", "max", "stakeholders")
return(result)
}
computeAllRequirementsForStakeholders <- function(nrpInstance) {
# Initialize data structures
n <- length(nrpInstance$requirementCosts)
m <- length(nrpInstance$stakeholdersWeights)
unprocessedDirectDependencies <- numeric(0)
toProcess <- numeric(0)
dependents <- matrix(FALSE, nrow=n, ncol=n)
dependencies <- matrix(FALSE, nrow=n, ncol=n)
dependents[nrpInstance$dependencies] <- TRUE
for (d in 1:n) {
unprocessedDirectDependencies[d] = sum(dependents[,d])
if (unprocessedDirectDependencies[d] == 0) {
toProcess <- c(toProcess, d)
}
}
# Start loop
while (length (toProcess) > 0) {
req <- toProcess[1]
toProcess <- toProcess[-1]
for (dep in (1:n)[dependents[req,]]) {
dependencies[dep,] <- dependencies[dep,] | dependencies[req,]
dependencies[dep,req] <- TRUE
unprocessedDirectDependencies[dep] <- unprocessedDirectDependencies[dep]-1
if (unprocessedDirectDependencies[dep] == 0) {
toProcess <- c(toProcess, dep)
}
}
}
if (sum(unprocessedDirectDependencies) > 0) {
print("Error!! Not all nodes have been processed")
}
stakeholdersDependencies <- list()
for (stakeholder in 1:m) {
requirements <- rep(FALSE, times=n)
for (requirement in nrpInstance$stakeholdersRequirements[[stakeholder]]) {
requirements <- requirements | dependencies[requirement,]
requirements[requirement] <- TRUE
}
stakeholdersDependencies[[stakeholder]] <- (1:n)[requirements]
}
return(stakeholdersDependencies)
}
generateLocalOptima <- function(nrpInstance, budgetLimitFraction) {
n <- length(nrpInstance$requirementCosts)
m <- length(nrpInstance$stakeholdersWeights)
budget <- budgetLimitFraction * sum(nrpInstance$requirementCosts)
stakeholderRequirements <- computeAllRequirementsForStakeholders(nrpInstance)
solution <- rep(FALSE,times=n+m)
cost <- 0
value <- 0
for (stakeholder in sample(m)) {
requirements <- stakeholderRequirements[[stakeholder]]
requirementsToAdd <- requirements[!solution[requirements]]
addedCost <- sum(nrpInstance$requirementCosts[requirementsToAdd])
if (addedCost+cost <= budget) {
value <- value + nrpInstance$stakeholdersWeights[stakeholder]
cost <- cost + addedCost
solution[n+stakeholder] <- TRUE
solution[requirementsToAdd] <- TRUE
}
}
result <- list((1:n)[solution[1:n]], (1:m)[solution[n+(1:m)]], cost, value)
names(result) <- c("requirements", "stakeholders", "cost", "value")
return(result)
}
cmsa <- function(nrpInstance, budgetLimitFraction, na, maxAge, ilpTimeLimit, totalTimeLimit) {
n <- length(nrpInstance$requirementCosts)
m <- length(nrpInstance$stakeholdersWeights)
age <- rep(maxAge, times=m)
startTime = proc.time()[3]
while (proc.time()[3]-startTime < totalTimeLimit) {
# Lines 5 to 10 of CMSA
generatedStakeholders <- numeric()
for (i in 1:na) {
solution <- generateLocalOptima(nrpInstance, budgetLimitFraction)
generatedStakeholders <- union(generatedStakeholders, solution$stakeholders)
}
for (stakeholder in generatedStakeholders) {
if (age[stakeholder] >= maxAge) {
age[stakeholder] <- 0
}
}
# Apply exact solver (Line 11 of CMSA)
stakeholders <- (1:m)[age < maxAge]
mprime <- length(stakeholders)
model <- ilpModelWithComponents(nrpInstance, budgetLimitFraction, stakeholders)
ilpSolution <- Rsymphony_solve_LP(obj=model$obj,
mat=model$mat,
dir=model$dir,
rhs=model$rhs,
types=model$types,
max=model$max,
time_limit = ilpTimeLimit)
print(ilpSolution$objval)
# Adapt (line 13 of CMSA)
age <- age+1
selectedStakeholders <- stakeholders[ilpSolution$solution[n+(1:mprime)] > 0.5]
age[selectedStakeholders] <- 0
}
}