-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add new search strategy: Large Neighborhood Search (LNS) #204
Conversation
… solution to returning model
src/CP/core/search/strategies.jl
Outdated
|
||
""" | ||
struct LNSearch <: SearchStrategy | ||
implements the basic version of the Large Neighboorhood Search. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can add a bit more doc about the Large Neighboorhood search or provide an external link
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
src/CP/core/search/lns.jl
Outdated
|
||
### Destroy and repair loop ### | ||
|
||
while (isnothing(globalTimeLimit) || peektimer() < globalTimeLimit) && bestSolution[objective] > optimalScore |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
optimalScore
can be deceiving. I 'd rather use optimalScoreLowerBound
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
src/CP/core/search/lns.jl
Outdated
function destroy(model, solution, numberOfValuesToRemove, objective) | ||
|
||
# Reset model | ||
objectiveBound = solution[objective] - 1 | ||
SeaPearl.reset_model!(model) | ||
|
||
# Get variable fixed by current solution | ||
vars = collect(values(model.variables)) | ||
branchableVariablesId = collect(filter(e -> model.branchable[e], keys(model.branchable))) | ||
varsToSet = sample(branchableVariablesId, count(values(model.branchable)) - numberOfValuesToRemove; replace=false) | ||
|
||
# Fix some variables as in current solution | ||
for var in varsToSet | ||
variable = vars[findfirst(e -> e.id == var, vars)] | ||
value = solution[var] | ||
SeaPearl.assign!(variable, value) | ||
end | ||
|
||
# Pruning the objective domain to force the search for a better solution | ||
objectiveVariable = vars[findfirst(e -> e.id == objective, vars)] | ||
SeaPearl.removeAbove!(objectiveVariable.domain, objectiveBound) | ||
|
||
return model | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some testsets on destroy
function would be usefull
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
src/CP/core/search/lns.jl
Outdated
# Get variable fixed by current solution | ||
vars = collect(values(model.variables)) | ||
branchableVariablesId = collect(filter(e -> model.branchable[e], keys(model.branchable))) | ||
varsToSet = sample(branchableVariablesId, count(values(model.branchable)) - numberOfValuesToRemove; replace=false) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add seed on the LNSearch
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done! I tried to add tests for the seed
argument but I think there is no way to do it... But I have tested locally and it works properly!
src/CP/core/search/lns.jl
Outdated
function repair(model, repairSearch, objective, variableHeuristic, valueSelection) | ||
search!(model, repairSearch, variableHeuristic, valueSelection) | ||
solutions = filter(e -> !isnothing(e), model.statistics.solutions) | ||
|
||
if isempty(solutions) | ||
toReturn = nothing | ||
else | ||
scores = map(solution -> solution[objective], solutions) | ||
bestSolution = solutions[findfirst(score -> score == Base.minimum(scores), scores)] | ||
toReturn = bestSolution | ||
end | ||
return toReturn | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some testsets on repair
function would be usefull
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice testsets, you clearly understood the underlying benefit of having diverse unit tests !
We just need to fix the little bug regarding the package CircularArrayBuffer.jl and you can merge the PR !
test/CP/core/search/lns.jl
Outdated
|
||
search = SeaPearl.LNSearch(limitValuesToRemove = 1) | ||
status = SeaPearl.expandLns!(search, model, SeaPearl.MinDomainVariableSelection(), SeaPearl.BasicHeuristic()) | ||
@test status === :NonOptimal |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you recall why the result is not Optimal ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because the first solution found (during initialization) will be {x => 2, y => 2} (because BasicHeuristic
choose the biggest value in the domain). This solution is non optimal as x is the objective variable and his domain is {1, 2}. As we set limitValuesToRemove
to 1, the destroy and repair loop will never reach any solution (because of the constraint Equal(x, y, trailer)
). In that way, the solution returned will be {x => 2, y => 2} (≠ to optimal solution {x => 1, y => 1}).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will add some comments in test file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok thanks, that's perfectly clear!
Last query : could you update some functions documentation to satisfy the standard function head where the arguments are individually listed and explained? |
Everything looks good for me ! You can squash & merge the PR ! |
Implementation of Large Neighborhood Search (LNS) to add a new search strategy to SeaPearl (existing strategies: DFS, IDS and RBS)