Algoritma Genetika dibuktikan dengan enam Cromosom Secara ACAK
1. Desain Tampilan
2. Program Visual Basic
using System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Windows.Forms;
namespace AL_Set
{
public partial class Form1 : Form
{
double FungOb, FungOb2, FIT, x;
int[,] Populasi = new int[4, 6];
int[,] Proba = new int[4, 6];
double[] Roll = new double[6];
double[] Roll2 = new double[6];
int[] FO = new int[6];
double[] FI = new double[6];
double[] PB = new double[6];
double[] KP = new double[6];
int[][] CH = new int[][]
{
new int[4],
new int[4],
new int[4],
new int[4],
new int[4],
new int[4],
};
public Form1()
{
InitializeComponent();
}
private void RANDOM_Click(object
sender, EventArgs e)
{
Random n = new Random();
for (int i = 1; i < 4; i++)
{
for (int j = 0; j < 6; j++)
{
for (int l = 0; l <
1000; l++)
{
Populasi[0, j] =
n.Next(30);
Populasi[i, j] =
n.Next(10);
Roll[j] =
n.NextDouble();
Roll[j] =
Math.Round(Roll[j], 3);
}
}
}
//Nilai awal chromosome
Cro00.Text =
Convert.ToString(Populasi[0, 0]);
Cro01.Text =
Convert.ToString(Populasi[0, 1]);
Cro02.Text =
Convert.ToString(Populasi[0, 2]);
Cro03.Text = Convert.ToString(Populasi[0,
3]);
Cro04.Text =
Convert.ToString(Populasi[0, 4]);
Cro05.Text =
Convert.ToString(Populasi[0, 5]);
Cro10.Text =
Convert.ToString(Populasi[1, 0]);
Cro11.Text =
Convert.ToString(Populasi[1, 1]);
Cro12.Text =
Convert.ToString(Populasi[1, 2]);
Cro13.Text =
Convert.ToString(Populasi[1, 3]);
Cro14.Text =
Convert.ToString(Populasi[1, 4]);
Cro15.Text =
Convert.ToString(Populasi[1, 5]);
Cro20.Text = Convert.ToString(Populasi[2,
0]);
Cro21.Text =
Convert.ToString(Populasi[2, 1]);
Cro22.Text =
Convert.ToString(Populasi[2, 2]);
Cro23.Text =
Convert.ToString(Populasi[2, 3]);
Cro24.Text = Convert.ToString(Populasi[2,
4]);
Cro25.Text =
Convert.ToString(Populasi[2, 5]);
Cro30.Text =
Convert.ToString(Populasi[3, 0]);
Cro31.Text =
Convert.ToString(Populasi[3, 1]);
Cro32.Text =
Convert.ToString(Populasi[3, 2]);
Cro33.Text =
Convert.ToString(Populasi[3, 3]);
Cro34.Text =
Convert.ToString(Populasi[3, 4]);
Cro35.Text =
Convert.ToString(Populasi[3, 5]);
//Roulete whell
Rou0.Text =
Convert.ToString(Roll[0]);
Rou1.Text =
Convert.ToString(Roll[1]);
Rou2.Text =
Convert.ToString(Roll[2]);
Rou3.Text =
Convert.ToString(Roll[3]);
Rou4.Text = Convert.ToString(Roll[4]);
Rou5.Text =
Convert.ToString(Roll[5]);
}
private void PROCESS_Click(object
sender, EventArgs e)
{
FungOb = 0;
FIT = 0;
for (int j = 0; j < 6; j++)
{
FO[j] = Math.Abs((Populasi[0, j] +
Populasi[1, j] * 2 + Populasi[2, j] * 3 + Populasi[3, j]*4) - 30);
FungOb = FungOb + FO[j];
}
for (int j = 0; j < 6; j++)
{
FI[j] = FO[j] + 1;
FIT = FIT + (1/FI[j]);
}
for (int j = 0; j < 6; j++)
{
PB[j] = (1 / FI[j]) / FIT;
}
KP[0] = PB[0];
KP[1] = PB[0] + PB[1];
KP[2] = PB[0] + PB[1] + PB[2];
KP[3] = PB[0] + PB[1] + PB[2] +
PB[3];
KP[4] = PB[0] + PB[1] + PB[2] +
PB[3] + PB[4];
KP[5] = PB[0] + PB[1] + PB[2] +
PB[3] + PB[4] + PB[5];
FungOb2 = FungOb / 6;
FungOb2 = Math.Round(FungOb2, 2);
for (int K = 0; K < 6; K++)
{
for (int J = 0; J < 4; J++)
{
if (Roll[K] < KP[0])
{
Proba[J,K] = Populasi[J,0];
} }
}
for (int i = 0; i < 100; i++)
{
for (int K = 0; K < 6; K++)
{
for (int J = 0; J < 4;
J++)
{
if (KP[0] < Roll[K]
&& Roll[K] < KP[1])
{
Proba[J, K] =
Populasi[J, 1];
}
if (KP[1] < Roll[K]
&& Roll[K] < KP[2])
{
Proba[J, K] =
Populasi[J, 2];
}
if (KP[2] < Roll[K]
&& Roll[K] < KP[3])
{
Proba[J, K] =
Populasi[J, 3];
}
if (KP[3] < Roll[K]
&& Roll[K] < KP[4])
{
Proba[J, K] =
Populasi[J, 4];
}
if (KP[4] < Roll[K]
&& Roll[K] < KP[5])
{
Proba[J, K] =
Populasi[J, 5];
}
}}}
CH[0] = new int[] { Proba[0, 0],
Proba[1, 0], Proba[2, 0], Proba[3, 0] };
CH[1] = new int[] { Proba[0, 1], Proba[1,
1], Proba[2, 1], Proba[3, 1] };
CH[2] = new int[] { Proba[0, 2],
Proba[1, 2], Proba[2, 2], Proba[3, 2] };
CH[3] = new int[] { Proba[0, 3],
Proba[1, 3], Proba[2, 3], Proba[3, 3] };
CH[4] = new int[] { Proba[0, 4],
Proba[1, 4], Proba[2, 4], Proba[3, 4] };
CH[5] = new int[] { Proba[0, 5],
Proba[1, 5], Proba[2, 5], Proba[3, 5] };
x=0;
do
{
Random n = new Random();
for (int j = 0; j < 6; j++)
{
for (int l = 0; l <
1000; l++)
{
Roll[j] =
n.NextDouble();
Roll[j] = Math.Round(Roll[j],
3);
}
if (Roll[j] < 0.25)
{
x++;
}
}
} while (x < 4);
//Fungsi Obyek
FO0.Text = Convert.ToString(FO[0]);
FO1.Text = Convert.ToString(FO[1]);
FO2.Text = Convert.ToString(FO[2]);
FO3.Text = Convert.ToString(FO[3]);
FO4.Text = Convert.ToString(FO[4]);
FO5.Text = Convert.ToString(FO[5]);
rata_FO.Text =
Convert.ToString(FungOb2);
//Fitnes
fit0.Text =
Convert.ToString(1/FI[0]);
fit1.Text =
Convert.ToString(1/FI[1]);
fit2.Text = Convert.ToString(1/FI[2]);
fit3.Text =
Convert.ToString(1/FI[3]);
fit4.Text =
Convert.ToString(1/FI[4]);
fit5.Text =
Convert.ToString(1/FI[5]);
total_Fit.Text =
Convert.ToString(FIT);
//Probalitas
pro0.Text = Convert.ToString(PB[0]);
pro1.Text =
Convert.ToString(PB[1]);
pro2.Text =
Convert.ToString(PB[2]);
pro3.Text =
Convert.ToString(PB[3]);
pro4.Text =
Convert.ToString(PB[4]);
pro5.Text =
Convert.ToString(PB[5]);
//Komulatif
kom0.Text =
Convert.ToString(KP[0]);
kom1.Text =
Convert.ToString(KP[1]);
kom2.Text =
Convert.ToString(KP[2]);
kom3.Text =
Convert.ToString(KP[3]);
kom4.Text = Convert.ToString(KP[4]);
kom5.Text =
Convert.ToString(KP[5]);
// Populasi Baru Chromosome
Cro200.Text =
Convert.ToString(Proba[0, 0]);
Cro201.Text =
Convert.ToString(Proba[0, 1]);
Cro202.Text = Convert.ToString(Proba[0,
2]);
Cro203.Text =
Convert.ToString(Proba[0, 3]);
Cro204.Text =
Convert.ToString(Proba[0, 4]);
Cro205.Text =
Convert.ToString(Proba[0, 5]);
Cro210.Text =
Convert.ToString(Proba[1, 0]);
Cro211.Text =
Convert.ToString(Proba[1, 1]);
Cro212.Text =
Convert.ToString(Proba[1, 2]);
Cro213.Text =
Convert.ToString(Proba[1, 3]);
Cro214.Text = Convert.ToString(Proba[1,
4]);
Cro215.Text =
Convert.ToString(Proba[1, 5]);
Cro220.Text =
Convert.ToString(Proba[2, 0]);
Cro221.Text =
Convert.ToString(Proba[2, 1]);
Cro222.Text =
Convert.ToString(Proba[2, 2]);
Cro223.Text =
Convert.ToString(Proba[2, 3]);
Cro224.Text =
Convert.ToString(Proba[2, 4]);
Cro225.Text =
Convert.ToString(Proba[2, 5]);
Cro230.Text =
Convert.ToString(Proba[3, 0]);
Cro231.Text =
Convert.ToString(Proba[3, 1]);
Cro232.Text =
Convert.ToString(Proba[3, 2]);
Cro233.Text =
Convert.ToString(Proba[3, 3]);
Cro234.Text =
Convert.ToString(Proba[3, 4]);
Cro235.Text =
Convert.ToString(Proba[3, 5]);
}
private void button1_Click(object
sender, EventArgs e)
{
this.Close();
}
private void button2_Click(object
sender, EventArgs e)
{
Random n = new Random();
for (int i = 1; i < 4; i++)
{
for (int j = 0; j < 6; j++)
{
for (int l = 0; l <
1000; l++)
{
Populasi[0, j] =
n.Next(30);
Populasi[i, j] = n.Next(10);
Roll2[j] =
n.NextDouble();
Roll2[j] =
Math.Round(Roll2[j], 3);
} }}
//Roulate ke 2
rou20.Text = Convert.ToString(Roll2[0]);
rou21.Text =
Convert.ToString(Roll2[1]);
rou22.Text =
Convert.ToString(Roll2[2]);
rou23.Text =
Convert.ToString(Roll2[3]);
rou24.Text =
Convert.ToString(Roll2[4]);
rou25.Text = Convert.ToString(Roll2[5]);
}}}
#visual Basic #algoritma #Algoritmagenetika
NB: Jika menginginkan file asli, silahkan PM melalui CP