-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.jl
190 lines (179 loc) · 7.37 KB
/
main.jl
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
#Ferrari Leon
#M1 ORO
#Version de test
#Julia JuMP
#DM1 - Metaheuristiques
using JuMP, GLPKMathProgInterface,PyPlot
include("myHeuristics.jl")
include("plotting_meta.jl")
type Problem
NBvariables::Int
NBconstraints::Int
Variables::Vector{Int}
LeftMembers_Constraints::SparseMatrixCSC{Float64,Int64}
RightMembers_Constraints::Vector{Int}
end
type CurrentSolution
NBconstraints::Int
NBvariables::Int
CurrentObjectiveValue::Int64
Variables::Vector{Int}
CurrentVariables::Vector{Int}
CurrentVarUsed::Vector{Int}
LeftMembers_Constraints::SparseMatrixCSC{Float64,Int64}
LastRightMemberValue_Constraint::Vector{Int}
Utility::Array{Float64,2}
Freedom::Vector{Int}
end
type Result
Name::String
NBvariables::Int
GLPKObj::Int
HeurConsObj::Int
HeureLSObj::Int
GLPKTime::Float64
HeurConsTime::Float64
HeurLSTime::Float64
Result() = new()#Allow to create unintialized type
end
type ProbStat
Max::Int64
Average::Vector{Float64}
Min::Int64
NBdone::Vector{Float64}
HeurConsTime::Vector{Float64}
HeurLSTime::Vector{Float64}
Sum::Vector{Int64}
BestSolution::CurrentSolution
end
function ReadFile(FileName::String)
workingfile = open(FileName)
NBcons,NBvar = parse.(split(readline(workingfile)))
Coef = parse.(split(readline(workingfile)))
LeftMembers_Constraints = spzeros(NBcons,NBvar)
RightMembers_Constraints = Vector(NBcons)
for i = 1:1:NBcons
readline(workingfile)
RightMembers_Constraints[i]=1
for val in split(readline(workingfile))
LeftMembers_Constraints[i, parse(val)]=1
end
end
close(workingfile)
return Problem(NBvar, NBcons, Coef, LeftMembers_Constraints, RightMembers_Constraints)
end
#GETTING DATA FRON FILE
nbProb = 1
FileList = readdir("./Data")
dir = pwd()
Resume = Vector{Result}(20)
for i in eachindex(FileList)
#MODEL CONSTRUCTION
m = Model(solver=GLPKSolverMIP())
#READING DATA FROM FILE
BPP = ReadFile(string("./Data/",FileList[i]))
#if nbProb <= 4 && BPP.NBvariables <= 100 && BPP.NBconstraints <= 400
# FileList[i] == "pb_100rnd0700.dat" || FileList[i] == "pb_100rnd0700.dat" || FileList[i] == "pb_1000rnd0100.dat" || FileList[i] =="pb_100rnd0100.dat" || FileList[i] == "pb_2000rnd0100.dat" || FileList[i] == "pb_200rnd0100.dat" || FileList[i] == "pb_500rnd0100.dat"
# BPP.NBvariables <= 1000 && BPP.NBconstraints <= 500 && nbProb <= 12
# BPP.NBvariables == 100 && nbProb <10
if FileList[i] == "pb_100rnd0100.dat"
println("Probleme : ",FileList[i])
ProbTemp = Result();
CSB = CurrentSolution(BPP.NBconstraints, BPP.NBvariables, 0, BPP.Variables,zeros(BPP.NBvariables),zeros(Int64,0), BPP.LeftMembers_Constraints, zeros(BPP.NBconstraints), zeros(2,BPP.NBvariables), zeros(BPP.NBvariables))
AlphaVal = [0.5, 0.6, 0.75, 0.9]
AlphaProba = [0.25,0.25,0.25,0.25]
println("Before the run we got these Lambda :")
println(AlphaVal)
println("With these probabilities :")
println(AlphaProba)
Stat = ProbStat(0,zeros(Float64,4),typemax(Int64),zeros(Float64,4),zeros(Float64,4),zeros(Float64,4),zeros(Int64,4),CSB)
itmax = 25
itmax1 = 10
GraspOBJ = Vector{Int64}(itmax*itmax1)
MaxObj = Vector{Int64}(itmax*itmax1)
LSOBJ = Vector{Int64}(itmax*itmax1)
zmaxit = Vector{Int64}(itmax1)
zminit = Vector{Int64}(itmax1)
zmoyit = Vector{Int64}(itmax1)
avgttot = 0
#fill!(AlphaValueOBJ,Vector{Int64})
@time for k in 1:1:itmax1
for j in 1:1:itmax
currIndex = ((k-1)*itmax)+j
CS = deepcopy(CSB)
indLa,Alpha = ReactiveGrasp(AlphaProba,AlphaVal)
Stat.HeurConsTime[indLa] += @elapsed CS = GraspConstruction(CS,Alpha)
GraspOBJ[currIndex] = CS.CurrentObjectiveValue
Stat.HeurLSTime[indLa] += @elapsed CS = SimpleGreedyLocalSearch(CS)
#Stat.HeurLSTime[indLa] += @elapsed CS = SimulatedAnnealing(CS,100.0,0.95,100,1.0)
LSOBJ[currIndex] = CS.CurrentObjectiveValue
if CS.CurrentObjectiveValue > Stat.Max
Stat.Max = CS.CurrentObjectiveValue
Stat.BestSolution = deepcopy(CS)
elseif CS.CurrentObjectiveValue < Stat.Min
Stat.Min = CS.CurrentObjectiveValue
end
MaxObj[currIndex] = Stat.Max
Stat.Sum[indLa] += CS.CurrentObjectiveValue
Stat.NBdone[indLa] += 1
avgttot += CS.CurrentObjectiveValue
end
for d in 1:1:4
Stat.Average[d] = Stat.Sum[d]/Stat.NBdone[d]
Stat.HeurConsTime[d] = Stat.HeurConsTime[d]/Stat.NBdone[d]
Stat.HeurLSTime[d] = Stat.HeurLSTime[d]/Stat.NBdone[d]
Stat.HeurConsTime[d] = round(Stat.HeurConsTime[d],5)
Stat.HeurLSTime[d] = round(Stat.HeurLSTime[d],5)
end
zmaxit[k] = Stat.Max
zminit[k] = Stat.Min
zmoyit[k] = convert(Int64, floor(avgttot /(itmax)))
AlphaProba = UpdateReactiveGrasp(AlphaProba, Stat.Average,Stat.Min,Stat.Max)
#=println("After the ",k*20," run we got :")
println("Maximum found : ",Stat.Max)
println("Minimum found : ",Stat.Min)
println("Average : ",zmoyit[k])
println("Alpha proba : ",AlphaProba)=#
end
#=println("After the ",itmax1*itmax," run we got :")
println(AlphaProba)
println(AlphaVal)
println("Maximum found : ",Stat.Max)
println("Minimum found : ",Stat.Min)
println("Average : ",Stat.Average)
println("Average GRASP construction time :",mean(Stat.HeurConsTime))
println("Average GRASP construction value :",mean(GraspOBJ))
println("Average LS time :",mean(Stat.HeurLSTime))
println("Average LS value :",mean(LSOBJ))
println("Number of runs : ",Stat.NBdone)
println("Grasp construction : ",Stat.BestSolution.CurrentObjectiveValue)
=#
HistoryX = Vector{Int64}(itmax1)
for div = 1:itmax1
HistoryX[div] = itmax * div
end
#plotRunGrasp(FileList[i],GraspOBJ, LSOBJ, MaxObj)
plotAnalyseGrasp(FileList[i],HistoryX,zmoyit,zminit,zmaxit)
#=
@variable( m , x[1:BPP.NBvariables], Bin)
@objective( m , Max, sum( BPP.Variables[j] * x[j] for j=1:BPP.NBvariables ) )
@constraint( m , cte[i=1:BPP.NBconstraints], sum(BPP.LeftMembers_Constraints[i,j] * x[j] for j=1:BPP.NBvariables) <= BPP.RightMembers_Constraints[i] )
#SOLVE IT AND DISPLAY THE RESULTS
#--------------------------------
ProbTemp.GLPKTime = @elapsed solve(m) # solves the model
ProbTemp.NBvariables = BPP.NBvariables
ProbTemp.Name = string("./Data/",FileList[i])
ProbTemp.GLPKObj = getobjectivevalue(m)
ProbTemp.HeurConsObj = CS.CurrentObjectiveValue
Resume[nbProb] = deepcopy(ProbTemp)=#
nbProb+=1
elseif nbProb >=12
break;
end
end
#=
for resu in eachindex(Resume)
println("For the problem ",Resume[resu].Name," with ",Resume[resu].NBvariables, " variables.")
println("GLPK Solver Objective value : ",Resume[resu].GLPKObj," Time : ",Resume[resu].GLPKTime ) # getObjectiveValue(model_name) gives the optimum objective value
println("Heuristics :",Resume[resu].HeurObj," Time : ",Resume[resu].HeurTime,"\n")
end=#