-
Notifications
You must be signed in to change notification settings - Fork 5
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 getBestMove and FirstBestAdmissibleTabuSearch #39
Add getBestMove and FirstBestAdmissibleTabuSearch #39
Conversation
…AdmissibleTabuSearch
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 have added some comments. Can you have a look? Please also add tests to get the coverage back up.
// track first improvement move AND best valid move + corresponding evaluation, validation and delta | ||
Move<? super SolutionType> bestMove = null, firstImprovementMove = null; | ||
double bestMoveDelta = -Double.MAX_VALUE, curMoveDelta, firstImprovementMoveDelta = -Double.MAX_VALUE; | ||
Evaluation curMoveEvaluation, bestMoveEvaluation = null, firstImprovementMoveEvaluation = null; |
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 don't need separate variables for first and best improvement. One variable, say chosenMove
, is sufficient to store the move you selected (either first improvement, or best). Then you also have chosenMoveDelta
, chosenMoveEvaluation
, and chosenMoveValidation
.
@SafeVarargs | ||
protected final Move<? super SolutionType> getBestMove(List<? extends Move<? super SolutionType>> moves, | ||
boolean requireImprovement, | ||
boolean acceptFirstImprovement, |
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.
Parameter acceptFirstImprovement
is never used. You implementation returns the first improvement, even if acceptFirstImprovement
is false
. See comments below to fix.
Evaluation curMoveEvaluation, bestMoveEvaluation = null, firstImprovementMoveEvaluation = null; | ||
Validation curMoveValidation, bestMoveValidation = null, firstImprovementMoveValidation = null; | ||
// go through all moves | ||
for (Move<? super SolutionType> move : moves) { |
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.
Please use an Iterator
here in a while
loop with multiple conditions to avoid the use of break
. You should continue as long as there are more moves to try and !(acceptFirstImprovement && chosenMoveDelta > 0)
.
* @return first improving move or the best valid move, may be <code>null</code> | ||
*/ | ||
@SafeVarargs | ||
protected final Move<? super SolutionType> getBestMove(Collection<? extends Move<? super SolutionType>> moves, |
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.
Once this method's implementation is finished, the other getBestMove
method should relay to this one with acceptFirstImprovement
set to 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.
Agree with the suggestion. Before adding tests, I found a problem in Line 445 of GenericProblem.java, which prevents the running of NeighbourhoodSearchTest. Could you have a look at it?
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.
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.
Problem solved by switching to IntelliJ IDEA
@SafeVarargs | ||
protected final Move<? super SolutionType> getBestMove(Collection<? extends Move<? super SolutionType>> moves, | ||
boolean requireImprovement, | ||
boolean acceptFirstImprovement, |
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.
the parameter acceptFirstImprovement
is never checked.
// track first improvement move AND best valid move + corresponding evaluation, validation and delta | ||
Move<? super SolutionType> bestMove = null, firstImprovementMove = null; | ||
double bestMoveDelta = -Double.MAX_VALUE, curMoveDelta, firstImprovementMoveDelta = -Double.MAX_VALUE; | ||
Evaluation curMoveEvaluation, bestMoveEvaluation = null, firstImprovementMoveEvaluation = null; |
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.
No need to store first and best move (and their metadata separately). You can have one series of variables for the chosen move, say chosenMove
, chosenMoveDelta
, etc. which will either be the first or best one depending on the parameter acceptFirstImprovement
.
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 work. I added some more suggestions.
@@ -7,6 +7,7 @@ | |||
<groupId>org.jamesframework</groupId> | |||
<artifactId>james</artifactId> | |||
<version>1.3-SNAPSHOT</version> | |||
<relativePath>../james/james</relativePath> |
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.
Why has this line been added? Should not be necessary.
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.
It is added to make the codes work on my computer, as I cloned James and James-core separately. I have added pom.xml to .gitignore. Please revise the pom.xml file accordingly.
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 see. I will remove this line when merging the pull request.
* @param <SolutionType> solution type of the problems that may be solved using this search, required to extend {@link Solution} | ||
* @author <a href="mailto:chenhuanfa@gmail.com">Huanfa Chen</a> | ||
*/ | ||
public class FirstBestAdmissibleTabuSearch<SolutionType extends Solution> extends TabuSearch<SolutionType>{ |
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.
Please also test this new Tabu 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.
Test added in the new pull request.
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.
Please don't create a new pull request for each commit. Just keep committing to the same branch, all changes will show up here. I closed the other pull request #43.
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 am not very familiar with Github. Say, I add and commit the changes, and push them to the remote repo. Will it be automatically committed to the same branch?
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.
If you don't specify an explicit branch when pushing, git
will push to the remote branch that has been associated with your local branch. So yes, these commits will end up in the same branch each time. In your case tabuSearch/add_variant
.
Are you using git
on command line or a desktop client?
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 am using git bash on Windows. Just add a new push.
|
||
|
||
@Test | ||
public void testGetBestMoveAcceptFirstImprovement() throws Exception { |
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.
Please move test method down (alongside other test methods instead of in between the setup code).
assertTrue(bestMove != null); | ||
assertTrue(bestMoveFirstImprovement != null); | ||
assertTrue(bestMove == bestMoveFirstImprovement); | ||
assertTrue(neighSearch.evaluate(bestMove).getValue() == neighSearch.evaluate(bestMoveFirstImprovement).getValue()); |
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.
Please avoid comparing double
values with ==
. Either use assertEquals(double expected, double actual, double delta)
or just remove this line because it can never fail if the previous line succeeds.
bestMoveFirstImprovement = neighSearch.getBestMove(moves, true,true); | ||
} | ||
|
||
// when curSolution is best, bestMove and bestMoveFirstImprovement should be the (nearly) the same |
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 test case! Why nearly the same? They should be exactly the same no? (which is what you test)
Could you perhaps think of a way to also explicitly test the other case, i.e. when the first and best improvement are not the same?
Please remove the following files and folders (IntelliJ/Eclipse settings) and include them in
Also see my other comments. |
You have several compiler warnings in your code. See https://travis-ci.org/hdbeukel/james-core/builds/238702467?utm_source=github_status&utm_medium=notification. Please fix. |
The undesired files listed above are also still present. |
Tests fail now because you have deleted the file |
Thanks for the fix. I will get back to this next week and merge your pull request (no time this week). |
I will probably make some more changes (e.g. Javadocs). If you want you can then pull them back into your fork. I will send you a pull request. |
Add a new getBestMove to NeighbourhoodSearch() with a parameter boolean acceptFirstImprovement.
Add FirstBestAdmissibleTabuSearch.java