简单的解释一下我的程序。
gene.java 基因类:其中的getgene()方法表示生成一个染色体(二维数组gene[i][j]),其中第一个维度i你可以不用管他,我都设为1,表示只有一代染色体。也就是说,这个2维数组目前只是一个一维的向量。
main.java 类: 程序入口
maze.java 类:迷宫类。定义迷宫不能走的地方,isObstacle()表示那个地方是障碍,outOfMaze()就是超出了迷宫的范围。
mazeSolver.java 类: 解决方法类,也是算法的核心。解释一下 move() 因为gene[][]的值是0,1,2,3这4个数中的随机一个。 如果是0,我定义它为向上,也就是纵坐标+1。后面类似。
目前的问题是,第一个值运行的很好,如果nextPoint是障碍或者超出迷宫,currentPoint会保持不变,然后计算下一个nextPoint,直到nextPoint为一个合法的值,才会执行currentPoint = nextPoint
但是之后就乱套了,不管是不是障碍,不管是不是超出迷宫,程序都会执行 currentPoint = nextPoint, 不知道为什么。
gene.java/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gam;import java.util.Random;/**
 *
 * @author panyunpeng
 */
public class Gene {    /**
     * 
     * @param x number of chromosome
     * @return one generation of chromosome
     */
    Maze maze;    public Gene(Maze maze) {
        this.maze = maze;
    }    /**
     * 
     * @param x max number of chromosomes
     * @return
     */
    
    /*
     * generate gene
     */
    public int[][] getGene(int x) {        int maxNumberOfChromosome = x;//你可以不用管他,目前我都设为1
        int gene[][] = new int[maxNumberOfChromosome][maze.width * maze.height];//基因的长度是迷宫的长*宽
        int i, j;
        Random k = new Random();
        for (i = 0; i < maxNumberOfChromosome; i++) {//i number of chromosomes
            for (j = 0; j < maze.width * maze.height; j++) { //j number of alleles
                int u = k.nextInt(4);//random from 0 to 3(int)
                gene[i][j] = u;
            }
        }
        return gene;
    }
}main.java/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gam;import java.awt.Point;/**
 *
 * @author panyunpeng
 */
public class Main {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {        Point startPoint = new Point(0,0);
        Point endPoint = new Point(12,12);
        Maze myMaze = new Maze(13,13);
        Gene myGene = new Gene(myMaze);
        MazeSolver ms = new MazeSolver(myGene, myMaze);
        ms.findWayOut(startPoint, endPoint);
        System.out.println("MaxDistance:" + new Point(0,0).distance(new Point(12,12)));//忽略它    }
}Maze.java/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gam;import java.awt.Point;/**
 *
 * @author panyunpeng
 */
public class Main {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {        Point startPoint = new Point(0,0);
        Point endPoint = new Point(12,12);
        Maze myMaze = new Maze(13,13);
        Gene myGene = new Gene(myMaze);
        MazeSolver ms = new MazeSolver(myGene, myMaze);
        ms.findWayOut(startPoint, endPoint);
        System.out.println("MaxDistance:" + new Point(0,0).distance(new Point(12,12)));//忽略它    }
}
MazeSolver.java/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gam;import java.awt.Point;/**
 *
 * @author panyunpeng
 */
public class MazeSolver {    Gene myGene;
    Maze myMaze;
    double fitness;//适应度,你可以忽略它
    Point currentPoint = new Point(0,0);
    Point nextPoint = new Point(0,0);
    int numberOfChromosome = 1;
    int currentChromosome;//忽略
    int currentAllele;//就是基因的第二维。
    int[][] gene;    public MazeSolver(Gene g, Maze m) {
        this.myGene = g;
        this.myMaze = m;
    }    public void findWayOut(Point startPoint, Point endPoint) {
//        currentPoint = new Point(0,0);
//        nextPoint = new Point(0,0);
        System.out.println("Start from here: "+currentPoint);
        System.out.println("Initial next point: "+nextPoint);
        gene = myGene.getGene(numberOfChromosome);
        myMaze.init();
        for (int i = 0; i < numberOfChromosome; i++) {
            currentChromosome = i;
            for (int j = 0; j < myMaze.height * myMaze.width; j++) {
                currentAllele = j;
                this.move();
                if(myMaze.isObstacle(nextPoint)){
                 System.out.println("Error:Obstacle!" + nextPoint + "We still at the point: " +currentPoint);
                }
                else if(myMaze.outOfMaze(nextPoint)){
                 System.out.println("Error:Out of maze!" + nextPoint + "We still at the point: " + currentPoint);
                }
                else{
                 System.out.println("Moved!");
                 currentPoint = nextPoint;
                 System.out.println("Now we are at the new point: " + currentPoint);
                }
            }
        }
    }    public void move() {        int i = currentChromosome;
        int j = currentAllele;
        if (gene[i][j] == 0) { //up
            nextPoint.x = currentPoint.x;
            nextPoint.y = currentPoint.y + 1;
            System.out.println("Next point calculated:" +nextPoint+"Current chromosome: " + currentChromosome+ "Current point:"+currentPoint);
        } else if (gene[i][j] == 1) {//down
            nextPoint.x = currentPoint.x;
            nextPoint.y = currentPoint.y - 1;
            System.out.println("Next point calculated"+nextPoint+"Current chromosome: " + currentChromosome+ "Current point:"+currentPoint);
        } else if (gene[i][j] == 2) {//left
            nextPoint.x = currentPoint.x - 1;
            nextPoint.y = currentPoint.y;
            System.out.println("Next point calculated"+nextPoint+"Current chromosome: " + currentChromosome+ "Current point:"+currentPoint);
        } else //right
        {
            nextPoint.x = currentPoint.x + 1;
            nextPoint.y = currentPoint.y;
            System.out.println("Next point calculated"+nextPoint+"Current chromosome: " + currentChromosome+ "Current point:"+currentPoint);
        }    }
}

解决方案 »

