NSGA:assigned_fitness negative values - c#

I have a problem with NSGA. When I have 100 pop, 2 objectives, sigma_share=0.01, epsilon=0.22 and run N times with NSGA then the assigned_fitness values are not negative.
When I have above 200 pop, 2 objectives, sigma_share=0.01, epsilon=0.22 and run with N=5 or N=6 times, then the assigned_fitness and shared_fitness are negative values.
How do I fix this?
public void Compute_Assigned_fitness()
{
//Np is 200, epsilon=0.22
double Fmin = this.Np + epsilon;
double fmin_temp=Fmin;
for (int i = 0; i < Front_id.Count; i++)
{
for (int j = 0; j < Front_id[i].Front_item.Count; j++)
{
Front_id[i].Front_item[j].Assigned_fitness = Fmin - epsilon;
double niche = 0;
for (int k = 0; k < Front_id[i].Front_item.Count; k++)
{
double d = Front_id[i].Front_item[j].distances_to(Front_id[i].Front_item[k]);
if (d <= sigma_share)
{
double shd = 1 - (d / sigma_share) * (d / sigma_share);
niche += shd;
}
Front_id[i].Front_item[j].Niche = niche;
}
Front_id[i].Front_item[j].Shared_fitness = Front_id[i].Front_item[j].Assigned_fitness / niche;
niche = 0;
if (fmin_temp > Front_id[i].Front_item[j].Shared_fitness)
fmin_temp = Front_id[i].Front_item[j].Shared_fitness;
}
Fmin = fmin_temp;
}
for (int i = 0; i < Front_id.Count; i++)
{
for (int j = 0; j < Front_id[i].Front_item.Count; j++)
{
P.Add(Front_id[i].Front_item[j]);
}
}
}

Related

Trying to perform 2D Cross correlation on 2 images without using Matlab/Numpy/etc

I am trying to perform Cross Correlation on these 2 images in C#:
Image, Template
Matlab says the result is supposed to look like: Matlab result
But this is my result: My result
Here is my Cross Correlation function:
public static Signal2D CrossCorrelation2D(Signal2D signal, Signal2D pulse) {
return InverseFFT2D(FFT2D(signal) * FFT2D(pulse).GetConjugate());
}
Here is my FFT2D:
public static Signal2D FFT2D(Signal2D signal) {
Signal2D result = new Signal2D(signal.Height, signal.Width);
for (int i = 0; i < result.Height; i++)
result[i] = new ComplexNumber[signal[i].Length];
//rows
for (int n = 0; n < signal.Height; n++) {
result[n] = FFT(signal[n]);
}
//columns
for (int i = 0; i < signal[0].Length; i++) {
ComplexNumber[] col = new ComplexNumber[signal.Height];
for (int j = 0; j < col.Length; j++) {
col[j] = result[j][i];
}
col = FFT(col);
for (int j = 0; j < col.Length; j++) {
result[j][i] = col[j];
}
}
return result;
}
Here is my FFT:
public static Signal FFT(Signal signal) {
int N = signal.Length;
if (N == 1)
return signal;
if ((N & (N - 1)) != 0)
throw new ArgumentOutOfRangeException("signal length must be a power of 2");
Signal evenArr = new Signal(N / 2);
Signal oddArr = new Signal(N / 2);
for (int i = 0; i < N / 2; i++) {
evenArr[i] = signal[2 * i];
}
evenArr = FFT(evenArr);
for (int i = 0; i < N / 2; i++) {
oddArr[i] = signal[2 * i + 1];
}
oddArr = FFT(oddArr);
Signal result = new Signal(N);
for (int k = 0; k < N / 2; k++) {
double w = -2.0 * k * Math.PI / N;
ComplexNumber wk = new ComplexNumber(Math.Cos(w), Math.Sin(w));
ComplexNumber even = evenArr[k];
ComplexNumber odd = oddArr[k];
result[k] = even + (wk * odd);
result[k + N / 2] = even - (wk * odd);
}
return result;
}
Here is my Signal multiplication (using pointwise multiplication):
public static Signal2D operator* (Signal2D a, Signal2D b) {
if (a.Height != b.Height || a.Width != b.Width)
throw new ArgumentException("Sizes must be equal");
Signal2D result = new Signal2D(a.Height, a.Width);
for (int y = 0; y < a.Height; y++) {
for (int x = 0; x < a.Width; x++) {
result[y][x] = a[y][x] * b[y][x];
}
}
return result;
}
Any help is appreciated, thanks.
Edit: I left the matlab image at the original size of 1023 by 1023 and overlayed my result. It looks like I may already be at the result, I am just not sure how Matlab pads the image. Overlayed results (The red is the white part from my result, the grey is the black part from my result. Black/white is from Matlab)

