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

aima.core.search.csp.SolutionStrategy 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;

import java.util.ArrayList;
import java.util.List;

/**
 * Base class for CSP solver implementations. Solving a CSP means finding an
 * assignment, which is consistent and complete with respect to a CSP. This
 * abstract class provides the central interface method and additionally an
 * implementation of an observer mechanism.
 * 
 * @author Ruediger Lunde
 * @author Mike Stampone
 */
public abstract class SolutionStrategy {
	List listeners = new ArrayList();

	/**
	 * Adds a CSP state listener to the solution strategy.
	 * 
	 * @param listener
	 *            a listener which follows the progress of the solution strategy
	 *            step-by-step.
	 */
	public void addCSPStateListener(CSPStateListener listener) {
		listeners.add(listener);
	}

	/**
	 * Removes a CSP listener from the solution strategy.
	 * 
	 * @param listener
	 *            the listener to remove
	 */
	public void removeCSPStateListener(CSPStateListener listener) {
		listeners.remove(listener);
	}

	protected void fireStateChanged(CSP csp) {
		for (CSPStateListener listener : listeners)
			listener.stateChanged(csp.copyDomains());
	}

	protected void fireStateChanged(Assignment assignment, CSP csp) {
		for (CSPStateListener listener : listeners)
			listener.stateChanged(assignment.copy(), csp.copyDomains());
	}

	/**
	 * Returns a solution to the specified CSP, which specifies values for all
	 * the variables such that the constraints are satisfied.
	 * 
	 * @param csp
	 *            a CSP to solve
	 * 
	 * @return a solution to the specified CSP, which specifies values for all
	 *         the variables such that the constraints are satisfied.
	 */
	public abstract Assignment solve(CSP csp);
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy