Genetic algorithm - clustering points on screen - c#

Below is the code I wrote for clustering using genetic algorithm. Points are from a picturebox, generated randomly (X,Y) before calling this class. However, the result of this algorithm is much worse than k-means or lbg I'm comparing it to. Can someone take a look for any errors in the algorithm, maybe I omitted something. Thanks.
I did this using arrays, the 2 other I did using lists, but I don't think that should have any impact on result.
public class geneticAlgorithm
{
static int pom = 0;
static PictureBox pb1;
public geneticAlgorithm(PictureBox pb)
{
pb1 = pb;
}
public static void doGA(PointCollection points, int clusterCounter)
//points is a list of points,
//those point have (X,Y) coordinates generated randomly from pictureBox
//coordinates. clusterCounter is how many clusters I want to divide the points into
{
//this part converts list of points into array testtab,
//where each array field hold X,Y of a point
Point[] test = new Point[points.Count];
test = points.ToArray();
double[][] testtab = new double[test.Length][];
for (int i = 0; i < testtab.GetLength(0); i++)
{
testtab[i] = new double[2];
testtab[i][0] = test[i].X;
testtab[i][1] = test[i].Y;
}
//end of converting
int n = testtab.GetLength(0);
int k = clusterCounter;
int chromosomeCount = 500;
int dimensions = 2;
double[][] testClusters = new double[k][];
for (int i = 0; i < k; i++)
{
testClusters[i] = new double[dimensions];
}
double[][] testChromosomes = new double[chromosomeCount][];
for (int i = 0; i < chromosomeCount; i++)
{
testChromosomes[i] = new double[2 * k];
}
int[][] testChromosomesInt = new int[chromosomeCount][];
for (int i = 0; i < chromosomeCount; i++)
{
testChromosomesInt[i] = new int[2 * k];
}
double[] partner = new double[chromosomeCount];
double[][] roulette = new double[chromosomeCount][];
for (int i = 0; i < chromosomeCount; i++)
{
roulette[i] = new double[1];
}
double[][] errors = new double[chromosomeCount][];
for (int i = 0; i < chromosomeCount; i++)
{
errors[i] = new double[1];
}
double crossingPossibility = 0.01;
double mutationPossibility = 0.0001;
int maxIterations = 10000;
//here I create chromosomes and initial clusters
for (int j = 0; j < chromosomeCount; j++)
{
for (int i = 0; i < k; i++)
{
Random rnd = new Random();
int r = rnd.Next(n);
for (int q = 0; q < dimensions; q++)
{
testClusters[i][q] = testtab[r][q];
}
}
int help = 0;
for (int i = 0; i < k; i++)
for (int l = 0; l < dimensions; l++) // here is creation of chromosome
{
testChromosomes[j][help] = testClusters[i][l];
help++;
}
//end
//here I call accomodation function to see which of them are good
errors[j][0] = accomodationFunction(testClusters, testtab, n, k);
//end
//cleaning of the cluster table
testClusters = new double[k][];
for (int i = 0; i < k; i++)
{
testClusters[i] = new double[dimensions];
}
}
//end
for (int counter = 0; counter < maxIterations; counter++)
{
//start of the roulette
double s = 0.0;
for (int i = 0; i < chromosomeCount; i++)
s += errors[i][0];
for (int i = 0; i < chromosomeCount; i++)
errors[i][0] = chromosomeCount * errors[i][0] / s;
int idx = 0;
for (int i = 0; i < chromosomeCount; i++)
for (int j = 0; i < errors[i][0] && idx < chromosomeCount; j++)
{
roulette[idx++][0] = i;
}
double[][] newTab = new double[chromosomeCount][];
for (int i = 0; i < chromosomeCount; i++)
{
newTab[i] = new double[2 * k];
}
Random rnd = new Random();
for (int i = 0; i < chromosomeCount; i++)
{
int q = rnd.Next(chromosomeCount);
newTab[i] = testChromosomes[(int)roulette[q][0]];
}
testChromosomes = newTab;
//end of roulette
//start of crossing chromosomes
for (int i = 0; i < chromosomeCount; i++)
partner[i] = (rnd.NextDouble() < crossingPossibility + 1) ? rnd.Next(chromosomeCount) : -1;
for (int i = 0; i < chromosomeCount; i++)
if (partner[i] != -1)
testChromosomes[i] = crossing(testChromosomes[i], testChromosomes[(int)partner[i]], rnd.Next(2 * k), k);
//end of crossing
//converting double to int
for (int i = 0; i < chromosomeCount; i++)
for (int j = 0; j < 2 * k; j++)
testChromosomes[i][j] = (int)Math.Round(testChromosomes[i][j]);
//end of conversion
//start of mutation
for (int i = 0; i < chromosomeCount; i++)
if (rnd.NextDouble() < mutationPossibility + 1)
testChromosomesInt[i] = mutation(testChromosomesInt[i], rnd.Next(k * 2), rnd.Next(10));
//end of mutation
}
//painting of the found centre on the picture box
int centrum = max(errors, chromosomeCount);
Graphics g = pb1.CreateGraphics();
SolidBrush brush = new SolidBrush(Color.Red);
for (int i = 0; i < 2 * k - 1; i += 2)
{
g.FillRectangle(brush, testChromosomesInt[centrum][i], testChromosomesInt[centrum][i + 1], 20, 20);
}
return;
}
//end of painting
public static int max(double[][] tab, int chromosomeCount)
{
double max = 0;
int k = 0;
for (int i = 0; i < chromosomeCount; i++)
{
if (max < tab[i][0])
{
max = tab[i][0];
k = i;
}
}
return k;
}
public static int[] mutation(int[] tab, int elem, int bit)
{
int mask = 1;
mask <<= bit;
tab[elem] = tab[elem] ^ mask;
return tab;
}
public static double[] crossing(double[] tab, double[] tab2, int p, int k)
{
double[] hold = new double[2 * k];
for (int i = 0; i < p; i++)
hold[i] = tab[i];
for (int i = p; i < 2 * k; i++)
hold[i] = tab2[i];
return hold;
}
//accomodation function, checks to which centre which point belongs based on distance
private static double accomodationFunction(double[][] klastry, double[][] testtab, int n, int k)
{
double Error = 0;
for (int i = 0; i < n; i++)
{
double min = 0;
int ktory = 0;
for (int j = 0; j < k; j++)
{
double dist = distance(klastry[j], testtab[i]);
if (j == 0)
{
min = dist;
}
if (min > dist)
{
min = dist;
ktory = j;
}
}
Error += min;
}
pom++;
return 1 / Error;
}
public static double distance(double[] tab, double[] tab2)
{
double dist = 0.0;
for (int i = 0; i < tab.GetLength(0); i++)
dist += Math.Pow(tab[i] - tab2[i], 2);
return dist;
}
}
The algorithm should work like so: (excuse me if not the best english)
1. Get random points (let's say 100)
2. Check into how many clusters we want to split them (the same thing I would do using k-means for example
3. Get starting population of chromosomes
4. Throu cutting on the roulette, crossing and mutation pick the best x centres, where x is the amount of clusters we want
5. Paint them.
Here are some results, and why I think it's wrong: (it's using 100 points, 5 clusters)
k-means:
lbg:
genetic(without colors now):
I hope this clarifies a bit.

Related

It's possible to write neural network for different size of training data inputs and outputs

It's possible to write neural network for different size of training data inputs and outputs
for example:
inputs are 1. (1,2,3,4) , 2. (2,3,1), 3. (1,2,3,4,5) and so on...
and the same for outputs 1. (0,0,1,1) 2. (1,1,1) 3. (0,0,1,1,1)
So far I have managed to write the one which only works with the same size of training data which mean that all my training data needs to have the same length.
So far I'm stuck with this
NeuralNetwork net;
int[] layers = new int[3]
{
3/*Always the same*/,
1/*Always the same*/,
3 /*Always the same*/
};
string[] activation = new string[2] { "leakyrelu", "leakyrelu" };
net = new NeuralNetwork(layers, activation);
What I need
NeuralNetwork net1;
int[] layers1 = new int[3]
{
input.Length /*Based on input's Length*/,
1/*Always the same*/,
output.Length /*Based on output's Length*/
};
string[] activation1 = new string[2] { "leakyrelu", "leakyrelu" };
net = new NeuralNetwork(layers, activation);
// BackPropagate
public void BackPropagate(float[] inputs, float[] expected)
{
float[] output = FeedForward(inputs);
cost = 0;
for (int i = 0; i < output.Length; i++) cost += (float)Math.Pow(output[i] - expected[i], 2);
cost = cost / 2;
float[][] gamma;
List<float[]> gammaList = new List<float[]>();
for (int i = 0; i < layers.Length; i++)
{
gammaList.Add(new float[layers[i]]);
}
gamma = gammaList.ToArray();
int layer = layers.Length - 2;
for (int i = 0; i < output.Length; i++)
gamma[layers.Length-1][i] = (output[i] - expected[i]) * activateDer(output[i],layer);
for (int i = 0; i < neurons[layers.Length - 1].Length; i++)
{
biases[layers.Length - 1][i] -= gamma[layers.Length - 1][i] * learningRate;
for (int j = 0; j < neurons[layers.Length - 2].Length; j++)
{
weights[layers.Length - 2][i][j] -= gamma[layers.Length - 1][i] * neurons[layers.Length-2][j] * learningRate;
}
}
for (int i = layers.Length - 2; i > 0; i--)
{
layer = i - 1;
for (int j = 0; j < neurons[i].Length; j++)
{
gamma[i][j] = 0;
for (int k = 0; k < gamma[i+1].Length; k++)
{
gamma[i][j] = gamma[i + 1][k] * weights[i][k][j];
}
gamma[i][j] *= activateDer(neurons[i][j],layer);
}
for (int j = 0; j < neurons[i].Length; j++)
{
biases[i][j] -= gamma[i][j] * learningRate;
for (int k = 0; k < neurons[i-1].Length; k++)
{
weights[i - 1][j][k] -= gamma[i][j] * neurons[i-1][k] * learningRate;
}
}
}
}

How should weights be optimized with gradient descent algorithm in order to work?

I have a neural network in visual studio. for the loss function I am using a basic cost function (pred-target)**2 and after I finish an epoch I optimize the parameter functions afterwards, but the algorithm doesn't work.
No matter what is my network configuration, the predictions are not write (it is the same output for all the inputs) and the loss function is not optimized. It stays the same through all the epochs.
void calc_lyr(int x, int y, int idx, float target) // thus function calculates the neuron value based on the previous layer
{
if (x == -1 || y == 0) // if its the first layer, get the data from input nodes
{
for (int i = 0; i < neurons[y]; i++)
{
float sum = 0;
for (int j = 0; j < inputTypes.Count; j++)
{
sum += weights[x+1][j][i] * training_test[idx][j];
}
sum = relu(sum);
vals[y+1][i] = sum;
}
}
else
{
for(int i = 0; i < neurons[y]; i++)
{
float sum = 0;
for(int j = 0; j < neurons[x]; j++)
{
sum += weights[x+1][j][i] * vals[x+1][j] + biases[y][i];
}
sum = relu(sum);
vals[y+1][i] = sum;
}
}
}
void train()
{
log("Proces de antrenare inceput ----------------- " + DateTime.Now.ToString());
vals = new List<List<float>>();
weights = new List<List<List<float>>>();
biases = new List<List<float>>();
Random randB = new Random(DateTime.Now.Millisecond);
Random randW = new Random(DateTime.Now.Millisecond);
for (int i = 0; i <= nrLayers; i++)
{
progressEpochs.Value =(int)(((float)i * (float)nrLayers) / 100.0f);
vals.Add(new List<float>());
weights.Add(new List<List<float>>());
if (i == 0)
{
for (int j = 0; j < inputTypes.Count; j++)
{
vals[i].Add(0);
}
}
else
{
biases.Add(new List<float>());
for (int j = 0; j < neurons[i-1]; j++)
{
vals[i].Add(0);
float valB = (float)randB.NextDouble();
biases[i-1].Add(valB - ((int)valB));
}
}
}
float valLB = (float)randB.NextDouble();
biases.Add(new List<float>());
biases[nrLayers].Add(valLB - ((int)valLB));
for (int i = 0; i <= nrLayers; i++)
{
if (i == 0)
{
for (int j = 0; j < inputTypes.Count; j++)
{
weights[i].Add(new List<float>());
for (int x = 0; x < neurons[i]; x++)
{
float valW = (float)randW.NextDouble();
weights[i][j].Add(valW);
}
}
}
else if (i == nrLayers)
{
for (int j = 0; j < neurons[i-1]; j++) {
weights[i].Add(new List<float>());
weights[i][j].Add(0);
}
}
else
{
for (int j = 0; j < neurons[i - 1]; j++)
{
weights[i].Add(new List<float>());
for (int x = 0; x < neurons[i]; x++)
{
float valW = (float)randW.NextDouble();
weights[i][j].Add(valW);
}
}
}
}
Random rand = new Random(DateTime.Now.Millisecond);
log("\n\n");
for (int i = 0; i < epochs; i++)
{
log("Epoch " + (i + 1).ToString() + " inceput ---> " + DateTime.Now.ToString());
int idx = rand.Next() % training_test.Count;
float target = outputsPossible.IndexOf(training_labels[idx]);
for (int j = 0; j < nrLayers; j++)
{
calc_lyr(j - 1, j, idx, target);
}
float total_val = 0;
for(int x = 0; x < neurons[nrLayers - 1]; x++)
{
float val = relu(weights[nrLayers][x][0] * vals[nrLayers][x] + biases[nrLayers][0]);
total_val += val;
}
total_val = sigmoid(total_val);
float cost_res = cost(total_val, target);
log("Epoch " + (i+1).ToString() + " terminat ----- " + DateTime.Now.ToString() + "\n");
log("Eroare epoch ---> " + (cost_res<1?"0":"") + cost_res.ToString("##.##") + "\n\n\n");
float cost_der = cost_d(total_val, target);
for (int a = 0; a < weights.Count; a++)
{
for (int b = 0; b < weights[a].Count; b++)
{
for (int c = 0; c < weights[a][b].Count; c++)
{
weights[a][b][c]-=cost_der*learning_rate * sigmoid_d(weights[a][b][c]);
}
}
}
for (int a = 0; a < nrLayers; a++)
{
for (int b = 0; b < neurons[a]; b++)
{
biases[a][b] -= cost_der * learning_rate;
}
}
}
hasTrained = true;
testBut.Enabled = hasTrained;
MessageBox.Show("Antrenament complet!");
SavePrompt sp = new SavePrompt();
sp.Show();
}
How can it be changed to optimize the weights, biases and loss function? For now, when I try to debug, the weights are changing, but it is the same value for the loss function.
I solved it by using AForge.NET: link

TSP branch and bound - sometimes incorrect results C#

I need to solve the TSP problem with the branch and bound algorithm. Here's the code. The problem is, the code works only in small instances (i.e. three cities), but also it isn't working every time.
I've been looking for some mistakes but I couldn't find any.
The costs matrix is the macierz in my code and number of cities is stored in variable ileMiast.
I think the rest is clear.
Don't bother to the sort function or the FinalMatrix array. It's just to present the final order.
public double branchibound(int cities)
{
double totalCost = 0;
FinalMatrix = new ArrayList(ileMiast);
/*
* The ReducedCostMatrix calculates the reduced
* cost matrix
* */
totalCost = this.ReducedCostMatrix(cities);
/*
* Now we would work on the reduced cost
* Matrix to check which nodes to be inluded
* yet or not.
* */
int count = 0;
for (int i = 0; i < cities; i++)
for (int j = 0; j < cities; j++)
{
double[][] tempReduceRow = new double[cities][];
for (int l = 0; l < cities; l++)
tempReduceRow[l] = new double[cities];
for (int l = 0; l < cities; l++)
for (int m = 0; m < cities; m++)
tempReduceRow[l][m] = ReducedMatrix[l][m];
double[][] tempReduceCol = new double[cities][];
for (int l = 0; l < cities; l++)
tempReduceCol[l] = new double[cities];
for (int l = 0; l < cities; l++)
for (int m = 0; m < cities; m++)
tempReduceCol[l][m] = ReducedMatrix[l][m];
/*
* We only include those edges with value 0.
* Now, we will have only those with minimum left
* child and maximum right child.
* */
if (ReducedMatrix[i][j] == 0)
{
OmitLeft(i, j, ref tempReduceRow);
double LBound = totalCost + ReducedCostMatrix(cities, ref tempReduceRow);
OmitRight(i, j, ref tempReduceCol);
double RBound = totalCost + ReducedCostMatrix(cities, ref tempReduceCol);
FinalMatrix.Add(new double[] { LBound, RBound, count });
}
}
//sort();
return totalCost;
}
private void sort()
{
double[] values = new double[3];
double[] a = new double[3];
values = (double[])this.FinalMatrix[0];
int i = 0;
int prevIndex = 0;
for (i = 0; i < this.FinalMatrix.Count; i++)
for (int j = 0; j < this.FinalMatrix.Count; j++)
{
a = (double[])this.FinalMatrix[j];
if (a[1] > values[1] || a[0] < values[0])
{
FinalMatrix[prevIndex] = FinalMatrix[j];
FinalMatrix[j] = values;
values = a;
prevIndex = j;
}
}
}
public String getString()
{
String str = new String('A', 20);
str = "";
foreach (double[] a in this.FinalMatrix)
str += (char)(a[2]);
return str;
}
private void OmitLeft(int row, int col, ref double[][] temp)
{
for (int j = 0; j < temp.Length; j++)
temp[row][j] = -1;
for (int j = 0; j < temp.Length; j++)
temp[j][col] = -1;
}
private void OmitRight(int row, int col, ref double[][] temp)
{
temp[row][col] = -1;
}
private double ReducedCostMatrix(int cities)
{
double minBound = 0;
ReducedMatrix = new double[cities][];
for (int i = 0; i < cities; i++)
ReducedMatrix[i] = new double[cities];
/*
* Here we make a new Reduced Matrix
* by copying the original matrix
* into the new one.
* */
for (int i = 0; i < cities; i++)
for (int j = 0; j < cities; j++)
ReducedMatrix[i][j] = macierz[i][j];
minBound = ReducedRows(cities);
minBound += ReducedCols(cities);
return minBound;
}
private double ReducedCostMatrix(int cities, ref double[][] temp)
{
double minBound = 0;
minBound = ReducedRows(cities, ref temp);
minBound += ReducedCols(cities, ref temp);
return minBound;
}
private double ReducedRows(int cities)
{
double min = 65535;
bool flag = false;
double minBound = 0;
for (int i = 0; i < cities; i++)
{
min = 65535;
for (int j = 0; j < cities; j++)
if (this.ReducedMatrix[i][j] < min && this.ReducedMatrix[i][j] != -1)
{
min = this.ReducedMatrix[i][j];
flag = true;
}
if (flag)
{
this.SubtractRow(i, min);
minBound += min;
}
}
return minBound;
}
private double ReducedRows(int cities, ref double[][] temp)
{
double min = 65535;
double minBound = 0;
for (int i = 0; i < cities; i++)
{
min = 65535;
bool flag = true;
for (int j = 0; j < cities; j++)
if (temp[i][j] < min && temp[i][j] != -1)
{
min = temp[i][j];
flag = false;
}
if (!flag)
{
this.SubtractRow(i, min, ref temp);
minBound += min;
}
}
return minBound;
}
private double ReducedCols(int cities)
{
double min = 65535;
double minBound = 0;
for (int j = 0; j < cities; j++)
{
for (int i = 0; i < cities; i++)
if (this.ReducedMatrix[i][j] < min && this.ReducedMatrix[i][j] != -1)
min = ReducedMatrix[i][j];
this.SubtractCol(j, min);
minBound += min;
}
return minBound;
}
private double ReducedCols(int cities, ref double[][] temp)
{
double min = 65535;
double minBound = 0;
for (int j = 0; j < cities; j++)
{
for (int i = 0; i < cities; i++)
if (temp[i][j] < min && temp[i][j] != -1)
min = temp[i][j];
SubtractCol(j, min, ref temp);
minBound += min;
}
return minBound;
}
private void SubtractRow(int row, double sub)
{
for (int j = 0; j < this.ReducedMatrix[0].Length; j++)
if (this.ReducedMatrix[row][j] == -1)
continue;
else
this.ReducedMatrix[row][j] -= sub;
}
private void SubtractRow(int row, double sub, ref double[][] temp)
{
for (int j = 0; j < temp[0].Length; j++)
if (temp[row][j] == -1)
continue;
else
temp[row][j] -= sub;
}
private void SubtractCol(int col, double sub)
{
for (int j = 0; j < this.ReducedMatrix[0].Length; j++)
if (this.ReducedMatrix[j][col] == -1)
continue;
else
this.ReducedMatrix[j][col] -= sub;
}
private void SubtractCol(int col, double sub, ref double[][] temp)
{
for (int j = 0; j < temp.Length; j++)
if (temp[j][col] == -1)
continue;
else
temp[j][col] -= sub;
}
Thanks in advance

Converting Codility solution to C# (Grocery-store, Hydrogenium 2013)

In order to learn and understand how Dijkstra's algorithm is used to solve the "Grocery Store" ([Hydrogenium 2013]: https://codility.com/programmers/challenges/hydrogenium2013) problem on codility, I'm trying to rewrite the #2, O(n^2) solution(https://codility.com/media/train/solution-grocery-store.pdf) in C#.
1) What language are those solutions written in?
2) What would be the C# equivalent to this bit of code?
G = [[]] *N
for i in xrange(M):
G[A[i]] = G[A[i]] + [(B[i], C[i])]
G[B[i]] = G[B[i]] + [(A[i], C[i])]
This is what I have so far
int[] G = new int[N];
for (int i = 0; i < M; i++)
{
G[A[i]] = G[A[i]];
G[B[i]] = G[B[i]];
}
Thanks in advance,
Gregory
Downloaded IDLE (a python IDE...kind of) and figured it out. It appears to be adding pairs to each array element. Here's the code I came up with if anyone else happens to stumble across the same problem.
private struct nodePair
{
public int node;
public int time;
public nodePair(int node, int time)
{
this.node = node;
this.time = time;
}
}
public int solution(int[] A, int[] B, int[] C, int[] D)
{
int M = A.Length;
int N = D.Length;
//build the graph
List<nodePair>[] G = new List<nodePair>[N];
for (int i = 0; i < N; i++)
{
G[i] = new List<nodePair>();
}
for (int i = 0; i < M; i++)
{
G[A[i]].Add(new nodePair(B[i], C[i]));
G[B[i]].Add(new nodePair(A[i], C[i]));
}
//initialize the distance table
int[] dist = new int[N];
for (int i = 0; i < N; i++)
{
dist[i] = int.MaxValue;
}
bool[] visited = new bool[N];
for (int i = 0; i < N; i++)
{
visited[i] = false;
}
//look for the minimum value
int ii = 0; ;
dist[0] = 0;
for (int k = 0; k < N; k++)
{
int s = int.MaxValue;
//find the minimum
for (int j = 0; j < N; j++)
{
if ((dist[j] < s) && (visited[j] == false))
{
s = dist[j];
ii = j;
}
}
visited[ii] = true;
if (s < D[ii])
{
return s;
}
List<nodePair> thisNodeLIst = G[ii];
foreach (nodePair oneNode in thisNodeLIst)
{
dist[oneNode.node] = Math.Min(dist[oneNode.node], s + oneNode.time);
}
}//for
return -1;
}
}

Confused about how to fill an array

I would like to fill out a 2-dimensional array randomly .But I dont know how to assign .This is what i have yet
Random rnd = new Random();
int x = rnd.Next(0, 3);
int y = rnd.Next(0, 3);
int[,] array=new int[2,2];
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
?? I have no idea
}
}
try this.
Random rnd = new Random();
int[,] array=new int[2,2];
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
array[i,j] = rnd.Next(0,100);
}
}
You can do it like this. But i wonder why you are generating 2 random numbers
Random rnd = new Random();
int x = rnd.Next(0, 3);
int y = rnd.Next(0, 3);
int[,] array=new int[2,2];
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
array[i,j] = yourvalue;
}
}

Categories

Resources