-
Notifications
You must be signed in to change notification settings - Fork 2
/
types.ts
206 lines (173 loc) · 4.6 KB
/
types.ts
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
export type SelectionStrategies =
| "elite"
| "random"
| "tournament"
| "roulette"
| "stochasticUniversalSampling"
| "boltzmannSelection"
| "rank";
export type CrossoverStrategies =
| "singlePoint"
| "orderOne"
| "uniform"
| "wholeArithmetic"
| "partiallyMatched";
export type MutationStrategies =
| "shuffle"
| "flipAll"
| "flipSome"
| "scramble"
| "scrambleSlice"
| "gaussian";
export type ReinsertionStrategies =
| "pure"
| "elitist";
export type SortDirection = "ASC" | "DESC";
export interface InitializeOptions {
populationSize: number;
}
export interface EvolveOptions<T>
extends
Partial<InitializeOptions>,
Partial<SelectionOptions>,
Partial<FitnessFnOptions>,
Partial<CrossoverOptions<T>>,
Partial<MutationOptions<T>>,
Partial<ReinsertionFnOptions<T>> {
coolingRate?: number;
}
export interface RunOptions<T>
extends
Partial<InitializeOptions>,
Partial<Omit<EvolveOptions<T>, "populationSize">> {
}
export type Genotype<T> = ReadonlyArray<T>;
export type Chromosome<T> = {
genes: Genotype<T>;
size: number;
fitness: number;
age: number;
};
export type PopulationTuple<T> = [Chromosome<T>, Chromosome<T>];
export type Parents<T> = Array<PopulationTuple<T>>;
export type Population<T> = Array<Chromosome<T>>;
export type GenotypePromise<T> = Promise<Genotype<T>>;
export type ChromosomePromise<T> = Promise<Chromosome<T>>;
export interface GetGenotypeFn<T> {
(counter: number): GenotypePromise<T>;
}
export type PopulationPromise<T> = Promise<Population<T>>;
export type FitnessScorePromise = Promise<number>;
export interface FitnessFn<T> {
(genotype: Chromosome<T>): FitnessScorePromise;
}
export interface FitnessFnOptions {
fitnessSortDirection: SortDirection;
}
export interface SelectFn<T> {
(
population: Population<T>,
selectionOffset: number,
temperature: number,
): Population<T>;
}
export interface CrossoverFn<T> {
(
p1: Chromosome<T>,
p2: Chromosome<T>,
options?: CrossoverOptions<T>,
): PopulationTuple<T>;
}
export interface RepairChromosomeFn<T> {
(chromosome: Chromosome<T>): Chromosome<T>;
}
export interface SelectionOptions {
selectionType?: SelectionStrategies;
selectionRate?: number;
}
export interface SelectionResponse<T> {
parents: Parents<T>;
leftovers: Population<T>;
}
export interface ScrambleSizeFn<T> {
(chromosome: Chromosome<T>): number;
}
export interface MutationOptions<T> extends
Pick<
RandomNumberHelpers,
| "randomNumberGenerator"
| "universalNumberGenerator"
| "randomGaussian"
> {
mutationRate?: number;
genMutationRate?: number;
mutationType?: MutationStrategies;
mutationFn?: MutationFn<T>;
scrambleSize?: number | ScrambleSizeFn<T>;
}
export interface MutationFn<T> {
(chromosome: Chromosome<T>, options?: MutationOptions<T>): Chromosome<T>;
}
interface PopulationSizeLimitFn<T> {
(population: Population<T>): number;
}
export interface ReinsertionFnOptions<T> {
reinsertionType?: ReinsertionStrategies;
survivalRate?: number;
survivorCount?: number;
reinsertionFn?: ReinsertionFn<T>;
fitnessSortDirection?: SortDirection;
maxPopulation?: number;
maxPopulationFn?: PopulationSizeLimitFn<T>;
}
export interface ReinsertionFn<T> {
(
parents: Population<T>,
offspring: Population<T>,
leftover: Population<T>,
options?: ReinsertionFnOptions<T>,
): Population<T>;
}
export interface CrossoverStrategyOptions extends
Pick<
RandomNumberHelpers,
| "randomNumberGenerator"
| "universalNumberGenerator"
| "triangularRandomNumber"
> {
crossoverRate?: number;
crossoverAlpha?: number;
}
export interface CrossoverOptions<T> extends CrossoverStrategyOptions {
crossoverType?: CrossoverStrategies;
crossoverRepairFn?: RepairChromosomeFn<T>;
}
export interface Logger {
(message: string): void;
}
export interface TerminateFn<T> {
(population: Population<T>, generation: number, temperature: number): boolean;
}
export interface Problem<T> {
getGenotype: GetGenotypeFn<T>;
fitnessFn: FitnessFn<T>;
shouldTerminate: TerminateFn<T>;
}
export interface RandomNumberGenerator {
(max: number): number;
}
export interface UniversalNumberGenerator {
(): number;
}
export interface TriangularNumberGenerator {
(biasNumber: number, min: number, max: number): number;
}
export interface GausianNumberGenerator {
(mean: number, sigma: number): number;
}
export interface RandomNumberHelpers {
randomNumberGenerator?: RandomNumberGenerator;
randomGaussian?: GausianNumberGenerator;
universalNumberGenerator?: UniversalNumberGenerator;
triangularRandomNumber?: TriangularNumberGenerator;
}