Skip to content
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

Dev restart #950

Merged
merged 6 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions parsers/src/main/java/org/chocosolver/parser/xcsp/XCSP.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
package org.chocosolver.parser.xcsp;

import org.chocosolver.cutoffseq.LubyCutoffStrategy;
import org.chocosolver.solver.search.restart.LubyCutoff;
import org.chocosolver.parser.Level;
import org.chocosolver.parser.RegParser;
import org.chocosolver.solver.Model;
Expand Down Expand Up @@ -124,7 +124,7 @@ public void parse(Model target, XCSPParser parser, int i) throws Exception {
Solver solver = target.getSolver();
solver.setSearch(Search.defaultSearch(target));
solver.setNoGoodRecordingFromRestarts();
solver.setRestarts(count -> solver.getFailCount() >= count, new LubyCutoffStrategy(500), 5000);
solver.setRestarts(count -> solver.getFailCount() >= count, new LubyCutoff(500), 5000);
}
}

Expand Down
4 changes: 1 addition & 3 deletions solver/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
exports org.chocosolver.memory.trailing.trail.flatten;
exports org.chocosolver.memory.trailing.trail.chunck;
exports org.chocosolver.memory.structure;

exports org.chocosolver.cutoffseq;

exports org.chocosolver.sat;
exports org.chocosolver.solver;
exports org.chocosolver.solver.learn;
Expand Down Expand Up @@ -159,5 +158,4 @@
exports org.chocosolver.solver.constraints.nary.knapsack.structure;
opens org.chocosolver.solver.constraints.nary.knapsack.structure to org.chocosolver.parsers;


}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
package org.chocosolver.solver;

import org.chocosolver.cutoffseq.LubyCutoffStrategy;
import org.chocosolver.solver.search.restart.LubyCutoff;
import org.chocosolver.solver.constraints.Constraint;
import org.chocosolver.solver.constraints.nary.sat.NogoodStealer;
import org.chocosolver.solver.constraints.real.RealConstraint;
Expand Down Expand Up @@ -354,6 +354,7 @@ public List<Model> getModels() {
* @return a list that contained the found solutions.
*/
public Stream<Solution> streamSolutions() {
//noinspection Convert2Diamond
Spliterator<Solution> it = new Spliterator<Solution>() {

@Override
Expand Down Expand Up @@ -388,7 +389,6 @@ public int characteristics() {
/////////////////////////////////////// INTERNAL METHODS //////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

@SuppressWarnings("unchecked")
public void prepare() {
isPrepared = true;
check();
Expand Down Expand Up @@ -597,7 +597,7 @@ private void configureModel(int workerID) {
if (reliableness.containsKey(worker)) {
solver.plugMonitor(new NogoodFromRestarts(worker, manager));
}
solver.setRestarts(count -> solver.getFailCount() >= count, new LubyCutoffStrategy(500), 5000);
solver.setRestarts(count -> solver.getFailCount() >= count, new LubyCutoff(500), 5000);
break;
}
// complete with set default search
Expand Down
57 changes: 52 additions & 5 deletions solver/src/main/java/org/chocosolver/solver/Solver.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.chocosolver.solver.search.loop.propagate.PropagateBasic;
import org.chocosolver.solver.search.measure.IMeasures;
import org.chocosolver.solver.search.measure.MeasuresRecorder;
import org.chocosolver.solver.search.restart.AbstractRestart;
import org.chocosolver.solver.search.strategy.Search;
import org.chocosolver.solver.search.strategy.decision.Decision;
import org.chocosolver.solver.search.strategy.decision.DecisionPath;
Expand Down Expand Up @@ -225,6 +226,11 @@ public enum Action {
*/
private boolean canBeRepaired = true;

/**
* The restarting strategy
*/
private AbstractRestart restarter;

/**
* This object is accessible lazily
*/
Expand Down Expand Up @@ -265,6 +271,7 @@ protected Solver(Model aModel) {
setMove(new MoveBinaryDFS());
setPropagate(new PropagateBasic());
setNoLearning();
restarter = AbstractRestart.NO_RESTART;
}

public void throwsException(ICause c, Variable v, String s) throws ContradictionException {
Expand Down Expand Up @@ -445,6 +452,7 @@ protected boolean initialize() {
warmStart.setStrategy(declared);
setSearch(warmStart);
}
restarter.init();
if (!M.init()) { // the initialisation of the Move and strategy can detect inconsistency
mModel.getEnvironment().worldPop();
feasible = FALSE;
Expand Down Expand Up @@ -498,10 +506,11 @@ private void fixpoint() {
protected void extend() {
searchMonitors.beforeOpenNode();
mMeasures.incNodeCount();
if (!M.extend(this)) {
action = propagate;
if (restarter.mustRestart(this)) {
this.restart();
} else if (!M.extend(this)) {
action = validate;
} else {
action = propagate;
}
searchMonitors.afterOpenNode();
}
Expand All @@ -520,7 +529,12 @@ protected void repair() {
action = propagate;
}
searchMonitors.beforeUpBranch();
canBeRepaired = M.repair(this);
if (restarter.mustRestart(this)) {
canBeRepaired = true;
this.restart();
} else {
canBeRepaired = M.repair(this);
}
searchMonitors.afterUpBranch();
if (!canBeRepaired) {
stop = true;
Expand Down Expand Up @@ -652,7 +666,7 @@ public void hardReset() {
setNoLearning();
//no need to unplug, done by searchMonitors.reset()
this.lastSol = null;
if(this.warmStart != null) {
if (this.warmStart != null) {
this.warmStart.clearHints();
this.warmStart = null;
}
Expand Down Expand Up @@ -1126,6 +1140,38 @@ public void setPropagate(Propagate p) {
this.P = p;
}

/**
* Add or complete a restart policy.
* If no policy is defined, this method declares <i>restarter</i> as the current strategy.
* If a policy is defined, this method adds <i>restarter</i> as the next policy to check.
*
* @param restarter restarter policy
* @see #clearRestarter()
*/
public void addRestarter(AbstractRestart restarter) {
if (this.restarter == AbstractRestart.NO_RESTART) {
this.restarter = restarter;
} else {
this.restarter.setNext(restarter);
}
}

/**
* @return the current declared restart policy or {@link AbstractRestart#NO_RESTART}
*/
public AbstractRestart getRestarter() {
return this.restarter;
}

/**
* Clear the declared restart strategy.
* Consequently, no restarting will occur.
* @implNote replace the declared restart policy by {@link AbstractRestart#NO_RESTART}
*/
public void clearRestarter() {
this.restarter = AbstractRestart.NO_RESTART;
}

/**
* Declares an objective manager to use.
*
Expand Down Expand Up @@ -1487,6 +1533,7 @@ public Logger log() {
/**
* Defines whether (when {@code ansi} is set to {@code true}) or not
* ANSI tags are added to any trace from choco-solver.
*
* @param ansi {@code true} to enable colors
*/
public void logWithANSI(boolean ansi) {
Expand Down
Loading