本文整理汇总了C#中Chromosome类的典型用法代码示例。如果您正苦于以下问题:C# Chromosome类的具体用法?C# Chromosome怎么用?C# Chromosome使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Chromosome类属于命名空间,在下文中一共展示了Chromosome类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Population_GetBestChromosome
public void Population_GetBestChromosome()
{
List<City> cities = new List<City>();
cities.Add(new City(1, 1, "A"));
cities.Add(new City(2, 2, "B"));
cities.Add(new City(3, 3, "C"));
cities.Add(new City(4, 4, "D"));
cities.Add(new City(5, 5, "E"));
List<City> cities2 = new List<City>();
cities2.Add(new City(1, 1, "A"));
cities2.Add(new City(5, 5, "B"));
cities2.Add(new City(3, 3, "C"));
cities2.Add(new City(4, 4, "D"));
cities2.Add(new City(2, 2, "E"));
Population p = new Population();
Chromosome c = new Chromosome(cities);
Chromosome c2 = new Chromosome(cities2);
p.Add(c);
p.Add(c2);
Chromosome best = p.GetBestChromosome();
Assert.AreEqual(c, best);
}
示例2: Test_CompareTo
public void Test_CompareTo()
{
Chromosome c1 = new Chromosome("Hello, world!");
Assert.AreEqual(0, c1.GetFitness());
Chromosome c2 = new Chromosome("H5p&J;!l<X\\7l");
Assert.AreEqual(399, c2.GetFitness());
Chromosome c3 = new Chromosome("Vc;fx#QRP8V\\$");
Assert.AreEqual(297, c3.GetFitness());
Chromosome c4 = new Chromosome("t\\O`E_Jx$n=NF");
Assert.AreEqual(415, c4.GetFitness());
Assert.AreEqual(0, c1.CompareTo(c1));
Assert.IsTrue(c1.CompareTo(c2) < 0);
Assert.IsTrue(c1.CompareTo(c3) < 0);
Assert.IsTrue(c1.CompareTo(c4) < 0);
Assert.AreEqual(0, c2.CompareTo(c2));
Assert.IsTrue(c2.CompareTo(c1) > 0);
Assert.IsTrue(c2.CompareTo(c3) > 0);
Assert.IsTrue(c2.CompareTo(c4) < 0);
Assert.AreEqual(0, c3.CompareTo(c3));
Assert.IsTrue(c3.CompareTo(c1) > 0);
Assert.IsTrue(c3.CompareTo(c2) < 0);
Assert.IsTrue(c3.CompareTo(c4) < 0);
Assert.AreEqual(0, c4.CompareTo(c4));
Assert.IsTrue(c4.CompareTo(c1) > 0);
Assert.IsTrue(c4.CompareTo(c2) > 0);
Assert.IsTrue(c4.CompareTo(c3) > 0);
}
示例3: SpawnNeutralCell
void SpawnNeutralCell()
{
Chromosome outputChromosome = new Chromosome(NeutralType);
Vector2 RandomPosition = Random.insideUnitCircle * SpawnDistanceScale;
Vector3 spawnLocation = new Vector3(transform.position.x + RandomPosition.x, 0, transform.position.z + RandomPosition.y);
manager.SpawnNeutral(spawnLocation, outputChromosome);
}
示例4: EqualsObj_xEqualsy_and_yEqualsx_returns_xEqualsz
public void EqualsObj_xEqualsy_and_yEqualsx_returns_xEqualsz()
{
var x = new Chromosome();
var y = new Chromosome();
var z = new Chromosome();
Assert.IsTrue(!(x.Equals((object)y) && y.Equals((object)z)) || x.Equals((object)z));
}
示例5: ChromosomePair_CreateCrossoverChild
public void ChromosomePair_CreateCrossoverChild()
{
List<City> cities = new List<City>();
cities.Add(new City(1, 1, "A"));
cities.Add(new City(2, 2, "B"));
cities.Add(new City(3, 3, "C"));
cities.Add(new City(4, 4, "D"));
cities.Add(new City(5, 5, "E"));
List<City> cities2 = new List<City>();
cities2.Add(new City(1, 1, "A"));
cities2.Add(new City(5, 5, "B"));
cities2.Add(new City(3, 3, "C"));
cities2.Add(new City(4, 4, "D"));
cities2.Add(new City(2, 2, "E"));
Chromosome c = new Chromosome(cities);
Chromosome c2 = new Chromosome(cities2);
ChromosomePair pair = new ChromosomePair(c, c2);
Chromosome child = pair.CreateCrossoverChild();
Assert.AreNotEqual(c, child);
}
示例6: Reorder
//int maxLength; // maximum length of string to invert.
/// <summary>
/// Reorders the genes in the chromosome.
/// </summary>
/// <param name="c">The chromosome to be reordered.</param>
public static void Reorder(Chromosome c)
{
// This is like two-point crossover, in that we pick two
// random points on the chromosome. We then reverse the order
// of the genes contained between those two points.
int NumberOfGenes;
NumberOfGenes = c.Genes.Length;
int CrossoverPoint1 = Utils.Rand.Next( NumberOfGenes );
int CrossoverPoint2 = Utils.Rand.Next( NumberOfGenes );
if ( CrossoverPoint1 > CrossoverPoint2 )
{
int tmp = CrossoverPoint1;
CrossoverPoint1 = CrossoverPoint2;
CrossoverPoint2 = tmp;
}
for (
int i=CrossoverPoint1, j=CrossoverPoint2;
(i - CrossoverPoint1) < (CrossoverPoint2-CrossoverPoint1)/2;
i++,j--
)
{
Gene g = c.Genes[i];
c.Genes[i] = c.Genes[j];
c.Genes[j] = g;
}
}
示例7: Crossover
public Tuple<Chromosome, Chromosome> Crossover(Chromosome c1, Chromosome c2)
{
var locus = random.Next(0, c1.Value.Length + 1); // Locus: http://en.wikipedia.org/wiki/Locus_(genetics)
string ch1 = c1.Value.Substring(0, locus) + c2.Value.Substring(locus),
ch2 = c2.Value.Substring(0, locus) + c1.Value.Substring(locus);
return new Tuple<Chromosome, Chromosome>(new Chromosome(ch1, fitnessCalculator), new Chromosome(ch2, fitnessCalculator));
}
示例8: TestFitnessNoMatch
public void TestFitnessNoMatch()
{
Chromosome.SetTargetGene("ABCF");
Chromosome first = new Chromosome("ABCE");
System.Diagnostics.Debug.Assert(first.Fitness == 1);
}
示例9: Population
public Population(float target, IEnumerable<Chromosome> chromos, Random random, Chromosome solution, int generation)
{
this.target = target;
this.chromos = chromos.ToList();
this.solution = solution;
this.generation = generation;
this.random = random;
}
示例10: Mutate
public Chromosome Mutate(Chromosome ch)
{
var sb = new StringBuilder(ch.Value);
var randomPositon = random.Next(0, ch.Value.Length);
sb[randomPositon] = generator.GenerateCharacter();
return new Chromosome(sb.ToString(), fitnessCalculator);
}
示例11: CalculateFitness
public override double CalculateFitness(Chromosome ch)
{
var score = game.Start(ch.Replay);
//var filledSpaces = game.FilledSpaces();
// TODO: find a way to calculate a fitness based on the score and filledSpaces
return score;// (score + 1) * filledSpaces;
}
示例12: chromosomeToString
// Converts a chromosome to its respective sequence of cities' indexes.
public static string chromosomeToString(Chromosome chromosome)
{
string str = "";
foreach (var gene in chromosome.Genes)
{
str += ((int)gene.RealValue).ToString() + " ";
}
return str;
}
示例13: Mutate
public Chromosome Mutate(Chromosome ch)
{
var replay = ch.Replay.ToString();
var sb = new StringBuilder(replay);
var randomPositon = random.Next(0, replay.Length);
sb[randomPositon] = characterSet.GetRandomCharacter();
return new Chromosome(new Replay(sb.ToString()), fitnessCalculator);
}
示例14: RandomPopulation
public Population RandomPopulation()
{
var chromosomes = new Chromosome[ChromosomeCount];
for (int i = 0; i < ChromosomeCount; i++)
{
chromosomes[i] = RandomChromosome();
}
return new Population(chromosomes);
}
示例15: CreateInfectedCell
public void CreateInfectedCell(Chromosome _inputChromosome, Vector3 _location)
{
GameObject newCell = (GameObject)Instantiate(cell, _location, transform.rotation);
CellScript script = newCell.GetComponent<CellScript>();
script.manager = this;
script.CreateInfected(_inputChromosome);
infectedList.Add(script);
}