  1.   


    你看下你发的Maze.java就知道了
      

  2.   

    你发的Maze.java
    里面是Main.java的内容
      

  3.   

    对不起各位,我把maze.java再发一次。Maze.java
    ----------package gam;import java.awt.Point;
    import java.util.ArrayList;
    import java.util.List;public class Maze { int height;
    int width; public Maze(int height, int width) {
    this.height = height;
    this.width = width;
    } List<Point> obstacleList = new ArrayList<Point>(); /*
     * Discribe a maze
     */
    public void init() {
    obstacleList.add(new Point(0, 7));
    obstacleList.add(new Point(1, 1));
    obstacleList.add(new Point(1, 3));
    obstacleList.add(new Point(1, 4));
    obstacleList.add(new Point(1, 5));
    obstacleList.add(new Point(1, 7));
    obstacleList.add(new Point(1, 9));
    obstacleList.add(new Point(1, 10));
    obstacleList.add(new Point(1, 11));
    obstacleList.add(new Point(2, 1));
    obstacleList.add(new Point(2, 7));
    obstacleList.add(new Point(2, 9));
    obstacleList.add(new Point(3, 1));
    obstacleList.add(new Point(3, 4));
    obstacleList.add(new Point(3, 5));
    obstacleList.add(new Point(3, 7));
    obstacleList.add(new Point(3, 9));
    obstacleList.add(new Point(3, 11));
    obstacleList.add(new Point(4, 5));
    obstacleList.add(new Point(4, 9));
    obstacleList.add(new Point(4, 11));
    obstacleList.add(new Point(5, 1));
    obstacleList.add(new Point(5, 2));
    obstacleList.add(new Point(5, 3));
    obstacleList.add(new Point(5, 5));
    obstacleList.add(new Point(5, 6));
    obstacleList.add(new Point(5, 7));
    obstacleList.add(new Point(5, 8));
    obstacleList.add(new Point(5, 9));
    obstacleList.add(new Point(5, 11));
    obstacleList.add(new Point(6, 1));
    obstacleList.add(new Point(6, 7));
    obstacleList.add(new Point(7, 1));
    obstacleList.add(new Point(7, 3));
    obstacleList.add(new Point(7, 4));
    obstacleList.add(new Point(7, 5));
    obstacleList.add(new Point(7, 6));
    obstacleList.add(new Point(7, 7));
    obstacleList.add(new Point(7, 9));
    obstacleList.add(new Point(7, 10));
    obstacleList.add(new Point(7, 11));
    obstacleList.add(new Point(7, 12));
    obstacleList.add(new Point(8, 1));
    obstacleList.add(new Point(8, 7));
    obstacleList.add(new Point(9, 1));
    obstacleList.add(new Point(9, 2));
    obstacleList.add(new Point(9, 3));
    obstacleList.add(new Point(9, 4));
    obstacleList.add(new Point(9, 5));
    obstacleList.add(new Point(9, 7));
    obstacleList.add(new Point(9, 9));
    obstacleList.add(new Point(9, 10));
    obstacleList.add(new Point(9, 11));
    obstacleList.add(new Point(10, 1));
    obstacleList.add(new Point(10, 5));
    obstacleList.add(new Point(10, 9));
    obstacleList.add(new Point(11, 1));
    obstacleList.add(new Point(11, 3));
    obstacleList.add(new Point(11, 5));
    obstacleList.add(new Point(11, 6));
    obstacleList.add(new Point(11, 7));
    obstacleList.add(new Point(11, 8));
    obstacleList.add(new Point(11, 9));
    obstacleList.add(new Point(11, 11));
    obstacleList.add(new Point(11, 12));
    obstacleList.add(new Point(12, 3));
    obstacleList.add(new Point(12, 5)); } public boolean isObstacle(Point P) {
    return obstacleList.contains(P);
    } public boolean outOfMaze(Point p) {
    if ((p.x < 0) || (p.y < 0)) {
    return true;

    else if((p.x > 12) || (p.y > 12)) {
    return true;
    }
    else return false;
    }}
      

  4.   

    问题关键:
    MazeSolver.java中
    这一句:
    currentPoint = nextPoint;
    所以在一次成功的moved之后,
    你的currentPoint和nextPoint的引用变成了同一个对象你之后做move()方法的时候
    nextPoint.x = currentPoint.x;
    nextPoint.y = currentPoint.y - 1;
    这样的语句,实际上就是改变了currentPoint本身的值了
    在move()的时候,你就已经让currentPoint超出迷宫了所以你的这句
    currentPoint = nextPoint;
    要改成
    currentPoint.x = nextPoint.x;
    currentPoint.y = nextPoint.y;