//Caculate hn public static int nonSuited(Chart trans, Chart goal) { int result = 0; for (int i = 0; i < goal.positions.Length; i++) { if (goal.positions[i].getNum() != trans.positions[i].getNum()) { result++; } } return result; }
public static int Manhattan(Chart trans,Chart goal) { int result = 0; for(int i=0;i<9;i++) { if(trans.positions[i].getNum()!=0) { for(int j=0;j<9;j++) { if(trans.positions[i].getNum()==goal.positions[j].getNum()) { result += Math.Abs(i - j); break; } } } } return result; }
public int BreadthFirst() { closeList.Clear(); openList.Clear(); openList.Add(initial); Chart transNode=new Chart(); while (true) { if (openList.Count == 0) { MessageBox.Show("open list is empty"); return fail; } transNode = openList.ElementAt(0); if (hit(transNode)) { MessageBox.Show("成功!遍历的节点数为:" + closeList.Count.ToString()); return success; } closeList.Add(transNode); openList.Remove(transNode); transNode.expandNode(); foreach (Chart item in transNode.children) { item.parent = transNode; } appendOpenList(transNode.children); } }
public bool equals(Chart chart2) { bool flag=true; for(int i=0;i<9;i++) { if (this.positions[i].getNum() != chart2.positions[i].getNum()) flag = false; } return flag; }
public Chart clone() { Chart newchart = new Chart(); for (int i = 0; i < this.positions.Length; i++) { newchart.positions[i] = (Position)this.positions[i].clone(); } return newchart; }
internal static int CalculateFn(Chart chart1, Chart chart2,int method) { switch(method) { case 0: return nonSuited(chart1, chart2); case 1: return Manhattan(chart1, chart2); default: return nonSuited(chart1, chart2); } }
public bool parentContains(Chart chart) { if (this.parent != null) { if (this.parent.equals(chart)) return true; else return this.parent.parentContains(chart); } return false; }
private void ModifyReference(Chart transNode, List<Chart> children) { foreach(Chart item in children) { bool flag1=false; bool flag2=false; foreach(Chart item1 in openList) { if(item.equals(item1)) flag1=true; } foreach(Chart item2 in closeList) { if(item.equals(item2)) flag2=true; } if(!flag1&&!flag2) { openList.Add(item); item.parent = transNode; item.gn = item.parent.gn + 1; item.hn = Chart.CalculateFn((Chart)item, (Chart)goal,ComboBox2.SelectedIndex); item.fn = item.gn + item.hn; } else if(flag1&&!flag2) { if(transNode.gn+1+item.hn<item.fn) { item.parent = transNode; item.fn=transNode.gn+1+item.hn; item.gn=transNode.gn+1; } } else if(!flag1&&flag2) { if (transNode.gn + 1 + item.hn < item.fn) { item.fn = transNode.gn + 1 + item.hn; item.gn = transNode.gn + 1; item.parent = transNode; openList.Add(item); closeList.Remove(item); } } } }
private bool hit(Chart transNode) { if (Chart.nonSuited((Chart)transNode, (Chart)goal) == 0) return true; return false; }
private void ButtonStart_Click(object sender, RoutedEventArgs e) { int []numForInitial=new int[9]; int []numForGoal=new int[9]; int i=0; foreach(TextBox _textBox in uniformgrid1.Children) { numForInitial[i] = int.Parse(_textBox.Text); i++; } i = 0; foreach(TextBox _textBox in uniformgrid2.Children) { numForGoal[i] = int.Parse(_textBox.Text); i++; } if (!isSolve(numForInitial, numForGoal)) { MessageBox.Show("该情况下无解"); } initial = new Chart(numForInitial); goal = new Chart(numForGoal); openList = new List<Chart>(); closeList = new List<Chart>(); //method=0,A*启发性搜索 //method=1,广度优先 int method = ComboBox1.SelectedIndex; switch(method) { case 0: //A star here; AStar(); break; case 1: //BreadthFirst Here; BreadthFirst(); break; } }