M
Matt2234
Guest
Here, I have 4 classes, and one class "Program" derives from "GeneticAlgHW". I have 6 methods to call in main which is in the base class, but everything I have tried does not seem to work.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace Genetic.HW
{
using ga_vect = List<ga_struct>;
public class ga_struct
{
public ga_struct(string str, uint fitness)
{
Str = str;
Fitness = fitness;
}
public string Str {get; set;}
public uint Fitness {get; set;}
}
public abstract class GeneticAlgHW
{
public Random random = new Random((int)DateTime.Now.Ticks);
public void Main(string[] args)
{
ga_vect popAlpha = new ga_vect();
ga_vect popBeta = new ga_vect();
init_Population(ref popAlpha, ref popBeta);
ga_vect Population = popAlpha;
ga_vect buffer = popBeta;
for (int i = 0; i < MyConstants.GA_MAXITER; i++)
{
calc_fitness(ref Population);
fitness_sort(ref Population);
PrintBest(ref Population);
if (Population[0].Fitness == 0) break;
Mate(ref Population, ref buffer);
Swap(ref Population, ref buffer);
}
Console.ReadKey();
}
}
public class MyConstants
{
public const int GA_POPSIZE = 500;
public const int GA_MAXITER = 16384;
public const float GA_ELITRATE = 0.10f;
public const float GA_MUTATIONRATE = 0.25f;
public const float GA_MUTATION = 32767 * GA_MUTATIONRATE;
public const string GA_TARGET = ("Hello world!");
// public List<ga_struct> ga_vect;
}
public class Program : GeneticAlgHW
{
void Swap(ref ga_vect Population, ref ga_vect buffer)
{
var temp = Population;
Population = buffer;
buffer = temp;
}
void init_Population( ref ga_vect Population, ref ga_vect buffer)
{
string SGA = MyConstants.GA_TARGET;
int tsize = SGA.Length;
for (int i = 0;i < MyConstants.GA_POPSIZE; i++)
{
var citizen = new ga_struct(string.Empty, 0);
for(int j = 0; j < tsize; j++)
citizen.Str += Convert.ToChar(random.Next(90) + 32);
Population.Add(citizen);
buffer.Add(new ga_struct(string.Empty, 0));
}
}
void calc_fitness(ref ga_vect Population)
{
string target = MyConstants.GA_TARGET;
int tsize = target.Length;
for(int i = 0; i < MyConstants.GA_POPSIZE; i++)
{
uint fitness = 0;
for (int j = 0; j < tsize; i++)
{
fitness += (uint)Math.Abs(Population.Str[j] - target[j]);
}
Population.Fitness = fitness;
}
}
int fitness_sort(ga_struct x, ga_struct y)
{
return x.Fitness.CompareTo(y.Fitness);
}
void sort_by_fitness(ref ga_vect Population)
{
Population.Sort((x, y) => fitness_sort(x , y));
}
void elitism(ref ga_vect Population, ref ga_vect buffer, int esize)
{
for (int i = 0; i < esize; i++)
{
buffer.Str = Population.Str;
buffer.Fitness =Population.Fitness;
}
}
void Mutate(ref ga_struct member)
{
int tsize = MyConstants.GA_TARGET.Length;
int ipos = random.Next(tsize);
int delta = random.Next(90) + 32;
var mutated = member.Str.ToCharArray();
Convert.ToChar((member.Str[ipos] + delta)%123).ToString().CopyTo(0, mutated, ipos, 1);
member.Str = mutated.ToString();
}
void Mate(ref ga_vect Population, ref ga_vect buffer)
{
const int esize = (int)(MyConstants.GA_POPSIZE * MyConstants.GA_ELITRATE);
int tsize = MyConstants.GA_TARGET.Length, spos, i1, i2;
elitism(ref Population, ref buffer, esize);
for (int i = esize; i < MyConstants.GA_POPSIZE; i++)
{
i1 = random.Next(MyConstants.GA_POPSIZE / 2);
i2 = random.Next(MyConstants.GA_POPSIZE / 2);
spos = random.Next(tsize);
buffer.Str = Population[i1].Str.Substring(0, spos) + Population[i2].Str.Substring(spos, tsize - spos);
if (random.Next() < MyConstants.GA_MUTATION)
{
var mutated = buffer;
Mutate(ref mutated);
buffer = mutated;
}
}
}
void PrintBest(ref ga_vect gav)
{
Console.WriteLine("Best: " + gav[0].Str + " (" + gav[0].Fitness + ")");
}}}
Continue reading...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace Genetic.HW
{
using ga_vect = List<ga_struct>;
public class ga_struct
{
public ga_struct(string str, uint fitness)
{
Str = str;
Fitness = fitness;
}
public string Str {get; set;}
public uint Fitness {get; set;}
}
public abstract class GeneticAlgHW
{
public Random random = new Random((int)DateTime.Now.Ticks);
public void Main(string[] args)
{
ga_vect popAlpha = new ga_vect();
ga_vect popBeta = new ga_vect();
init_Population(ref popAlpha, ref popBeta);
ga_vect Population = popAlpha;
ga_vect buffer = popBeta;
for (int i = 0; i < MyConstants.GA_MAXITER; i++)
{
calc_fitness(ref Population);
fitness_sort(ref Population);
PrintBest(ref Population);
if (Population[0].Fitness == 0) break;
Mate(ref Population, ref buffer);
Swap(ref Population, ref buffer);
}
Console.ReadKey();
}
}
public class MyConstants
{
public const int GA_POPSIZE = 500;
public const int GA_MAXITER = 16384;
public const float GA_ELITRATE = 0.10f;
public const float GA_MUTATIONRATE = 0.25f;
public const float GA_MUTATION = 32767 * GA_MUTATIONRATE;
public const string GA_TARGET = ("Hello world!");
// public List<ga_struct> ga_vect;
}
public class Program : GeneticAlgHW
{
void Swap(ref ga_vect Population, ref ga_vect buffer)
{
var temp = Population;
Population = buffer;
buffer = temp;
}
void init_Population( ref ga_vect Population, ref ga_vect buffer)
{
string SGA = MyConstants.GA_TARGET;
int tsize = SGA.Length;
for (int i = 0;i < MyConstants.GA_POPSIZE; i++)
{
var citizen = new ga_struct(string.Empty, 0);
for(int j = 0; j < tsize; j++)
citizen.Str += Convert.ToChar(random.Next(90) + 32);
Population.Add(citizen);
buffer.Add(new ga_struct(string.Empty, 0));
}
}
void calc_fitness(ref ga_vect Population)
{
string target = MyConstants.GA_TARGET;
int tsize = target.Length;
for(int i = 0; i < MyConstants.GA_POPSIZE; i++)
{
uint fitness = 0;
for (int j = 0; j < tsize; i++)
{
fitness += (uint)Math.Abs(Population.Str[j] - target[j]);
}
Population.Fitness = fitness;
}
}
int fitness_sort(ga_struct x, ga_struct y)
{
return x.Fitness.CompareTo(y.Fitness);
}
void sort_by_fitness(ref ga_vect Population)
{
Population.Sort((x, y) => fitness_sort(x , y));
}
void elitism(ref ga_vect Population, ref ga_vect buffer, int esize)
{
for (int i = 0; i < esize; i++)
{
buffer.Str = Population.Str;
buffer.Fitness =Population.Fitness;
}
}
void Mutate(ref ga_struct member)
{
int tsize = MyConstants.GA_TARGET.Length;
int ipos = random.Next(tsize);
int delta = random.Next(90) + 32;
var mutated = member.Str.ToCharArray();
Convert.ToChar((member.Str[ipos] + delta)%123).ToString().CopyTo(0, mutated, ipos, 1);
member.Str = mutated.ToString();
}
void Mate(ref ga_vect Population, ref ga_vect buffer)
{
const int esize = (int)(MyConstants.GA_POPSIZE * MyConstants.GA_ELITRATE);
int tsize = MyConstants.GA_TARGET.Length, spos, i1, i2;
elitism(ref Population, ref buffer, esize);
for (int i = esize; i < MyConstants.GA_POPSIZE; i++)
{
i1 = random.Next(MyConstants.GA_POPSIZE / 2);
i2 = random.Next(MyConstants.GA_POPSIZE / 2);
spos = random.Next(tsize);
buffer.Str = Population[i1].Str.Substring(0, spos) + Population[i2].Str.Substring(spos, tsize - spos);
if (random.Next() < MyConstants.GA_MUTATION)
{
var mutated = buffer;
Mutate(ref mutated);
buffer = mutated;
}
}
}
void PrintBest(ref ga_vect gav)
{
Console.WriteLine("Best: " + gav[0].Str + " (" + gav[0].Fitness + ")");
}}}
Continue reading...