Skip to content

Commit

Permalink
Simple Search: max idle iterations during construction
Browse files Browse the repository at this point in the history
- added an ability to disable the max-idle limitation during the first part of the search, effectively setting up a minimal construction time
- parameter Search.MinConstructionTime, which can be in seconds or in percentage of the total time (Termination.TimeOut)
- defaults to 10% of the total time limit
  - so, if max-idle is enabled, it will not stop construction or IFS phase during the first 10% time of the solver run
  • Loading branch information
tomas-muller committed Nov 8, 2024
1 parent db4b876 commit 88e2efd
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/org/cpsolver/ifs/heuristics/MaxIdleNeighbourSelection.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.util.Collection;
import java.util.Map;

import org.apache.logging.log4j.Logger;
import org.cpsolver.ifs.algorithms.SimpleSearch;
import org.cpsolver.ifs.assignment.Assignment;
import org.cpsolver.ifs.assignment.context.AssignmentContext;
import org.cpsolver.ifs.assignment.context.NeighbourSelectionWithContext;
Expand Down Expand Up @@ -48,14 +50,33 @@
* @param <T> Value
**/
public class MaxIdleNeighbourSelection<V extends Variable<V, T>, T extends Value<V, T>> extends NeighbourSelectionWithContext<V, T, MaxIdleNeighbourSelection<V, T>.MaxIdleContext> implements SolutionListener<V, T> {
private Logger iLog = org.apache.logging.log4j.LogManager.getLogger(SimpleSearch.class);
protected NeighbourSelection<V, T> iParent = null;
protected int iMaxIdle = 1000;
protected int iBestAssigned = 0;
protected ConflictStatistics<V, T> iStat = null;
protected long iTimeOut = -1;


public MaxIdleNeighbourSelection(DataProperties properties, NeighbourSelection<V, T> parent, int maxIdle) {
iParent = parent;
iMaxIdle = maxIdle;
iTimeOut = -1;
try {
String idle = properties.getProperty("Search.MinConstructionTime", "10%");
if (idle != null && !idle.isEmpty()) {
if (idle.endsWith("%")) {
iTimeOut = Math.round(0.01 * Double.parseDouble(idle.substring(0, idle.length() - 1).trim()) *
properties.getPropertyLong("Termination.TimeOut", 0l));
} else {
iTimeOut = Long.parseLong(idle);
}
}
if (iTimeOut > 0)
iLog.debug("Minimal construction time is " + iTimeOut + " seconds.");
} catch (Exception e) {
iLog.warn("Failed to set the minimal construction time: " + e.getMessage());
}
}

@Override
Expand Down Expand Up @@ -108,6 +129,7 @@ public void bestRestored(Solution<V, T> solution) {

@Override
public Neighbour<V, T> selectNeighbour(Solution<V, T> solution) {
if (iTimeOut >= 0 && solution.getTime() < iTimeOut) return iParent.selectNeighbour(solution);
if (iMaxIdle < 0) return iParent.selectNeighbour(solution);
if (iMaxIdle == 0) return null;
MaxIdleContext context = getContext(solution.getAssignment());
Expand Down

0 comments on commit 88e2efd

Please sign in to comment.