complex valued neural network back propogation

i am working oncomplex valued neural networks, rvnn ok but when convert it to cvnn error is not decreases, and i cant figure out where is mistake, my code is here
public bool Train(List<Complex> input, List<Complex> output)
{
if ((input.Count != this.Layers[0].Neurons.Count) || (output.Count != this.Layers[this.Layers.Count - 1].Neurons.Count)) return false;
Run(input);
error = 0;
for(int i = 0; i < this.Layers[this.Layers.Count - 1].Neurons.Count; i++)
{
Neuron neuron = this.Layers[this.Layers.Count - 1].Neurons[i];
neuron.Delta = dSigmoid(neuron.Value) * (output[i] - neuron.Value);
error += (output[i] - neuron.Value).Magnitude;
for(int j = this.Layers.Count - 2; j > 2; j--)
{
for(int k = 0; k < this.Layers[j].Neurons.Count; k++)
{
Neuron n = this.Layers[j].Neurons[k];
n.Delta = dSigmoid(n.Value)*
this.Layers[j + 1].Neurons[i].Dendrites[k].Weight *
this.Layers[j + 1].Neurons[i].Delta;
}
}
}
for(int i = this.Layers.Count - 1; i > 1; i--)
{
for(int j=0; j < this.Layers[i].Neurons.Count; j++)
{
Neuron n = this.Layers[i].Neurons[j];
n.Bias = n.Bias + (this.LearningRate * n.Delta);
for (int k = 0; k < n.Dendrites.Count; k++)
n.Dendrites[k].Weight = n.Dendrites[k].Weight + (this.LearningRate * this.Layers[i - 1].Neurons[k].Value * n.Delta);
}
}
return true;
}

How to improve nested loop in C#?

I have a function to calculate the costMatrix like this code below.
public double[,] makeCostMatrixClassic(List<PointD> firstSeq, List<PointD> secondSeq)
{
double[,] costMatrix = new double[firstSeq.Count, secondSeq.Count];
costMatrix[0, 0] = Math.Round(this.getEuclideanDistance(firstSeq[0], secondSeq[0]), 3);
// For D(n,1)
for (int i = 0; i < firstSeq.Count; i++)
{
for (int j = 0; j <= i; j++)
{
costMatrix[i, 0] += Math.Round(this.getEuclideanDistance(firstSeq[j], secondSeq[0]), 3);
}
}
// For D(1,m)
for (int i = 0; i < secondSeq.Count; i++)
{
for (int j = 0; j <= i; j++)
{
costMatrix[0, i] += Math.Round(this.getEuclideanDistance(firstSeq[0], secondSeq[j]), 3);
}
}
// For D(n,m)
for (int i = 1; i < firstSeq.Count; i++)
{
for (int j = 1; j < secondSeq.Count; j++)
{
double min = this.findMin(costMatrix[i - 1, j - 1], costMatrix[i - 1, j], costMatrix[i, j - 1]);
costMatrix[i, j] = min + Math.Round(this.getEuclideanDistance(firstSeq[i], secondSeq[j]), 3);
}
}
return costMatrix;
}
For the 3rd loop (n,m), how could i improve its performance if the "count" of each Sequence is 1 million points.

C# Recursion Logic

