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 genetic_2._0
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public int totalfitness;
public int counter2 = 0;
private void Form1_Load(object sender, EventArgs e)
{
}
public void TotalFitness(int[] fitnesses)
{
foreach (int i in fitnesses)
{
totalfitness = totalfitness + i;
}
}
int[] GetIntArray(int num)
{
List<int> listOfInts = new List<int>();
while (num > 0)
{
listOfInts.Add(num % 10);
num = num / 10;
}
listOfInts.Reverse();
return listOfInts.ToArray();
}
private void button1_Click(object sender, EventArgs e)
{
genome[] genome = new genome[1000];
genome[counter2] = new genome();
genome[counter2].SetLenght(Int32.Parse((textBox1.Text)));
int[] genes = GetIntArray(Int32.Parse(textBox2.Text));
genome[counter2].SetGenesBinary(genes);
genome[counter2].SetName("g"+counter2.ToString());
genome[counter2].ConvertBinaryToDecimal();
genome[counter2].GetFitness(genome[counter2].genes_decimal);
string toDisplay = string.Join(Environment.NewLine, genome[counter2].genes_binary);
string toDisplay2 = string.Join(Environment.NewLine, genome[counter2].genes_decimal);
listBox1.Items.Add(genome[counter2].name1 + " " + genome[counter2].lenght1+" "+toDisplay+" "+toDisplay2+ " "+genome[counter2].fitness);
totalfitness = Int32.Parse(label4.Text);
totalfitness = Int32.Parse(label4.Text) + genome[counter2].fitness;
label4.Text = totalfitness.ToString();
counter2++;
}
private void button2_Click(object sender, EventArgs e)
{
genome[] genome = new genome[1000];
int number = 0;
do
{
genome[counter2] = new genome();
genome[counter2].SetLenght(Int32.Parse((textBox1.Text)));
genome[counter2].SetGenesBinaryAutomatic();
genome[counter2].SetName("g" + counter2.ToString());
genome[counter2].ConvertBinaryToDecimal();
genome[counter2].GetFitness(genome[counter2].genes_decimal);
string toDisplay = string.Join(Environment.NewLine, genome[counter2].genes_binary);
string toDisplay2 = string.Join(Environment.NewLine, genome[counter2].genes_decimal);
listBox1.Items.Add(genome[counter2].name1 + " " + genome[counter2].lenght1 + " " + toDisplay + " " + toDisplay2 + " " + genome[counter2].fitness);
totalfitness = Int32.Parse(label4.Text);
totalfitness = Int32.Parse(label4.Text) + genome[counter2].fitness;
label4.Text = totalfitness.ToString();
counter2++;
number++;
} while (number < Int32.Parse(textBox3.Text));
}
}
}
public class genome
{
public string name1; //nazwa
public int lenght1; //długość genomu
public int fitness;//fitness
public int[] genes_binary; //geny binarne
public int[] genes_decimal= new int[2]; //geny decymalne
public int chance; //chance of choseing
int counter; //counter not to use//
public genome()
{
}
public void SetName(string name)
{
name1 = name;
} //ustaw nazwe
public void SetLenght(int lenght)
{
lenght1 = lenght;
genes_binary = new int[lenght];
} //ustaw dlugosc
public void SetGenesBinary(int[] genesbinary) //ustaw geny binarne
{
if(genesbinary.Length == lenght1)
{
counter = 0;
foreach(int gene in genesbinary)
{
genes_binary[counter] = gene;
counter++;
}
}
}
public void SetGenesBinaryAutomatic()
{
Random rand = new Random();
counter = 0;
int lenght;
for (lenght = 0; lenght < lenght1; lenght++ )
{
genes_binary[counter] = rand.Next(0, 2);
counter++;
}
}//ustaw geny binarne automatycznie
public void ConvertBinaryToDecimal()//zmien geny binarne na decymalne
{
if(genes_binary.Length%2 == 0 && genes_binary.Length <10)
{
//liczba parzysta mniejsza od dziesięciu
string liczba = null;
int polowa = (genes_binary.Length / 2);
foreach (int i in genes_binary.Take(polowa))
{
string i_s = i.ToString();
liczba = liczba + i_s;
}
genes_decimal[0] = Convert.ToInt32(liczba,2);
liczba = null;
foreach(int i in genes_binary.Reverse().Take(polowa).Reverse())
{
string i_s = i.ToString();
liczba = liczba + i_s;
}
genes_decimal[1] = Convert.ToInt32(liczba, 2);
}
else
{
string liczba = null;
int b = genes_binary.Length / 2; //3 = 1
int c = genes_binary.Length / 2 + 1;//3 = 2
foreach (int i in genes_binary.Take(b))
{
string i_s = i.ToString();
liczba = liczba + i_s;
}
genes_decimal[0] = Convert.ToInt32(liczba, 2);
liczba = null;
foreach (int i in genes_binary.Reverse().Take(c).Reverse())
{
string i_s = i.ToString();
liczba = liczba + i_s;
}
genes_decimal[1] = Convert.ToInt32(liczba, 2);
}
}
public void GetFitness(int[] genesdecimal)
{
int liczba = 0;
foreach(int i in genes_decimal)
{
fitness = fitness + i;
}
现在button2用于生成x随机基因。我们有函数基因组[x] .Setgenesbinaryautomatic,它在数组中生成随机基因。 例如:[1,0,1,0]。 但现在循环中它给出了相同的基因。 我怎样才能修复它,以便随机提供不同的基因。