本文整理汇总了Java中org.chocosolver.solver.search.loop.monitors.IMonitorSolution类的典型用法代码示例。如果您正苦于以下问题:Java IMonitorSolution类的具体用法?Java IMonitorSolution怎么用?Java IMonitorSolution使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
IMonitorSolution类属于org.chocosolver.solver.search.loop.monitors包,在下文中一共展示了IMonitorSolution类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: solve
import org.chocosolver.solver.search.loop.monitors.IMonitorSolution; //导入依赖的package包/类
public void solve() {
GraphModel model = new GraphModel();
IntVar totalCost = model.intVar("obj", lb, ub, true);
// graph var domain
UndirectedGraph GLB = new UndirectedGraph(model,n,SetType.LINKED_LIST,true);
UndirectedGraph GUB = new UndirectedGraph(model,n,SetType.BIPARTITESET,true);
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (dist[i][j] != -1 && !(dMax[i] == 1 && dMax[j] == 1)) {
GUB.addEdge(i, j); // possible edge
}
}
}
UndirectedGraphVar graph = model.graphVar("G", GLB, GUB);
IntVar[]degrees = model.degrees(graph);
for (int i = 0; i < n; i++) {
model.arithm(degrees[i], "<=", dMax[i]).post();
}
// degree constrained-minimum spanning tree constraint
model.dcmst(graph,degrees,totalCost,dist,2).post();
final GraphSearch mainSearch = new GraphSearch(graph, dist);
// find the first solution by selecting cheap edges
mainSearch.configure(GraphSearch.MIN_COST);
Solver s = model.getSolver();
// then select the most expensive ones (fail first principle, with last conflict)
s.plugMonitor((IMonitorSolution) () -> {
mainSearch.useLastConflict();
mainSearch.configure(GraphSearch.MIN_P_DEGREE);
System.out.println("Solution found : "+totalCost);
});
// bottom-up optimization : find a first solution then reach the global minimum from below
s.setSearch(new ObjectiveStrategy(totalCost, OptimizationPolicy.BOTTOM_UP), mainSearch);
s.limitSolution(2); // therefore there is at most two solutions
long TIMELIMIT = 300000;
s.limitTime(TIMELIMIT); // time limit
// find optimum
model.setObjective(Model.MINIMIZE,totalCost);
while (s.solve()){
System.out.println(totalCost);
}
if (s.getSolutionCount() == 0 && s.getTimeCount() < TIMELIMIT/1000) {
throw new UnsupportedOperationException("Provided instances are feasible!");
}
String output = instance+";"+s.getSolutionCount()+";"+s.getBestSolutionValue()+";"
+s.getNodeCount()+";"+s.getFailCount()+";"+s.getTimeCount()+";\n";
write(output,OUT_PUT_FILE,false);
}