ga

package
v0.0.0-...-62a0fd1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 27, 2020 License: MIT Imports: 14 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Maximize

func Maximize(s1, s2 float64) bool

Function to choose for maximization problems.

func Minimize

func Minimize(s1, s2 float64) bool

Function to choose for minimization problems.

func RunGA

func RunGA(dna DNA, conf *Config, chOut, chIn, chNonce chan Sol, jobHash string)

Defines the standard execution of a GA

Types

type Comp

type Comp func(float64, float64) bool
var Optimum Comp = Minimize

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 BuildBlockchainGAConfig(hash []byte, keepmining *bool, step int) *Config

func DefConf

func DefConf(x int64, gen, step int) *Config

Generate a Config with default parameters.

func RandConf

func RandConf(x int64, gen, step int) *Config

Generate a Config with random parameters.

func (*Config) ChangeHash

func (self *Config) ChangeHash(hash []byte)

func (*Config) GetHash

func (self *Config) GetHash() []byte

func (*Config) SetBlockHash

func (c *Config) SetBlockHash(hash []byte)

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.

func LoadGA

func LoadGA(path2job, path2data string) (DNA, error)

Returns a DNA after checking and compiling a user-defined plugin.

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

func BuildExecutor(chUpdateOut chan Sol) *Executor

Builds and initialize an Executor

func (*Executor) ChangeBlockHashInJob

func (self *Executor) ChangeBlockHashInJob(job, hashPrev, publicKey string)

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

func (self *Executor) InjectSharedSolution(job string, solSerialized []byte)

Sends a good solution to an active job so that it can include it in his population.

func (*Executor) IsExecutingJob

func (self *Executor) IsExecutingJob(job string) bool

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.

func (*Executor) StopJob

func (self *Executor) StopJob(job string)

Terminates the execution of a job by its hash.

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

func BuildJob(jobpath, datapath string, chUpdateOut chan Sol, jobHash string) *Job

Initialize a Job, loading its dna and data from file after compiling a user-defined plugin.

func (*Job) ChangeBlockHash

func (self *Job) ChangeBlockHash(hashPrev, publicKey string)

Change the hash used during the job execution to initialize the coefficients of the operations, that are stored in the state.

func (*Job) EvaluateSingleSolution

func (self *Job) EvaluateSingleSolution(individual []byte,
	hashPrev, publicKey string) *Sol

Builds a job and returns the evaluation of an individual, without start executing the genetic algorithm.

func (*Job) Execute

func (self *Job) Execute(hashPrev, publicKey string)

Keeps the GA execution alive and should be called in a goroutine. Stops when self.KeepRunning==false. Add to the current population the Sol passed to self.ChUpdateIn and publish the best found Sol to self.ChUpdateOut every 100 epochs.

type JobChannels

type JobChannels struct {
	ChNonce    chan Sol // used to send nonce candidates
	ChUpdateIn chan Sol // used to receive shared solutions from miners
}

type Packet

type Packet struct {
	Solution Sol
	End      bool
	Shared   Population
}

The type of the messages the miner exchanges with the GA thread.

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

type Problem interface {
	Initialize(path string)
	New() DNA
}

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.

func (Sol) DeepCopy

func (self Sol) DeepCopy() Sol

Returns a deep copy instance of a Sol

func (*Sol) Eval2

func (self *Sol) Eval2(st *op.State, blockHash []byte)

This method is used only for testing; please use eval to evaluate a Sol.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL