算法学习1简单遗传算法寻路

使用 Silverlight 做表现层

代码思路:

             //1 随机生成 20个 Role
           
//2 移动
           
//3评分 如果 满分就退出
           
//4 杂交
           
//5变异
           
//6GOTO2

遗传算法的核心思想

1构造 可杂交和变异的 染色体,

2通过基因组合起来。

3通过外部 优胜劣汰 进行筛选 对应结果的匹配度。

最终得到结果。

通用神经网络类似人的神经网络。

 

完整源码

部分代码

 
  
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Learning {
public partial class MainPage : UserControl {

private Thread thread1;
private List < Role > list = new List < Role > ();
private RoleManager roleManager;
private int OPTime = 0 ;
public MainPage() {
InitializeComponent();
thread1
= new Thread(yichuan);
dataGrid1.ItemsSource
= list;
}

private void yichuan() {
MapManager mapManager
= new MapManager();
MapManager.Map[
0 , 9 , 9 ] = - 1 ;

// 1 随机生成 20个 Role
// 2 移动
// 3评分 如果 满分就退出
// 4 杂交
// 5变异
// 6GOTO2

// 1
roleManager = new RoleManager();
for ( int i = 0 ; i < 20 ; i ++ ) {
roleManager.RoleList.Add(RoleManager.GetRandomRole(
9 , 9 ));
}

// 2
step2:
for ( int i = 0 ; i < roleManager.RoleList.Count; i ++ ) {
var role
= roleManager.RoleList[i];
foreach (var a in role.Directions) {
mapManager.TryMove(
ref role, a);
}
}
// 3
roleManager.RoleList.ForEach(a => a.GetGrade(Global.ENDX, Global.ENDY));
UpdataDG();
// 4
var dad = roleManager.GetRole();
Role mom;
do {
mom
= roleManager.GetRole();
}
while (mom == dad);
roleManager.CrossOver(
ref dad, ref mom);
// 5
dad.Mutate(); mom.Mutate();
roleManager.RoleList.ForEach(a
=> {
if (a.Grade == 1 ) {
UpdataDG(
" ok " );
Thread.CurrentThread.Abort();
return ;
}
});
this .Dispatcher.BeginInvoke(
()
=> {
this .OPTime ++ ;
textBlock1.Text
= " " + OPTime + " 次 总分: " + Environment.NewLine + " 最佳表现的终点一个路线和终点 " + Environment.NewLine;
var first
= roleManager.RoleList.OrderByDescending(a => a.Grade).FirstOrDefault();
textBlock1.Text
+= first.X + " / " + first.Y + Environment.NewLine;
first.Directions.ToList().ForEach(
a
=> {
textBlock1.Text
+= a.ToString();
});
}
);
Thread.Sleep(
500 );
goto step2;
}

private void UpdataDG() {
this .Dispatcher.BeginInvoke(
()
=> {
// MessageBox.Show("prefect !");
dataGrid1.ItemsSource = null ;
list
= roleManager.RoleList.ToList(); // adsadadsda
dataGrid1.ItemsSource = list;
}
);
}
private void UpdataDG(Object OB) {
this .Dispatcher.BeginInvoke(
()
=> {
MessageBox.Show(OB.ToString());
}
);
}

private void button1_Click( object sender, RoutedEventArgs e) {
thread1.Start();
}

private void button2_Click( object sender, RoutedEventArgs e) {
try {
thread1.Abort();
}
catch (Exception) {
}

}
}
}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值