forked from Jay-Jay-D/LeanOptimization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChromosome.cs
63 lines (50 loc) · 1.61 KB
/
Chromosome.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using GeneticSharp.Domain.Chromosomes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Optimization
{
public class Chromosome : ChromosomeBase
{
GeneConfiguration[] _config;
bool _isActual;
public Chromosome(bool isActual, GeneConfiguration[] config) : base(config.Length)
{
_isActual = isActual;
_config = config;
for (int i = 0; i < _config.Length; i++)
{
ReplaceGene(i, GenerateGene(i));
}
}
public override Gene GenerateGene(int geneIndex)
{
var item = _config[geneIndex];
return GeneFactory.Generate(item, _isActual);
}
public override IChromosome CreateNew()
{
return new Chromosome(false, GeneFactory.Config);
}
public override IChromosome Clone()
{
var clone = base.Clone() as Chromosome;
return clone;
}
public Dictionary<string, object> ToDictionary()
{
return this.GetGenes().ToDictionary(d => ((KeyValuePair<string, object>)d.Value).Key, d => ((KeyValuePair<string, object>)d.Value).Value);
}
public string ToKeyValueString()
{
StringBuilder output = new StringBuilder();
foreach (var item in this.ToDictionary())
{
output.Append(item.Key).Append(": ").Append(item.Value.ToString()).Append(", ");
}
return output.ToString().TrimEnd(',', ' ');
}
}
}