blob: a6617dea60b0860180038f331096f9525ea8fabe (
plain)
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
64
65
66
67
68
|
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace Landfall.AI;
public class Population
{
private List<Genome> m_genomes = new List<Genome>();
public int Size => m_genomes.Count;
public Population(int size)
: this(size, init: true)
{
}
public Population(int size, bool init)
{
for (int i = 0; i < size; i++)
{
m_genomes.Add(null);
}
if (init)
{
for (int j = 0; j < size; j++)
{
Genome genome = new Genome();
genome.GenerateIndividual();
m_genomes[j] = genome;
}
}
}
public Genome GetGenome(int index)
{
return m_genomes[index];
}
public Genome GetRandomGenome()
{
return m_genomes[Random.Range(0, m_genomes.Count)];
}
public void SetGenome(int index, Genome g)
{
m_genomes[index] = g;
}
public Genome GetFittest()
{
Genome genome = m_genomes[0];
for (int i = 1; i < m_genomes.Count; i++)
{
Genome genome2 = m_genomes[i];
if (genome2.Fitness > genome.Fitness)
{
genome = genome2;
}
}
return genome;
}
public void Sort()
{
m_genomes.OrderBy((Genome g) => g.Fitness);
}
}
|