Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
/*I am trying to design a solution to 8-puzzle problem in artificial intelligence using state-space tree.If nodes with same fvalue are generated then which should be removed from the queue first,the one with the lesser gval or the one with the higher gvalue or there is any other criterion for precedence in such case.*/

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Scanner;

public class eightpuzzle
{
	public static void main(String[] args)
	{
		Scanner sc=new Scanner(System.in);
		System.out.println("Enter the initial config.\n");
		node initial=new node();
		for(i=0;i<3;i++)
			for(j=0;j<3;j++)
			initial.tiles[i][j]=sc.nextInt();
		System.out.println("Enter the position of blank tile\n");
		initial.x=sc.nextInt();
		initial.y=sc.nextInt();
		//System.out.println(initial);
		initial.gval=0;
		initial.hval=mh(initial);
		//System.out.println(initial.hval+"\n\n");
		initial.fval=initial.hval;
		open.add(initial);
		node gen;
		while(!open.isEmpty() && i<7)
		{
			cur=open.remove();
			System.out.println("Removed"+cur);
			if(mh(cur)==0)
			{
				System.out.println("Solution found");
				break;
			}
			closed.add(cur);
			
			gen=left(cur);
			if(check(gen) && !open.contains(gen))
			{
				gen.hval=mh(gen);
				gen.fval=gen.gval+gen.hval;
				open.add(gen);
				System.out.println(gen);
				System.out.println(gen.fval);
			}
				
			gen=right(cur);
			if(check(gen) && !open.contains(gen))
			{
				gen.hval=mh(gen);
				gen.fval=gen.gval+gen.hval;
				open.add(gen);
				System.out.println(gen);
				System.out.println(gen.fval);
			}
			
			gen=up(cur);
			if(check(gen) && !open.contains(gen))
			{
				gen.hval=mh(gen);
				gen.fval=gen.gval+gen.hval;
				open.add(gen);
				System.out.println(gen);
				System.out.println(gen.fval);
			}
			
			gen=down(cur);
			if(check(gen) && !open.contains(gen))
			{
				gen.hval=mh(gen);
				gen.fval=gen.gval+gen.hval;
				open.add(gen);
				System.out.println(gen);
				System.out.println(gen.fval);
			}
			i++;
			System.out.println("Queue is\n");
			for(node j:open)
				System.out.println(j);
		}
		/*i=0;
		while(cur.parent!=null)
		{
			System.out.println(cur);
			cur=cur.parent;
			i++;
		}
		if(i>0)
			System.out.println(cur);*/
	}
	
	static boolean check(node nd)
	{
		if(nd!=null && !closed.contains(nd) )
			return true;
		else
			return false;
	}
	
	static node left(node nd)
	{
		if(nd.y!=0)
		{
		node newNode=new node(nd);
		int temp=newNode.tiles[nd.x][nd.y-1];
		newNode.tiles[nd.x][nd.y-1]=newNode.tiles[nd.x][nd.y];
		newNode.tiles[nd.x][nd.y]=temp;
		newNode.y=nd.y-1;
		newNode.gval=nd.gval+1;
		return newNode;
		}
		return null;
	}
	
	static node up(node nd)
	{
		if(nd.x!=0)
		{
		node newNode=new node(nd);
		int temp=newNode.tiles[nd.x-1][nd.y];
		newNode.tiles[nd.x-1][nd.y]=newNode.tiles[nd.x][nd.y];
		newNode.tiles[nd.x][nd.y]=temp;
		newNode.x=nd.x-1;
		newNode.gval=nd.gval+1;
		return newNode;
		}
		return null;
	}
	
	static node right(node nd)
	{
		if(nd.y!=2)
		{
		node newNode=new node(nd);
		int temp=newNode.tiles[nd.x][nd.y+1];
		newNode.tiles[nd.x][nd.y+1]=newNode.tiles[nd.x][nd.y];
		newNode.tiles[nd.x][nd.y]=temp;
		newNode.y=nd.y+1;
		newNode.gval=nd.gval+1;
		return newNode;
		}
		return null;
	}
	
