Documentation
¶
Overview ¶
Package ga provides functions and types to define and execute genetic algorithms.
*
- @Author: Bizzaro Francesco <d33pblue>
- @Date: 2020-May-14
- @Project: Proof of Evolution
- @Filename: job.go
- @Last modified by: d33pblue
- @Last modified time: 2020-May-27
- @Copyright: 2020
Index ¶
- func Maximize(s1, s2 float64) bool
- func Minimize(s1, s2 float64) bool
- func RunGA(dna DNA, conf *Config, chOut, chIn, chNonce chan Sol, jobHash string)
- type Comp
- type Config
- type DNA
- type Executor
- func (self *Executor) ChangeBlockHashInJob(job, hashPrev, publicKey string)
- func (self *Executor) GetChannels(job string) *JobChannels
- func (self *Executor) InjectSharedSolution(job string, solSerialized []byte)
- func (self *Executor) IsExecutingJob(job string) bool
- func (self *Executor) StartJob(hash, hashPrev, publicKey, jobpath, datapath string) *JobChannels
- func (self *Executor) StopJob(job string)
- type Job
- type JobChannels
- type Packet
- type Population
- type Problem
- type Sol
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Config ¶
type Config struct {
Miner int64
Gen int
Step int
NPop int
Pcross float64
Pmut float64
Mu int
Lambda int
Verbose int
BlockHash []byte
// contains filtered or unexported fields
}
Config collect the execution parameters the miner chooses to use while executing a GA.
func BuildBlockchainGAConfig ¶
func (*Config) ChangeHash ¶
func (*Config) SetBlockHash ¶
Change the current block's hash (useful to update the complexity accordingly).
type DNA ¶
type DNA interface {
Generate(prng *rand.Rand) DNA
Mutate(prng *rand.Rand) DNA
Crossover(ind2 DNA, prng *rand.Rand) DNA
Evaluate(st *op.State) float64
DeepCopy() DNA
HasToMinimize() bool
Serialize() []byte
LoadFromSerialization([]byte)
}
DNA is the interface the user has to implement to define a problem for a Job transaction.
type Executor ¶
type Executor struct {
ActiveJobs map[string]*Job // the key is the hash of the JobTransaction
// in which the job is defined
ChUpdateOut chan Sol
}
func BuildExecutor ¶
Builds and initialize an Executor
func (*Executor) ChangeBlockHashInJob ¶
Change the hash of the block in the job's execution configuration, so that the coefficients for the complexity are updated. The solutions in ChNonce channel are resetted.
func (*Executor) GetChannels ¶
func (self *Executor) GetChannels(job string) *JobChannels
Returns the channels of an already running job if it is found by its hash; nil otherwise.
func (*Executor) InjectSharedSolution ¶
Sends a good solution to an active job so that it can include it in his population.
func (*Executor) IsExecutingJob ¶
true <=> the job with a hash has been already defined
func (*Executor) StartJob ¶
func (self *Executor) StartJob(hash, hashPrev, publicKey, jobpath, datapath string) *JobChannels
Initialize and start executing a job from the paths to the files with its definition and data. If the job runs correctly, this methos returns a JobChannels with the channels to communicate with the job; otherwise nil.
type Job ¶
type Job struct {
ChNonce chan Sol // used to send nonce candidates
ChUpdateIn chan Sol // used to receive shared solutions from miners
ChUpdateOut chan Sol // used to share good solutions to miners
KeepRunning bool
// contains filtered or unexported fields
}
func BuildJob ¶
Initialize a Job, loading its dna and data from file after compiling a user-defined plugin.
func (*Job) ChangeBlockHash ¶
Change the hash used during the job execution to initialize the coefficients of the operations, that are stored in the state.
func (*Job) EvaluateSingleSolution ¶
Builds a job and returns the evaluation of an individual, without start executing the genetic algorithm.
type JobChannels ¶
type Population ¶
type Population []Sol
A population is a set of solution candidates, and here is represented as array of Sol.
func (Population) DeepCopy ¶
func (self Population) DeepCopy() (pop Population)
Returns a deep copy instance of the whole population.
func (Population) Len ¶
func (a Population) Len() int
func (Population) Less ¶
func (a Population) Less(i, j int) bool
func (Population) Swap ¶
func (a Population) Swap(i, j int)
type Problem ¶
Problem defines the interface the user has to implement to declare his problem. Initialize method is called at the beginning, and then New has to return an instance of the DNA interface.
type Sol ¶
type Sol struct {
Individual DNA
Fitness float64
Complex float64
IsEval bool
Conf Config
Gen int
HashUsed []byte
JobHash string // used only to share good solutions (to identify the job)
IsMin bool // used only to share good solutions; true <=> minimization problem
}
Sol represents a possible solution to the problem and an Individual.