CSP

  1. ดาวโหลด aima-java จาก http://code.google.com/p/aima-java/
  2. นำ aima-java เข้าสู่ eclipse
  3. เพิ่ม source code ด้านล้างเข้าไปใน project
  4. เปลี่ยนแผนที่จาก A, B, C, D เป็นแผนที่กรุงเทพฯ และจังหวัดรอบๆ แล้วลองรัน โปรแกรมดูครับ

ส่วนที่ต้องแก้ไขหลักๆ คือ variables และ neighbors โดย variables ในกรณีนี้เปลี่ยนเป็นชื่อจังหวัด และ neighbors ใส่ว่าจังหวัดใดติดกันบ้าง โดยต้องใส่ทั้งสองทาง เช่น กรุงเทพฯติดกับนนทบรี และ นนทบุรีติดกับกรุงเทพฯ

package aima.search.csp;

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

//Copyright (c) <2003-2008> 
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.

public class ModMapCsp extends CSP {
	public static String WA = "WA";

	public static String NT = "NT";

	public static String SA = "SA";

	public static String Q = "Q";

	public static String NSW = "NSW";

	public static String V = "V";

	public static String T = "T";

	public static String RED = "RED";

	public static String BLUE = "BLUE";

	public static String GREEN = "GREEN";

	private ModMapCsp(List variables, Constraint constraints) {
		super(variables, constraints);

	}

	public static CSP getMap() {
		List variables = new ArrayList();
		variables.add("A");
		variables.add("B");
		variables.add("C");
		variables.add("D");

		List colors = new ArrayList();
		colors.add(RED);
		colors.add(BLUE);
		colors.add(GREEN);

		Domain domains = new Domain(variables);
		for (int i = 0; i < variables.size(); i++) {
			String variable = variables.get(i);
			domains.addToDomain(variable, colors);
		}

		Hashtable<tring, List> neighbors = new Hashtable<string , List>();
		addToNeighbors(neighbors, "A", "B", "C", "D");
		addToNeighbors(neighbors, "B", "A", "C");
		addToNeighbors(neighbors, "C", "A", "B");
		addToNeighbors(neighbors, "D", "A");

		Constraint mapConstraints = new MapColoringConstraint(neighbors);

		return new CSP(variables, mapConstraints, domains);
	}

	public static void addToNeighbors(
			Hashtable<string , List> neighbors, String whose) {
		List l = new ArrayList();
		neighbors.put(whose, l);
	}

	public static void addToNeighbors(
			Hashtable<string , List> neighbors, String whose, String one) {
		List l = new ArrayList();
		l.add(one);
		neighbors.put(whose, l);
	}

	public static void addToNeighbors(
			Hashtable<string , List> neighbors, String whose,
			String one, String two) {
		List l = new ArrayList();
		l.add(one);
		l.add(two);
		neighbors.put(whose, l);
	}

	public static void addToNeighbors(
			Hashtable<string , List> neighbors, String whose,
			String one, String two, String three) {
		List l = new ArrayList();
		l.add(one);
		l.add(two);
		l.add(three);
		neighbors.put(whose, l);
	}

	public static void addToNeighbors(
			Hashtable<string , List> neighbors, String whose,
			String one, String two, String three, String four) {
		List l = new ArrayList();
		l.add(one);
		l.add(two);
		l.add(three);
		l.add(four);
		neighbors.put(whose, l);
	}

	public static void addToNeighbors(
			Hashtable<string , List> neighbors, String whose,
			String one, String two, String three, String four, String five) {
		List l = new ArrayList();
		l.add(one);
		l.add(two);
		l.add(three);
		l.add(four);
		l.add(five);
		neighbors.put(whose, l);
	}

	public static void main(String args[]) {
		System.out.println("!!!");
		CSP csp = ModMapCsp.getMap();
		Assignment result = csp.backTrackingSearch();
		System.out.println(result.getAssignment("A"));
		System.out.println(result.getAssignment("B"));
		System.out.println(result.getAssignment("C"));
		System.out.println(result.getAssignment("D"));
	}
}

ใส่ความเห็น

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / เปลี่ยนแปลง )

Twitter picture

You are commenting using your Twitter account. Log Out / เปลี่ยนแปลง )

Facebook photo

You are commenting using your Facebook account. Log Out / เปลี่ยนแปลง )

Google+ photo

You are commenting using your Google+ account. Log Out / เปลี่ยนแปลง )

Connecting to %s