使用 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) {
}
}
}
}
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) {
}
}
}
}