当前位置: 首页>>代码示例>>Java>>正文


Java IMonitorSolution类代码示例

本文整理汇总了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);
}
 
开发者ID:chocoteam,项目名称:choco-graph,代码行数:52,代码来源:DCMST.java


注:本文中的org.chocosolver.solver.search.loop.monitors.IMonitorSolution类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。