Monday, November 5, 2012

Vacuum cleaner Agent ( Java Code )

A Vacuum Cleaner Agent implementation was the project made by my group as a mini-project which was a part of out AI lab work.
Better explanations of  Agents and their types will be available on other sources online. This post is made by me in order to share the code with the view that it can be useful for other students and can be made better by people sitting out there.

Without wasting much time.... here is the code....



import java.lang.Math.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;

/**
 *
 * @author TS
 */
public class vcai extends JFrame implements ActionListener {

    JTextField[][] b = new JTextField[10][10];
    JButton bt;
    //int count;
    int face;//1 u;2 d; 3 l; 4 r
    int i;
    int j;
    int moves;

 
 
    void move() {
        Random rn = new Random();
        int n = 4;
        int nface = Math.abs(rn.nextInt() % n);
     
                    System.out.println(nface);
        switch (face) {
            case 0:
                if (moves%5==0 || i + 1 > 9 || b[i + 1][j].getText().equalsIgnoreCase("o")) {
                    face = nface;
                    System.out.println(nface);
                } else {
                    b[i + 1][j].setText("A");
                    b[i][j].setText("c");
                    i = i + 1;
                }
                    moves=moves+1;
                break;
            case 1:
                if (moves%5==0 || i - 1 < 0 || b[i - 1][j].getText().equalsIgnoreCase("o")) {
                    face = nface;
                    System.out.println(nface);
                } else {
                    b[i - 1][j].setText("A");
                    b[i][j].setText("c");
                    i = i - 1;
                }
                    moves=moves+1;
                break;
            case 2:
                if (moves%5==0 || j - 1 < 0 || b[i][j - 1].getText().equalsIgnoreCase("o")) {
                    face = nface;
                    System.out.println(nface);
                } else {
                    b[i][j - 1].setText("A");
                    b[i][j].setText("c");
                    j = j - 1;
                }
                    moves=moves+1;
                break;
            case 3:
                if (moves%5==0 || j + 1 > 9 || b[i][j + 1].getText().equalsIgnoreCase("o")) {
                    face = nface;
                    System.out.println(nface);
                } else {
                    b[i][j + 1].setText("A");
                    b[i][j].setText("c");
                    j = j + 1;
                }
                    moves=moves+1;
                break;
        }
        if(moves%70==0) {
            JOptionPane.showMessageDialog(this, "step");
        }
    }

    //GroupLayout layout = new GroupLayout(getContentPane());
    //GroupLayout.Group[] g=new GroupLayout.Group[10];
    public vcai() {
        super("vacuum cleaner agent");
        bt = new JButton("Start");
        Container content = this.getContentPane();
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(11, 10));
        for (int i = 0; i < 10; i++) {
            //g[i]=layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup());
            for (int j = 0; j < 10; j++) {
                b[i][j] = new JTextField();
                //b[i][j].setLocation((i*10)+10, (j*10)+10);
                panel.add(b[i][j], j);
            }
        }
        bt.addActionListener(this);
        content.add(new JLabel("A: Agent ; o: obstacle ; *: dirt"), BorderLayout.PAGE_END);
        content.add(bt, BorderLayout.AFTER_LINE_ENDS);
        content.add(panel);
        //getContentPane().setLayout(layout);
        //  pack();
        this.setSize(360, 400);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    @Override
    public void actionPerformed(ActionEvent be) {
        if (be.getActionCommand().equals(bt.getLabel())) {
            //System.out.println("this");
            placeAgent();
            //slp(5000);
            face = 3;
            int l[] = new int[2];
            i = l[0];
            j = l[1];
            moves=1;
            while (true) {
                move();
            }
        }
    }

    void placeAgent() {
        outerloop:
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                if (b[i][j].getText().equalsIgnoreCase("")) {
                    b[i][j].setText("A");
                    break outerloop;
                }
            }
        }
    }


    int[] aloc() {
        int l[] = {0, 0};
        outerloop:
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                if (b[i][j].getText().equalsIgnoreCase("A")) {
                    l[0] = i;
                    l[1] = j;
                    break outerloop;
                }
            }
        }
        return l;
    }

    public static void main(String[] args) {
        new vcai().setVisible(true);
    }
}


The project was made using java swing. The screenshots are given below.



The main idea behind the agent is to make it traverse through the whole room. Better implementations may be available for solving situations like a "cave created by obstacles" and "linear arrangement of obstacles" , but the approach used here is fairly simple. 

APPROACH: A random direction is chosen every five moves. This will overcome the problem of the agent getting stuck in a corner inside the cave of obstacles. The "five" moves are selected because the room is 10*10. a halfway number generally gives better results. 
Other assumptions are that the "o" in the textbox is an obstacle, "*" is dirt, "A" is agent, "c" is the traversed area.

SHORTCOMINGS: The implementation does not show the stepwise movement of the agent. A messagebox is displayed  every 70 moves to see the area traversed in the interim. 
There is no better way of stopping the program, except for the "STOP" option in netbeans (yes... it is made in netbeans).

Above are a few of the many shortcomings of the implementation.

DO HELP IMPROVE THE CODE AND HAVE BETTER IMPLEMENTATIONS. 

I declare that you are free to use the code and make changes to it but, this should not be used in a commercial manner, ie to make profit.