-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeneFitness.py
More file actions
57 lines (52 loc) · 2.34 KB
/
GeneFitness.py
File metadata and controls
57 lines (52 loc) · 2.34 KB
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
""" Set of classes to determine the fitness of a set of positions and velocities.
"""
import numpy as np
import logging
class MaxVelocity(object):
""" Defines the fitness as the number of particles at the detection plane
below a threshold velocity.
"""
def __init__(self, geneFlyer, optprops):
self.log = logging.getLogger(__name__)
self.log.info('Maximum velocity fitness function')
self.geneFlyer = geneFlyer
self.detectorPos = geneFlyer.flyer.detectionProps['position']
try:
self.targetspeed = optprops['targetspeed']
except KeyError:
self.log.critical('No target speed in OPTIMISER options')
raise RuntimeError('Missing fitness option')
def __call__(self, gene):
""" Fitness is the negative of the number of particles reaching the
detection plane below 1.04 the target velocity.
"""
pos, vel = self.geneFlyer.flyGene(gene)
ind = np.where((pos[:, 2] >= self.detectorPos) &
(vel[:, 2] < 1.04*self.targetspeed)
)[0]
return -len(ind)
class VelocityWindow(object):
""" Defines the fitness as the number of particles at the detection plane
within a range of velocities centred at targetspeed+-windowwidth.
"""
def __init__(self, geneFlyer, optprops):
self.log = logging.getLogger(__name__)
self.log.info('Velocity window fitness function')
self.geneFlyer = geneFlyer
self.detectorPos = geneFlyer.flyer.detectionProps['position']
try:
self.targetspeed = optprops['targetspeed']
self.windowwidth = optprops['windowwidth']
except KeyError:
self.log.critical('No target speed or windowwidth in OPTIMISER options')
raise RuntimeError('Missing fitness option')
def __call__(self, gene):
""" Fitness is the negative of the number of particles reaching the
detection plane within +/- windowwidth of the targetspeed.
"""
pos, vel = self.geneFlyer.flyGene(gene)
ind = np.where((pos[:, 2] >= self.detectorPos) &
(vel[:, 2] < self.targetspeed+self.windowwidth) &
(vel[:, 2] > self.targetspeed-self.windowwidth)
)[0]
return -len(ind)