Maximum Density Still Life:search

From CometPublic

Jump to: navigation, search

This is the complete search implementation for the Maximum Density Still Life problem.

include "LocalSolver";
include "LifeGrid";
include "selectBest";

int h = 10; int w = h;
range Height = 1..h;
range Width = 1..w;

LifeGrid lg(h, w, false);
LocalSolver m = lg.getLocalSolver();
ConstraintSystem S = lg.getConstraintSystem();
var{int}[,] alive = lg.getGrid();

var{int} obj(m) <- max(2, w * h / 200) * lg.violations() - lg.getNumAlive();

lg.close();

int tabu[Height, Width] = 0;
Counter it(m, 0);
forall (i in Height, j in Width)
	whenever alive[i, j]@changes(int o, int n) {
		tabu[i, j] = it + 8;
		it++;
	}

Closure step = closure {
	if (S.violations() == 0)
		selectMin(i in Height, j in Width : tabu[i, j] <= it && alive[i, j] == 0)
			(S.getAssignDelta(alive[i, j], 1))
			alive[i, j] := 1;
	else
		selectMax(i in Height, j in Width : tabu[i, j] <= it)
			(S.violations(alive[i, j]))
			alive[i, j] := 1 - alive[i, j];
};

int maxit = max(h * w, 100);
forall (i in 1..maxit) {
	it := 0;
	forall (i in Height, j in Width)
 		tabu[i, j] = 0;
	selectBest(m, 100, obj, step);
}

lg.prettyPrint();