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

aima.core.probability.proposition.IntegerSumProposition Maven / Gradle / Ivy

Go to download

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

There is a newer version: 3.0.0
Show newest version
package aima.core.probability.proposition;

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

import aima.core.probability.RandomVariable;
import aima.core.probability.domain.FiniteIntegerDomain;

public class IntegerSumProposition extends AbstractDerivedProposition {

	private FiniteIntegerDomain sumsDomain = null;
	private List sumVars = new ArrayList();
	//
	private String toString = null;

	public IntegerSumProposition(String name, FiniteIntegerDomain sumsDomain,
			RandomVariable... sums) {
		super(name);

		if (null == sumsDomain) {
			throw new IllegalArgumentException("Sum Domain must be specified.");
		}
		if (null == sums || 0 == sums.length) {
			throw new IllegalArgumentException(
					"Sum variables must be specified.");
		}
		this.sumsDomain = sumsDomain;
		for (RandomVariable rv : sums) {
			addScope(rv);
			sumVars.add(rv);
		}
	}

	//
	// START-Proposition
	public boolean holds(Map possibleWorld) {
		Integer sum = new Integer(0);

		for (RandomVariable rv : sumVars) {
			Object o = possibleWorld.get(rv);
			if (o instanceof Integer) {
				sum += ((Integer) o);
			} else {
				throw new IllegalArgumentException(
						"Possible World does not contain a Integer value for the sum variable:"
								+ rv);
			}
		}

		return sumsDomain.getPossibleValues().contains(sum);
	}

	// END-Proposition
	//

	@Override
	public String toString() {
		if (null == toString) {
			StringBuilder sb = new StringBuilder();
			sb.append(getDerivedName());
			sb.append(" = ");
			sb.append(sumsDomain.toString());
			toString = sb.toString();
		}
		return toString;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy