github.com/RH12503/Triangula@v1.2.0/algorithm/evaluator/evaluators.go (about) 1 // Package evaluator provides an interface for a fitness evaluator and 2 // some implementations for common use cases. 3 package evaluator 4 5 import ( 6 "github.com/RH12503/Triangula/fitness" 7 ) 8 9 // An Evaluator is used by an algorithm to evaluate the fitness of its members. 10 type Evaluator interface { 11 // Get returns a fitness function given the index of a member. 12 Get(i int) fitness.Function 13 14 // Update carries out any updates necessary for a specified member. 15 Update(i int) 16 17 // Prepare does any preparations necessary for the evaluator. 18 Prepare() 19 20 // SetBase should be called to indicate the base of member i. 21 SetBase(i, base int) 22 23 // Swap should be called if members i and j are swapped. 24 Swap(i, j int) 25 } 26 27 // one is a generic evaluator which has one fitness function for each member. 28 type one struct { 29 evaluator fitness.Function 30 } 31 32 func (o one) SetBase(i, base int) { 33 } 34 35 func (o one) Swap(i, j int) { 36 } 37 38 func (o one) Prepare() { 39 } 40 41 func (o one) Update(i int) { 42 } 43 44 func (o one) Get(i int) fitness.Function { 45 // Return the same fitness function every time 46 return o.evaluator 47 } 48 49 // NewOne returns a new "one" fitness evaluator. 50 func NewOne(evaluator fitness.Function) one { 51 return one{evaluator: evaluator} 52 } 53 54 // many is a generic evaluator where there is one fitness function for each member. 55 type many struct { 56 evaluators []fitness.Function 57 } 58 59 func (m many) SetBase(i, base int) { 60 } 61 62 func (m many) Swap(i, j int) { 63 } 64 65 func (m many) Prepare() { 66 } 67 68 func (m many) Update(i int) { 69 } 70 71 func (m many) Get(i int) fitness.Function { 72 return m.evaluators[i] 73 } 74 75 // NewMany returns a new "many" fitness evaluator. 76 func NewMany(newEvaluator func() fitness.Function, n int) many { 77 var evaluators []fitness.Function 78 79 for i := 0; i < n; i++ { 80 evaluators = append(evaluators, newEvaluator()) 81 } 82 83 return many{evaluators: evaluators} 84 }