1、遗传算法示例实验一、过河问题 一、问题描述有三个牧师和三个野人过河,只有一条能装下两个人的船,在河的任何一方或者船上,如果野人的人数大于牧师的认输,那么牧师就会有危险.找出一种按的渡河方法。将该问题转变为:假如有n个牧师和n个野人准备渡河,但只有一条能容纳c个人的小船,为了防止野人侵犯牧师,要求无论在何处,牧师的人数不得少于野人的人数(除非牧师人数为0),且假定两种人都会划船,试设计一个算法,确定它们能否渡过河去,若能,则给出一只小船来回次数最少的最佳方案。二、基本要求输入:牧师人数(即野人人数)n,小船一次至多载客人数c。输出:若问题无解,则显示“渡河失败”信息,否则,输出一组最佳方案,用三
2、组(X1,X2,X3)表示渡河过程中的状态。并用箭头输出这些状态之间的迁移:目的状态- -中间状态 - -初始状态。例:当n=2,c=2时,输出 000-021-211-110p1,队头元素出队列B while(未达到目标,且P1有可达、合法、且未到达过的相邻顶点Q) if (Q=(000) 则cond=1,Q入队列 否则 置QW为“已达”,Q入队列 /* B可用函数COMBINE实现 */4 if (cond=1)则按队列中前趋指针指示的次序依次输出序列,否则输出“渡河失败”。5 COMBINE函数的功能等价于从数量不等的物品,分别选出1件、2件、C件物品的所有组合,同时对每一种组合确定其合
3、法性。COMBINE( ) 1 栈SP初始化(SP存放已放入物品序号),NUM为已取出物品个数,NUM=0,i为准备取出物品序号,i=1。2 do while (未达到目标,且所有物品还未取尽,且NUMi,将第种物品放回一件:NUM-:退栈;i+;while(未达到目标,且并非所有情况均已列举完) dicision ( ) if (当前状态(x1,x2,x3)合法且未达) 则(x1,x2,x3)及前趋指针入队列STACK;if (x1,x2,x3)=0,0,0) 则 cond=1;四、源程序#include typedef struct node int np; /* The normal p
4、eoples number at start shore. */ int mp; /* The mad peoples number at start shore. */ int shore; /* 0=end shore,1=start shore */ int track; /* The track of the point */NODE;NODE stack80; /* The massage from stack1*/int state80802,n,c,front,back,cond;void dicision(int t) int a4,i; for(i=0;i4;i+) ai=t
5、i; if(a2=1) a0=n-a0; a1=n-a1; if(a0=0|a0=n|a0=a1) & statea0a1a2=1) back+; stackback.np=a0; stackback.mp=a1; stackback.shore=a2; stackback.track=front; statea0a1a2=0; if(a0=0 & a1=0 & a2=0) cond=1; void combine(int t) int sp80;/* The stack */ int top; /* The stack sps top*/ int all; /* The peoples nu
6、mber at start shore */ int num; /* The things number which allready get */ int i; top=i=num=0; t2=!t2; all=t0+t1; do while(cond!=1 & num0 & i2) if(ti=0) if(i0) i=sp-top; ti+; all+; num-; i+; while(cond!=1&( top0 | (i0) ) );void put(NODE stack) int i,j,m,b80; printf(nStack Np Mp Shore Last pointn); f
7、or(i=1;i=back;i+) printf(%5d%5d%7d%10dn,i,stacki.np,stacki.mp,stacki.shore,stacki.track); if(cond=1) i=back;m=0; while(i!=0) bm+=i; i=stacki.track; printf(The cross way is: ); for(j=m-1;j=0;j-) printf(%d,stackbj.np); printf(%d,stackbj.mp); printf(%d,stackbj.shore); if(j!=0) printf()-); printf()n); p
8、rintf(The stack is: %d-,back); for(j=0;j); printf(nSeccess!); else printf(Failure!); printf(n);void main() int i,j,s,t4; printf(please input the number of people (n): ); scanf(%d,&n); printf(please input the capacity of boat (c): ); scanf(%d,&c); for(i=0;i80;i+) for(j=0;j80;j+) for(s=0;sfront & cond
9、!=1) front+; t0=stackfront.np; t1=stackfront.mp; t2=stackfront.shore; t3=stackfront.track; if(t2=0) t0=n-t0; t1=n-t1; combine(t); put(stack);五、运行结果实验二、九宫重排一、 问题描述 利用A*算法实现八数码难题(九宫重排)的搜索。 二、 算法描述procedure heuristic_search open :=start ; closed:= ;f(s) := g(s) + h(s) ; while open != do begin 从open 表中删
10、除第一个状态,称为n ; if n = 目的状态 then return (success) ; 生成n 的所有子状态; if n 没有任何子状态 then continue ; for n 的每个子状态 do case 子状态 is not already on open 表 or closed 表 : begin 计算该子状态的估价函数值; 将该子状态加到open 表中; end ; case 子状态 is already on open 表 : if 该子状态是沿着一条比在open 表已有的更短路径而到达 then 记录更短路径走向及其估价函数值; case 子状态 is already
11、 on closed 表: if 该子状态是沿着一条比在closed 表已有的更短路径而到达 begin 将该子状态从closed 表移到 open 表中; 记录更短路径走向及其估价函数值; end ; case end ; 将n 放入closed 表中; 根据估价函数值,从小到大重新派列open 表; end ; return (failure);end 应当注意:定义h*(n)为状态n到目的状态的最优路径的代价。 则,估价函数f(n)中的h(n)应满足:h(n)h*(n)三、 程序如下#include using namespace std;const int N = 3; /数组的维数c
12、onst int M = 100; /open 和close的大小const static int goalNN = 1,2,3,/目标状态 8,0,4, 7,6,5;struct state /状态结点 int arrStateNN; int value; /该结点的启发值f(n) int depth; /所在的第几层 int parent; /父节点 int nID; /结点标记public: state() for(int i=0; iN; i+) for(int j=0; jN; j+) arrStateij = -1; value = -1; /该结点的启发值f(n) depth =
13、 -1; /所在的第几层 parent = -1; /父节点 nID = -1; /结点标记 state& operator = (state s) for(int i=0; iN; i+) for(int j=0; jN; j+) arrStateij = s.arrStateij; value = s.value; depth = s.depth; parent = s.parent; nID = s.nID; return *this; bool operator = (state s) for(int i=0; iN; i+) for(int j=0; jN; j+) if(arrSt
14、ateij != s.arrStateij) return false; return true; bool operator != (state s) return !(*this = s); ;class Eightprivate: state openM; /open 表 int openIndex;/open表的元素个数 state closedM; /close 表 int closedIndex;/closed 表的元素个数 int startNN;/开始的状态 int nAutoIncrease;public: Eight(); Eight(int sN); void init(
15、);/初始化open 和close int f(state s); int w(int sNN); void sortOpen();/对Open表进行排序 void moveToClosed(state s); /void moveToOpen(state s); void genToOpen(state s); void findZeroPostion(int &x,int &y, state s);/查找0 的位置 进行上下左右移动 bool compare(state s); /当前的状态与目标状态比较 void genNewState(state oldState); void heu
16、risticSearch();/查找路径 bool IsInOpen(state s); bool IsInClosed(state s); void move(state des,state src); bool IsCanSolve(int sNN); void findPath(); void show(state s);Eight:Eight() start00 = 2; start01 = 8; start02 = 3; start10 = 1; start11 = 6; start12 = 4; start20 = 7; start21 = 0; start22 = 5; nAut
17、oIncrease = 1; openIndex = -1; closedIndex = -1;Eight:Eight(int sN) for(int i = 0; iN; i+) for(int j = 0; jN; j+) startij = sij; nAutoIncrease = 1; openIndex = -1; closedIndex = -1;void Eight:init() for(int i = 0; iN; i+) for(int j = 0; jN; j+) open0.arrStateij = startij; open0.nID = 0; open0.depth
18、= 0; open0.parent = 0; open0.value = w(start) + 0; /f(0) = w(0) +d(0) openIndex = 0; closedIndex = -1;/void Eight:show(state s) for(int i = 0; i N; i+) coutt; for(int j = 0; j N; j+) couts.arrStateij ; coutendl; coutendl;/ coutfn=s.valuetdepth=:s.depthtnID=:s.nIDtparent=:s.parentendl;/启发值f(n) = dept
19、h + w(n)int Eight:f(state s) return s.depth + w(s.arrState);/计算不在位w(n)的值int Eight:w(int sNN) int count = 0; for(int i = 0; i N; i+) for(int j = 0; j N; j+) if(sij = 0 )/不考虑0的位置 continue; if(sij != goalij) count+; return count;/ sort for Open tablevoid Eight:sortOpen() state temp; for(int i=0; i open
20、Index; i+) for(int j = i+1; j openj.value) temp = openi; openi = openj; openj = temp; / find current state 0 postionvoid Eight:findZeroPostion(int &x, int &y, state s) for(int i = 0; i N; i+) for(int j = 0; j N; j+) if(s.arrStateij = 0) x = i; /保持行坐标 y = j; /保存列坐标 return; / the two states exchangevo
21、id Eight:move(state des, state src) for(int row = 0; rowN; row+) for(int col = 0; colN; col+) des.arrStaterowcol = src.arrStaterowcol; des.depth = src.depth; des.parent = src.parent; des.value = src.value;/ extend other statesvoid Eight:genNewState(state oldState) int row,col; int temp;/保存状态转换数组中的值 state newState; findZeroPostion(row,col,oldState);/find zero position if(row +1 -1)/0向上移动 newState = oldState; newState.d
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1