当前位置: 代码迷 >> 综合 >> 5阶幻方 奇异幻方 18中心 遗传算法
  详细解决方案

5阶幻方 奇异幻方 18中心 遗传算法

热度:86   发布时间:2023-10-13 12:56:32.0

用遗传算法,配合数据库,找了一个五阶的幻方。
颠覆认知的是,18在中心,而且还成立。
一般认为13在中心的。
5阶幻方 奇异幻方 18中心 遗传算法
以下是找到的部分结果

18,10,25,7,5,6,22,3,14,20,11,2,13,24,15,9,12,23,4,17,21,19,1,16,8,
18,12,23,7,5,6,22,1,16,20,9,2,13,24,17,11,10,25,4,15,21,19,3,14,8,
18,12,23,7,5,6,22,1,16,20,11,2,13,24,15,9,10,25,4,17,21,19,3,14,8,
18,12,23,7,5,9,22,1,16,17,6,2,13,24,20,11,10,25,4,15,21,19,3,14,8,
18,12,23,7,5,9,22,1,16,17,11,2,13,24,15,6,10,25,4,20,21,19,3,14,8,
18,12,23,7,5,11,22,1,16,15,6,2,13,24,20,9,10,25,4,17,21,19,3,14,8,
18,12,23,7,5,11,22,1,16,15,9,2,13,24,17,6,10,25,4,20,21,19,3,14,8

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace FiveLevelMagicNumber
{
    public partial class Form1 : Form{
    public Form1(){
    InitializeComponent();}private void Form1_Load(object sender, EventArgs e){
    }private void button1_Click(object sender, EventArgs e){
    Task t1 = Task.Run(()=> {
    GA(label1);});}private void GA(Label L){
    Random R1 = new Random(452);Random R2 = new Random(7687);Random R3 = new Random(4854);string Swap,sql;int SwapN1;int SwapN2;int GenationN;int MaxRows;DataTable DtHightScore = new DataTable();int Score;for (int i = 0; i < 7654321; i++){
    L.BeginInvoke(new Action(() => {
     L.Text = i.ToString(); }));//读出得分最高的前12345种排列sql = $@"select top 12345 ID,N1,N2,N3,N4,N5,N6,N7,N8,N9,N10,N11,N12,N13,N14,N15 ,N16,N17,N18,N19,N20,N21,N22,N23,N24,N25,Fitness from FiveLevelMagicNumber2 order by Fitness desc";DtHightScore.Clear();DtHightScore = SQLHelper.GetDataTable(sql);//dataGridView1.DataSource = DtHightScore;MaxRows = DtHightScore.Rows.Count;//随机筛选里面的一种结果GenationN = R1.Next(0, MaxRows);//对其中的两个数进行调换SwapN1 = R2.Next(1, 26);SwapN2 = R2.Next(1, 26);if (SwapN1 == SwapN2){
    continue;}//交换Swap = DtHightScore.Rows[GenationN][SwapN1].ToString();DtHightScore.Rows[GenationN][SwapN1] =DtHightScore.Rows[GenationN][SwapN2].ToString();DtHightScore.Rows[GenationN][SwapN2] = Swap;//交换之后计算每行、每列的和Score = 0;for (int j = 0; j < 5; j++){
    if (int.Parse(DtHightScore.Rows[GenationN][5 * j + 1].ToString())+ int.Parse(DtHightScore.Rows[GenationN][5 * j + 2].ToString())+ int.Parse(DtHightScore.Rows[GenationN][5 * j + 3].ToString())+ int.Parse(DtHightScore.Rows[GenationN][5 * j + 4].ToString())+ int.Parse(DtHightScore.Rows[GenationN][5 * j + 5].ToString())== 65){
    Score = Score + 1;}}for (int j = 0; j < 5; j++){
    if (int.Parse(DtHightScore.Rows[GenationN][j + 1].ToString())+ int.Parse(DtHightScore.Rows[GenationN][j + 6].ToString())+ int.Parse(DtHightScore.Rows[GenationN][j + 11].ToString())+ int.Parse(DtHightScore.Rows[GenationN][j + 16].ToString())+ int.Parse(DtHightScore.Rows[GenationN][j + 21].ToString())== 65){
    Score = Score + 1;}}//斜对角加分if (int.Parse(DtHightScore.Rows[GenationN][1].ToString())+ int.Parse(DtHightScore.Rows[GenationN][7].ToString())+ int.Parse(DtHightScore.Rows[GenationN][13].ToString())+ int.Parse(DtHightScore.Rows[GenationN][19].ToString())+ int.Parse(DtHightScore.Rows[GenationN][25].ToString())== 65){
    Score = Score + 1;}if (int.Parse(DtHightScore.Rows[GenationN][5].ToString())+ int.Parse(DtHightScore.Rows[GenationN][9].ToString())+ int.Parse(DtHightScore.Rows[GenationN][13].ToString())+ int.Parse(DtHightScore.Rows[GenationN][17].ToString())+ int.Parse(DtHightScore.Rows[GenationN][21].ToString())== 65){
    Score = Score + 1;}DtHightScore.Rows[GenationN]["Fitness"] = Score;sql = $@"INSERT INTO [dbo].[FiveLevelMagicNumber2] ( [N1], [N2], [N3], [N4], [N5], [N6], [N7] , [N8], [N9], [N10], [N11], [N12], [N13], [N14], [N15], [N16], [N17], [N18], [N19], [N20], [N21], [N22], [N23] , [N24], [N25], [Fitness]) VALUES ( N'{DtHightScore.Rows[GenationN][1]}', N'{DtHightScore.Rows[GenationN][2]}' , N'{DtHightScore.Rows[GenationN][3]}',N'{DtHightScore.Rows[GenationN][4]}' ,N'{DtHightScore.Rows[GenationN][5]}', N'{DtHightScore.Rows[GenationN][6]}' , N'{DtHightScore.Rows[GenationN][7]}', N'{DtHightScore.Rows[GenationN][8]}' , N'{DtHightScore.Rows[GenationN][9]}' , N'{DtHightScore.Rows[GenationN][10]}', N'{DtHightScore.Rows[GenationN][11]}' , N'{DtHightScore.Rows[GenationN][12]}', N'{DtHightScore.Rows[GenationN][13]}' , N'{DtHightScore.Rows[GenationN][14]}', N'{DtHightScore.Rows[GenationN][15]}' , N'{DtHightScore.Rows[GenationN][16]}', N'{DtHightScore.Rows[GenationN][17]}' , N'{DtHightScore.Rows[GenationN][18]}',N'{DtHightScore.Rows[GenationN][19]}' , N'{DtHightScore.Rows[GenationN][20]}', N'{DtHightScore.Rows[GenationN][21]}' , N'{DtHightScore.Rows[GenationN][22]}', N'{DtHightScore.Rows[GenationN][23]}' , N'{DtHightScore.Rows[GenationN][24]}', N'{DtHightScore.Rows[GenationN][25]}' , N'{Score}')";SQLHelper.ExecuteNonQuery(sql);}}}
}