I am attempting to write a function in which the number of nested loops is dependent upon an integer (numStroke) passed into it. For example, when numStrokes is 1, the code executed should be:
double checkProfitability(GameState state, int numStrokes)
{
double[] possiblePayoffs = new double[50000];
int pPIndex = 0;
double sumOfPayoffs = 0;
double averagePayoff = 0;
for (int i = 0; i <= 5; i++)
{
// Populate possiblePayoffs[]
}
for (int ii = 0; ii < pPIndex; ii++)
{
sumOfPayoffs += possiblePayoffs[i];
}
averagePayoff = sumOfPayoffs / pPIndex;
return averagePayoff;
}
When numStrokes is 3, it should be:
double checkProfitability(GameState state, int numStrokes)
{
double[] possiblePayoffs = new double[50000];
int pPIndex = 0;
double sumOfPayoffs = 0;
double averagePayoff = 0;
for (int i = 0; i <= 5; i++)
{
state.colors[i]++;
for (int j = 0; j <= 5; j++)
{
state.colors[j]++;
for (int k = 0; k <= 5; k++)
{
// Populate possiblePayoffs[]
}
state.colors[j]--;
}
state.colors[i]--;
}
for (int ii = 0; ii < pPIndex; ii++)
{
sumOfPayoffs += possiblePayoffs[i];
}
averagePayoff = sumOfPayoffs / pPIndex;
return averagePayoff;
}
Linked is the extra example of when numStrokes is 6, just in case what I'm trying to do isn't already clear:
http://hastebin.com/hemolikodo.avrasm
So far, I have come up with the following attempt to implement numStrokes amount of nested loops, but it does not work (if for no other reason, because the function tries to create another copy of int i when it calls itself recursively). I'm not sure if this code is even the right approach. I'm not even certain that I should be trying to do this recursively. I considered just using a giant if statement that executes code based on the value of numStrokes, but a more general implementation seemed preferable.
double checkProfitability(GameState state, int numStrokes, int i)
{
double[] possiblePayoffs = new double[50000];
int pPIndex = 0;
double sumOfPayoffs = 0;
double averagePayoff = 0;
if (numStrokes == 0)
{
// Populate possiblePayoffs[]
}
else
{
for (int i = 0; i <= 5 && numStrokes >= 1; i++)
{
checkProfitability(state, --numStrokes, i);
}
}
for (int ii = 0; ii < pPIndex; ii++)
{
sumOfPayoffs += possiblePayoffs[ii];
}
averagePayoff = sumOfPayoffs / pPIndex;
richTextBox1.Text = averagePayoff.ToString();
return averagePayoff;
}
Can anyone explain how to implement this properly?
Edit: The problem is that I don't know how many nested loops I need until run time.
for (int i = 0; i < Math.Pow(6, numStrokes); i++)
{
int innerForIndex = i;
for (int j = 0; j < numStrokes; j++)
{
colors[innerForIndex % 6]++;
innerForIndex /= 6;
}
//populate your possiblePayoffs[]
innerForIndex = i;
for (int j = 0; j < numStrokes; j++)
{
colors[innerForIndex % 6]--;
innerForIndex /= 6;
}
}
numStrokes for loops from 0 to 5 inclusive means you have total Math.Pow(6, numStrokes) elements. You use your inner loops indexes to increment/decrement some cololrs array. Those indexes can be easily calculated from element number. For numStroke == 3 example k can be calculated as innerForIndex % 6, j as (innerForIndex / 6) % 6, i as ((innerForIndex / 6) / 6) % 6.
This is the closest I think I can get you to a solution for this.
First up this is the checkProfitability method:
double checkProfitability(GameState state, int numStrokes)
{
var possiblePayoffs = new double[50000];
computePayoffs(state, possiblePayoffs, Enumerable.Empty<int>(), numStrokes);
var averagePayoff = possiblePayoffs.Select(x => (double)x).Average();
richTextBox1.Text = averagePayoff.ToString();
return averagePayoff;
}
The recursion is now in the computePayoffs method:
void computePayoffs(GameState state, int[] possiblePayoffs,
IEnumerable<int> values, int numStrokes)
{
if (numStrokes == 0)
{
// Populate possiblePayoffs[]
}
else
{
for (int i = 0; i <= 5; i++)
{
state.colors[i]++;
computePayoffs(
state,
possiblePayoffs,
values.Concat(new [] { i }),
numStrokes - 1);
state.colors[i]--;
}
}
}
for (int i = 0; i <= 5 * numstrokes; i++)
{
// Populate possiblePayoffs[]
if(i % 5 == 0)
{
//start of next loop
}
}
Why not do this?
The question is not clear. But I think recursion will help you to solve these type of cases. What i could understand is you need to do some looping numstocks*6(The below code will loop this much time. If this is the case The code will be structured as(Didn't test it. May need some minor modifications)
double checkProfitability(GameState state, int numStrokes)
{
if(numStrokes!=0)
{
for (int i = 0; i <= 5; i++)
{
checkProfitability(state,numStrokes-1);
}
}
//your code //calls this code numStrokes*6 times
}
More over beware of stackoverflow Exception also

Training ff nn using backpropagation

I have encountered some difficulties while training my nn. When I use, lets say, 10 training sets, at the end of training procces neural network is trained just for the last two. I'm entering same values that I have used to train network and I am getting wrong results save for the last two. It seems to me that new nn memory suppresses older memory. I'm using 64 input neurons, 42 neurons in hidden layer and one output neuron. Sigmoid function is used for activating neurons. Training inputs and expected outputs are in 0 to 1 range. Does anyone have any clue what might be causing the problem?
Neuron b = new Neuron();
Fft f = new Fft();
float e = 2.71828f;
float eta = 0.05f;
float alpha = 0.05f;
float[] saw = new float[42];
float[] dh = new float[42];
float error = 0;
float dto = 0;
Random broj = new Random();
TextReader br = new StreamReader("d:/trening.txt");
TextReader ir = new StreamReader("d:\\input.txt");
float NextFloat(Random rng, float min, float max)
{
return (float)(min + (rng.NextDouble() * (max - min)));
}
public void load()//load memory
{
int i, j;
byte[] floatBytes;
BinaryReader br = new BinaryReader(File.Open("d:/memorija.txt", FileMode.Open));
for (j = 0; j <= 41; j++)
{
for (i = 0; i <= 64; i++)
{
floatBytes = br.ReadBytes(4);
b.w12[i][j] = BitConverter.ToSingle(floatBytes, 0);
}
}
for (j = 0; j <= 1; j++)
{
for (i = 0; i <= 41; i++)
{
floatBytes = br.ReadBytes(4);
b.w23[i][j] = BitConverter.ToSingle(floatBytes, 0);
}
}
br.Close();
}
public void trening()//Get training inputs and expected outputs
{ //Calls process methode
int i, n,ct=0;
using (TextReader tr = new StreamReader("d:/trening.txt"))
{
do
{
ct++;
} while (tr.ReadLine() != null);
tr.Close();
}
for (n = 0; n < (ct-1)/65; n++)
{
for (i = 1; i <= 65; i++)
b.input[i] = Convert.ToSingle(br.ReadLine());
process(b.input[65]);
target.Text = ((b.input[65]).ToString());
}
}
public void process(double t)//Trains nn using backpropagation
{
error = 0;
do
{
int i, j, k;
BinaryWriter bw = new BinaryWriter(File.Open("d:\\memorija.txt", FileMode.Create));
i = k = j = 0;
for (j = 1; j <= 41; j++)
{
b.ulaz2[j] = b.w12[0][j];
for (i = 1; i <= 64; i++)
{
b.ulaz2[j] += b.input[i] * b.w12[i][j];
} b.izlaz2[j] = (float)(1.0 / (1.0 + Math.Pow(e, -b.ulaz2[j])));
if (b.izlaz2[j] < 0)
MessageBox.Show(b.izlaz2[j].ToString());
}
for (k = 1; k <= 1; k++)
{
b.ulaz3 = b.w23[0][k];
for (j = 1; j <= 41; j++)
{
b.ulaz3 += b.izlaz2[j] * b.w23[j][k];
} b.izlaz = (float)(1.0 / (1.0 + Math.Pow(e, -b.ulaz3)));
error += (float)(0.5 * (t - b.izlaz) * (t - b.izlaz));
dto = (float)(t - b.izlaz) * b.izlaz * (1 - b.izlaz);
}
for (j = 1; j <= 41; j++)
{
saw[j] = 0;
for (k = 1; k <= 1; k++)
{
saw[j] += dto * b.izlaz2[j];
} dh[j] = saw[j] * b.izlaz2[j] * (1 - b.izlaz2[j]);
}
for (j = 1; j <= 41; j++)
{
b.w12d[0][j] = eta * dh[j] + alpha * b.w12d[0][j];
b.w12[0][j] += b.w12d[0][j];
for (i = 1; i <= 64; i++)
{
b.w12d[i][j] = eta * b.input[i] * dh[j] + alpha * b.w12d[i][j];
b.w12[i][j] += b.w12d[i][j];
}
}
for (k = 1; k <= 1; k++)
{
b.w23d[0][k] = eta * dto + alpha * b.w23d[0][k];
b.w23[0][k] += b.w23d[0][k];
for (j = 1; j <= 41; j++)
{
b.w23d[j][k] = eta * b.izlaz2[j] * dto + alpha * b.w23d[j][k];
b.w23[j][k] += b.w23d[j][k];
}
}
for (j = 0; j <= 41; j++)
{
for (i = 0; i <= 64; i++)
bw.Write(b.w12[i][j]);
}
for (j = 0; j <= 1; j++)
{
for (i = 0; i <= 41; i++)
bw.Write(b.w23[i][j]);
}
bw.Close();
izlazb.Text = Convert.ToString(b.izlaz);
errorl.Text = Convert.ToString(Math.Abs(b.izlaz - b.input[64]));
} while (Math.Abs(b.izlaz - t) > 0.03);
}
public void test()//This methode gets input values and gives output based on previous training
{
int i = 0, j = 0, k = 0;
for (i = 1; i < 65; i++)
b.input[i] = (float)Convert.ToDouble(ir.ReadLine());
for (j = 1; j <= 41; j++)
{
b.ulaz2[j] = b.w12[0][j];
for (i = 1; i <= 64; i++)
{
b.ulaz2[j] += b.input[i] * b.w12[i][j];
} b.izlaz2[j] = (float)(1.0 / (1.0 + Math.Pow(e, -b.ulaz2[j])));
}
for (k = 1; k <= 1; k++)
{
b.ulaz3 = b.w23[0][k];
for (j = 1; j <= 41; j++)
{
b.ulaz3 += b.izlaz2[j] * b.w23[j][k];
} b.izlaz = (float)(1.0 / (1.0 + Math.Pow(e, -b.ulaz3)));
} izlazb.Text = Convert.ToString(b.izlaz);
target.Text = "/";
errorl.Text = "/";
}
public void reset()//Resets memory
{
BinaryWriter fw = new BinaryWriter(File.Open("d:\\memorija.txt", FileMode.Create));
int i = 0;
int j = 0;
Random broj = new Random();
for (j = 0; j <= 41; j++)
{
for (i = 0; i <= 64; i++)
{
b.w12[i][j] = 0;
b.w12[i][j] = 2 * (NextFloat(broj, -0.5f, 0.5f));
fw.Write(b.w12[i][j]);
}
}
for (j = 0; j <= 1; j++)
{
for (i = 0; i <= 41; i++)
{
b.w23[i][j] = 0;
b.w23[i][j] = 2 * (NextFloat(broj, -0.5f, 0.5f));
fw.Write(b.w23[i][j]);
}
}
fw.Close();
}
}
}
And neuron class
public class Neuron
{
public float[][] w12 = new float[65][];//(65, 42);
public float[][] w12d = new float[65][];//(65, 42);
public float[][] w23 = new float[42][];//(42,2);
public float[][] w23d = new float[42][];//(42, 2);
public float[] ulaz2 = new float[42];
public float[] izlaz2 = new float[42];
public float ulaz3;
public float[] input =new float[66];
public static float[] ioutput;
public float izlaz;
public void arrayInit()
{
int i, j;
for (i = 0; i <=64; i++)
{
w12[i] = new float[42];
w12d[i] = new float[42];
}
for (i = 0; i <42; i++)
{
w23[i] = new float[2];
w23d[i] = new float[2];
}
for (j = 0; j < 42; j++)
for (i = 0; i <=64; i++)
{
w12[i][j] = 0;
w12d[i][j] = 0;
}
for (j = 0; j < 2; j++)
for (i = 0; i < 42; i++)
{
w23[i][j] = 0;
w23d[i][j] = 0;
}
}
}
I found out what the problem was. I didn't mix training arrays, I was introducing one array to nn until it was trained for it, instead of introducing all arrays in cyclic manner. I hope this will be useful for someone.

Categories