All Downloads are FREE. Search and download functionalities are using the official Maven repository.

aima.core.search.csp.examples.NQueensCSP Maven / Gradle / Ivy

Go to download

AIMA-Java Core Algorithms from the book Artificial Intelligence a Modern Approach 3rd Ed.

The newest version!
package aima.core.search.csp.examples;

import aima.core.search.csp.CSP;
import aima.core.search.csp.Domain;
import aima.core.search.csp.Variable;

public class NQueensCSP extends CSP {

	public NQueensCSP(int size) {
		for (int i = 0; i < size; i++)
			addVariable(new Variable("Q" + (i+1)));
		
		Integer[] values = new Integer[size];
		for (int i = 0; i < size; i++)
			values[i] = i+1;
		Domain positions = new Domain(values);

		for (Variable var : getVariables())
			setDomain(var, positions);

		for (int i = 0; i < size; i++) {
			Variable var1 = getVariables().get(i);
			for (int j = i+1; j < size; j++) {
				Variable var2 = getVariables().get(j);
				addConstraint(new DiffNotEqualConstraint(var1, var2, 0));
				addConstraint(new DiffNotEqualConstraint(var1, var2, j-i));
			}
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy