java迷宫move类意思

java迷宫move类意思,第1张

这个没啥含义啊,一个对象类

private int x;

private int y;

两个属性,从字面意义上说x是横坐标,y是纵坐标。

public Move(){

}无参数的构造方法

public Move(int x, int y){

thisx = x;

thisy = y;

}有参数的构造方法

下面那些get方法时用来访问对象属性的,set方法是给对象属性赋值的。

给你举个例子你就知道了

Move m1=new Move();这里声明了一个Move类的对象,调用的是无参数的构造方法,也就是说m1的x和y都是没有赋值的。但是呢,int类型默认值是0;

然后你可以通过set方法去给属性赋值m1setX(2),然后通过get方法取值m1getX()这个时候就能取到2了。

而有参数的构造方法就是在声明对象的时候直接赋值给元素

Move m2=new Move(1,2);这个时候你再用get方法去取,就会有x=1,y=2了,就是省掉了用set方法赋值的一步。

你还是先看看什么是对象,什么是构造函数,还有get,set方法的作用吧

通过遗传算法走迷宫。虽然图1和图2均成功走出迷宫,但是图1比图2的路径长的多,且复杂,遗传算法可以计算出有多少种可能性,并选择其中最简洁的作为运算结果。

示例图1:

示例图2:

实现代码:

import javautilArrayList;

import javautilCollections;

import javautilIterator;

import javautilLinkedList;

import javautilList;

import javautilRandom;

/

用遗传算法走迷宫

@author Orisun

/

public class GA {

int gene_len; // 基因长度

int chrom_len; // 染色体长度

int population; // 种群大小

double cross_ratio; // 交叉率

double muta_ratio; // 变异率

int iter_limit; // 最多进化的代数

List<boolean[]> individuals; // 存储当代种群的染色体

Labyrinth labyrinth;

int width;      //迷宫一行有多少个格子

int height;     //迷宫有多少行

public class BI {

double fitness;

boolean[] indv;

public BI(double f, boolean[] ind) {

fitness = f;

indv = ind;

}

public double getFitness() {

return fitness;

}

public boolean[] getIndv() {

return indv;

}

}

List<BI> best_individual; // 存储每一代中最优秀的个体

public GA(Labyrinth labyrinth) {

thislabyrinth=labyrinth;

thiswidth = labyrinthmap[0]length;

thisheight = labyrinthmaplength;

chrom_len = 4  (width+height);

gene_len = 2;

population = 20;

cross_ratio = 083;

muta_ratio = 0002;

iter_limit = 300;

individuals = new ArrayList<boolean[]>(population);

best_individual = new ArrayList<BI>(iter_limit);

}

public int getWidth() {

return width;

}

public void setWidth(int width) {

thiswidth = width;

}

public double getCross_ratio() {

return cross_ratio;

}

public List<BI> getBest_individual() {

return best_individual;

}

public Labyrinth getLabyrinth() {

return labyrinth;

}

public void setLabyrinth(Labyrinth labyrinth) {

thislabyrinth = labyrinth;

}

public void setChrom_len(int chrom_len) {

thischrom_len = chrom_len;

}

public void setPopulation(int population) {

thispopulation = population;

}

public void setCross_ratio(double cross_ratio) {

thiscross_ratio = cross_ratio;

}

public void setMuta_ratio(double muta_ratio) {

thismuta_ratio = muta_ratio;

}

public void setIter_limit(int iter_limit) {

thisiter_limit = iter_limit;

}

// 初始化种群

public void initPopulation() {

Random r = new Random(SystemcurrentTimeMillis());

for (int i = 0; i < population; i++) {

int len = gene_len chrom_len;

boolean[] ind = new boolean[len];

for (int j = 0; j < len; j++)

ind[j] = rnextBoolean();

individualsadd(ind);

}

}

// 交叉

public void cross(boolean[] arr1, boolean[] arr2) {

Random r = new Random(SystemcurrentTimeMillis());

int length = arr1length;

int slice = 0;

do {

slice = rnextInt(length);

} while (slice == 0);

if (slice < length / 2) {

for (int i = 0; i < slice; i++) {

boolean tmp = arr1[i];

arr1[i] = arr2[i];

arr2[i] = tmp;

}

} else {

for (int i = slice; i < length; i++) {

boolean tmp = arr1[i];

arr1[i] = arr2[i];

arr2[i] = tmp;

}

}

}

// 变异

public void mutation(boolean[] individual) {

int length = individuallength;

Random r = new Random(SystemcurrentTimeMillis());

individual[rnextInt(length)] ^= false;

}

// 轮盘法选择下一代,并返回当代最高的适应度值

public double selection() {

boolean[][] next_generation = new boolean[population][]; // 下一代

int length = gene_len chrom_len;

for (int i = 0; i < population; i++)

next_generation[i] = new boolean[length];

double[] cumulation = new double[population];

int best_index = 0;

double max_fitness = getFitness(individualsget(best_index));

cumulation[0] = max_fitness;

for (int i = 1; i < population; i++) {

double fit = getFitness(individualsget(i));

cumulation[i] = cumulation[i - 1] + fit;

// 寻找当代的最优个体

if (fit > max_fitness) {

best_index = i;

max_fitness = fit;

}

}

Random rand = new Random(SystemcurrentTimeMillis());

for (int i = 0; i < population; i++)

next_generation[i] = individualsget(findByHalf(cumulation,

randnextDouble() cumulation[population - 1]));

// 把当代的最优个体及其适应度放到best_individual中

BI bi = new BI(max_fitness, individualsget(best_index));

// printPath(individualsget(best_index));

//Systemoutprintln(max_fitness);

best_individualadd(bi);

// 新一代作为当前代

for (int i = 0; i < population; i++)

individualsset(i, next_generation[i]);

return max_fitness;

}

// 折半查找

public int findByHalf(double[] arr, double find) {

if (find < 0 || find == 0 || find > arr[arrlength - 1])

return -1;

int min = 0;

int max = arrlength - 1;

int medium = min;

do {

if (medium == (min + max) / 2)

break;

medium = (min + max) / 2;

if (arr[medium] < find)

min = medium;

else if (arr[medium] > find)

max = medium;

else

return medium;

} while (min < max);

return max;

}

// 计算适应度

public double getFitness(boolean[] individual) {

int length = individuallength;

// 记录当前的位置,入口点是(1,0)

int x = 1;

int y = 0;

// 根据染色体中基因的指导向前走

for (int i = 0; i < length; i++) {

boolean b1 = individual[i];

boolean b2 = individual[++i];

// 00向左走

if (b1 == false && b2 == false) {

if (x > 0 && labyrinthmap[y][x - 1] == true) {

x--;

}

}

// 01向右走

else if (b1 == false && b2 == true) {

if (x + 1 < width && labyrinthmap[y][x + 1] == true) {

x++;

}

}

// 10向上走

else if (b1 == true && b2 == false) {

if (y > 0 && labyrinthmap[y - 1][x] == true) {

y--;

}

}

// 11向下走

else if (b1 == true && b2 == true) {

if (y + 1 < height && labyrinthmap[y + 1][x] == true) {

y++;

}

}

}

int n = Mathabs(x - labyrinthx_end) + Mathabs(y -labyrinthy_end) + 1;

//      if(n==1)

//          printPath(individual);

return 10 / n;

}

// 运行遗传算法

public boolean run() {

// 初始化种群

initPopulation();

Random rand = new Random(SystemcurrentTimeMillis());

boolean success = false;

while (iter_limit-- > 0) {

// 打乱种群的顺序

Collectionsshuffle(individuals);

for (int i = 0; i < population - 1; i += 2) {

// 交叉

if (randnextDouble() < cross_ratio) {

cross(individualsget(i), individualsget(i + 1));

}

// 变异

if (randnextDouble() < muta_ratio) {

mutation(individualsget(i));

}

}

// 种群更替

if (selection() == 1) {

success = true;

break;

}

}

return success;

}

//  public static void main(String[] args) {

//      GA ga = new GA(8, 8);

//      if (!garun()) {

//          Systemoutprintln("没有找到走出迷宫的路径");

//      } else {

//          int gen = gabest_individualsize();

//          boolean[] individual = gabest_individualget(gen - 1)indv;

//          Systemoutprintln(gagetPath(individual));

//      }

//  }

// 根据染色体打印走法

public String getPath(boolean[] individual) {

int length = individuallength;

int x = 1;

int y = 0;

LinkedList<String> stack=new LinkedList<String>();

for (int i = 0; i < length; i++) {

boolean b1 = individual[i];

boolean b2 = individual[++i];

if (b1 == false && b2 == false) {

if (x > 0 && labyrinthmap[y][x - 1] == true) {

x--;

if(!stackisEmpty() && stackpeek()=="右")

stackpoll();

else

stackpush("左");

}

} else if (b1 == false && b2 == true) {

if (x + 1 < width && labyrinthmap[y][x + 1] == true) {

x++;

if(!stackisEmpty() && stackpeek()=="左")

stackpoll();

else

stackpush("右");

}

} else if (b1 == true && b2 == false) {

if (y > 0 && labyrinthmap[y - 1][x] == true) {

y--;

if(!stackisEmpty() && stackpeek()=="下")

stackpoll();

else

stackpush("上");

}

} else if (b1 == true && b2 == true) {

if (y + 1 < height && labyrinthmap[y + 1][x] == true) {

y++;

if(!stackisEmpty() && stackpeek()=="上")

stackpoll();

else

stackpush("下");

}

}

}

StringBuilder sb=new StringBuilder(length/4);

Iterator<String> iter=stackdescendingIterator();

while(iterhasNext())

sbappend(iternext());

return sbtoString();

}

}

以上就是关于java迷宫move类意思全部的内容,包括:java迷宫move类意思、如何用Java实现遗传算法、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/9675927.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-30
下一篇 2023-04-30

发表评论

登录后才能评论

评论列表(0条)

保存