	static node down(node nd)
	{
		if(nd.x!=2)
		{
		node newNode=new node(nd);
		int temp=newNode.tiles[nd.x+1][nd.y];
		newNode.tiles[nd.x+1][nd.y]=newNode.tiles[nd.x][nd.y];
		newNode.tiles[nd.x][nd.y]=temp;
		newNode.x=nd.x+1;
		newNode.gval=nd.gval+1;
		return newNode;
		}
		return null;
	}

	
	static int mh(node nd)
	{
		int i,j;
		int count=0;
		for(i=0;i<3;i++)
		{
			for(j=0;j<3;j++)
			{
				switch(nd.tiles[i][j])
				{
				case 1:
					count+=Math.abs(i-0)+Math.abs(j-0);
					break;
					
				case 2:
					count+=Math.abs(i-0)+Math.abs(j-1);
					break;
					
				case 3:
					count+=Math.abs(i-0)+Math.abs(j-2);
					break;
					
				case 4:
					count+=Math.abs(i-1)+Math.abs(j-0);
					break;
					
				case 5:
					count+=Math.abs(i-1)+Math.abs(j-1);
					break;
					
				case 6:
					count+=Math.abs(i-1)+Math.abs(j-2);
					break;
					
				case 7:
					count+=Math.abs(i-2)+Math.abs(j-0);
					break;
					
				case 8:
					count+=Math.abs(i-2)+Math.abs(j-1);
					break;
					
				case -1:
					break;
				}
			}
		}
		return count;
	}
	
	
	static Comparator<node> comp=new IntegerComparator();
	static PriorityQueue<node> open=new PriorityQueue<node>(50,comp);
	static ArrayList<node> closed=new ArrayList<node>();
	static int i,j,tent_gval;
	static node cur=new node();
}

class IntegerComparator implements Comparator<node>
{
	public int compare(node x,node y)
	{
		if(x.fval<y.fval)
			return -1;
		if(x.fval>y.fval)
			return 1;
		return 0;
	}
}

class node
{
	public int fval,gval,hval;
	public node parent;
	public int tiles[][]=new int[3][3];
	public int x,y;
	
	public node()
	{
		for(int i=0;i<3;i++)
			for(int j=0;j<3;j++)
				tiles[i][j]=0;
		parent=null;
	}
	
	public node(node nd)
	{
		for(int i=0;i<3;i++)
			for(int j=0;j<3;j++)
				this.tiles[i][j]=nd.tiles[i][j];
		this.parent=nd;
		this.x=nd.x;
		this.y=nd.y;
	}
	
	public boolean equals(Object nd)
	{
		node nod=(node)nd;
		boolean flag=true;
		int i,j;
		for(i=0;i<3;i++)
			for(j=0;j<3;j++)
			{
				if(nod.tiles[i][j]!=tiles[i][j])
				{
					flag=false;
					break;
				}
			}
		return flag;
	}
	
	public String toString()
	{
		return(tiles[0][0]+" "+tiles[0][1]+" "+tiles[0][2]+"\n"+tiles[1][0]+" "+tiles[1][1]+" "+tiles[1][2]+"\n"+
				tiles[2][0]+" "+tiles[2][1]+" "+tiles[2][2]+"\n");
	}
}
Posted
Updated 24-Jan-14 20:41pm
v2

1 solution

In real life, when we are faced with two equally promising options, what do we do? Flip a coin! Yes, leave it to probability.
For example, node m and node n have the same f value, we generate a random number between 0 and 1, if the number is below 0.5, choose node m else node n. Say, node m has been chosen for being lucky. If the f value of the subsequent node originated from node m is worse than that of node n, node n will take over according to A* algorithm.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900