ga

package
v0.0.0-...-e06ad40 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2019 License: GPL-3.0 Imports: 31 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidLengthService = fmt.Errorf("proto: negative length found during unmarshaling")
	ErrIntOverflowService   = fmt.Errorf("proto: integer overflow")
)
View Source
var Log = logrus.New()

Log is the Pool Logger

Functions

func RegisterDistributedEAServer

func RegisterDistributedEAServer(s *grpc.Server, srv DistributedEAServer)

Types

type Client

type Client struct {
	ServerAddr   string
	Log          *logrus.Logger
	ID           string
	CustomDialer func(s string, dt time.Duration) (net.Conn, error)
	Delegate     ServiceDelegate
	// contains filtered or unexported fields
}

Client is a client to communicate with the GRPC server

func (*Client) Start

func (c *Client) Start() error

Start starts the client on a while loop borrowing, evaluating and returning individuals

func (*Client) StatsFetcher

func (c *Client) StatsFetcher(client *DistributedEAClient)

StatsFetcher is a thread to ask server about stats {best_fitness, best_neurons, total_evaluations_overall}

type Cluster

type Cluster struct {
	Logger                  *logrus.Logger
	ListeningPort           int
	BoostrapPeers           []string
	BroadcastBestIndividual chan Individual
	ReceiveBestIndividual   chan Individual
	// contains filtered or unexported fields
}

Cluster represents a p2p cluster

func MakeCluster

func MakeCluster(listenPort int, buffSize int, newBestIndividualChan chan Individual, boostrapPeers []string, logger ...*logrus.Logger) *Cluster

MakeCluster creates a cluster with default logger and listening on :9999

func (*Cluster) GetMembers

func (c *Cluster) GetMembers() []*memberlist.Node

GetMembers resturns the members on the cluster

func (*Cluster) GetNumNodes

func (c *Cluster) GetNumNodes() int

GetNumNodes resturns the number of nodes in the cluster

func (*Cluster) PrintMembers

func (c *Cluster) PrintMembers()

PrintMembers of the p2p cluster

func (*Cluster) Shutdown

func (c *Cluster) Shutdown()

Shutdown the cluster node

func (*Cluster) Start

func (c *Cluster) Start(metadata NodeMetadata)

Start creates a new node with metadata for this process and joins a existing cluster by connecting to peers in cluster.boostrapPeers array

type DistributedEAClient

type DistributedEAClient interface {
	// Communication for Clients
	GetProblemDescription(ctx context.Context, in *empty.Empty, opts ...grpc.CallOption) (*ProblemDescription, error)
	GetStats(ctx context.Context, in *empty.Empty, opts ...grpc.CallOption) (*Stats, error)
	BorrowIndividual(ctx context.Context, in *empty.Empty, opts ...grpc.CallOption) (*Individual, error)
	ReturnIndividual(ctx context.Context, in *Individual, opts ...grpc.CallOption) (*empty.Empty, error)
	// Communication with other Servers
	MigrateIndividuals(ctx context.Context, in *IndividualsBatch, opts ...grpc.CallOption) (*empty.Empty, error)
}

DistributedEAClient is the client API for DistributedEA service.

For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream.

func NewDistributedEAClient

func NewDistributedEAClient(cc *grpc.ClientConn) DistributedEAClient

type DistributedEAServer

type DistributedEAServer interface {
	// Communication for Clients
	GetProblemDescription(context.Context, *empty.Empty) (*ProblemDescription, error)
	GetStats(context.Context, *empty.Empty) (*Stats, error)
	BorrowIndividual(context.Context, *empty.Empty) (*Individual, error)
	ReturnIndividual(context.Context, *Individual) (*empty.Empty, error)
	// Communication with other Servers
	MigrateIndividuals(context.Context, *IndividualsBatch) (*empty.Empty, error)
}

DistributedEAServer is the server API for DistributedEA service.

type GenomeGenerator

type GenomeGenerator = func(*rand.Rand) eaopt.Genome

GenomeGenerator is a function that creates genomes randomly initializaed

type Individual

type Individual struct {
	IndividualID         string   `protobuf:"bytes,2,opt,name=individualID,proto3" json:"individualID,omitempty"`
	Evaluated            bool     `protobuf:"varint,3,opt,name=evaluated,proto3" json:"evaluated,omitempty"`
	Fitness              float64  `protobuf:"fixed64,4,opt,name=fitness,proto3" json:"fitness,omitempty"`
	Genome               []byte   `protobuf:"bytes,5,opt,name=genome,proto3" json:"genome,omitempty"`
	XXX_NoUnkeyedLiteral struct{} `json:"-"`
	XXX_unrecognized     []byte   `json:"-"`
	XXX_sizecache        int32    `json:"-"`
}

func NewPopulatedIndividual

func NewPopulatedIndividual(r randyService, easy bool) *Individual

func (*Individual) Descriptor

func (*Individual) Descriptor() ([]byte, []int)

func (*Individual) Equal

func (this *Individual) Equal(that interface{}) bool

func (Individual) Finished

func (in Individual) Finished()

Finished implements Broadcast interface

func (*Individual) GoString

func (this *Individual) GoString() string

func (Individual) Invalidates

func (in Individual) Invalidates(other memberlist.Broadcast) bool

Invalidates implements Broadcast interface for Individual

func (*Individual) Marshal

func (m *Individual) Marshal() (dAtA []byte, err error)

func (*Individual) MarshalTo

func (m *Individual) MarshalTo(dAtA []byte) (int, error)

func (Individual) Message

func (in Individual) Message() []byte

Message implements Broadcast interface

func (Individual) Name

func (in Individual) Name() string

Name implements NamedBroadcast

func (*Individual) ProtoMessage

func (*Individual) ProtoMessage()

func (*Individual) Reset

func (m *Individual) Reset()

func (*Individual) Size

func (m *Individual) Size() (n int)

func (*Individual) String

func (this *Individual) String() string

func (*Individual) Unmarshal

func (m *Individual) Unmarshal(dAtA []byte) error

func (*Individual) XXX_DiscardUnknown

func (m *Individual) XXX_DiscardUnknown()

func (*Individual) XXX_Marshal

func (m *Individual) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*Individual) XXX_Merge

func (m *Individual) XXX_Merge(src proto.Message)

func (*Individual) XXX_Size

func (m *Individual) XXX_Size() int

func (*Individual) XXX_Unmarshal

func (m *Individual) XXX_Unmarshal(b []byte) error

type IndividualsBatch

type IndividualsBatch struct {
	Individuals          []Individual `protobuf:"bytes,1,rep,name=individuals,proto3" json:"individuals"`
	XXX_NoUnkeyedLiteral struct{}     `json:"-"`
	XXX_unrecognized     []byte       `json:"-"`
	XXX_sizecache        int32        `json:"-"`
}

func NewPopulatedIndividualsBatch

func NewPopulatedIndividualsBatch(r randyService, easy bool) *IndividualsBatch

func (*IndividualsBatch) Descriptor

func (*IndividualsBatch) Descriptor() ([]byte, []int)

func (*IndividualsBatch) Equal

func (this *IndividualsBatch) Equal(that interface{}) bool

func (*IndividualsBatch) GoString

func (this *IndividualsBatch) GoString() string

func (*IndividualsBatch) Marshal

func (m *IndividualsBatch) Marshal() (dAtA []byte, err error)

func (*IndividualsBatch) MarshalTo

func (m *IndividualsBatch) MarshalTo(dAtA []byte) (int, error)

func (*IndividualsBatch) ProtoMessage

func (*IndividualsBatch) ProtoMessage()

func (*IndividualsBatch) Reset

func (m *IndividualsBatch) Reset()

func (*IndividualsBatch) Size

func (m *IndividualsBatch) Size() (n int)

func (*IndividualsBatch) String

func (this *IndividualsBatch) String() string

func (*IndividualsBatch) Unmarshal

func (m *IndividualsBatch) Unmarshal(dAtA []byte) error

func (*IndividualsBatch) XXX_DiscardUnknown

func (m *IndividualsBatch) XXX_DiscardUnknown()

func (*IndividualsBatch) XXX_Marshal

func (m *IndividualsBatch) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*IndividualsBatch) XXX_Merge

func (m *IndividualsBatch) XXX_Merge(src proto.Message)

func (*IndividualsBatch) XXX_Size

func (m *IndividualsBatch) XXX_Size() int

func (*IndividualsBatch) XXX_Unmarshal

func (m *IndividualsBatch) XXX_Unmarshal(b []byte) error

type MetricsServer

type MetricsServer struct {
	EvaluationsCount        prometheus.Counter
	OutgoingMigrationsCount prometheus.Counter
	IncomingMigrationsCount prometheus.Counter
	OutgoingBroadcasts      prometheus.Counter
	IncomingBroadcasts      prometheus.Counter
	BestFitnessGauge        prometheus.Gauge
	// contains filtered or unexported fields
}

MetricsServer is a prometheus server

func MakeMetricsServer

func MakeMetricsServer(pool *PoolModel, addr string, logger *logrus.Logger) *MetricsServer

MakeMetricsServer starts a metric server listening on addr

func (*MetricsServer) Shutdown

func (ms *MetricsServer) Shutdown()

Shutdown the metrics server unregistering metrics

func (*MetricsServer) Start

func (ms *MetricsServer) Start()

Start the metrics server

type NodeMetadata

type NodeMetadata struct {
	GrpcPort             int64    `protobuf:"varint,1,opt,name=grpcPort,proto3" json:"grpcPort,omitempty"`
	GrpcWsPort           int64    `protobuf:"varint,2,opt,name=grpcWsPort,proto3" json:"grpcWsPort,omitempty"`
	XXX_NoUnkeyedLiteral struct{} `json:"-"`
	XXX_unrecognized     []byte   `json:"-"`
	XXX_sizecache        int32    `json:"-"`
}

func NewPopulatedNodeMetadata

func NewPopulatedNodeMetadata(r randyService, easy bool) *NodeMetadata

func (*NodeMetadata) Descriptor

func (*NodeMetadata) Descriptor() ([]byte, []int)

func (*NodeMetadata) Equal

func (this *NodeMetadata) Equal(that interface{}) bool

func (*NodeMetadata) GoString

func (this *NodeMetadata) GoString() string

func (*NodeMetadata) Marshal

func (m *NodeMetadata) Marshal() (dAtA []byte, err error)

func (*NodeMetadata) MarshalTo

func (m *NodeMetadata) MarshalTo(dAtA []byte) (int, error)

func (*NodeMetadata) ProtoMessage

func (*NodeMetadata) ProtoMessage()

func (*NodeMetadata) Reset

func (m *NodeMetadata) Reset()

func (*NodeMetadata) Size

func (m *NodeMetadata) Size() (n int)

func (*NodeMetadata) String

func (this *NodeMetadata) String() string

func (*NodeMetadata) Unmarshal

func (m *NodeMetadata) Unmarshal(dAtA []byte) error

func (*NodeMetadata) XXX_DiscardUnknown

func (m *NodeMetadata) XXX_DiscardUnknown()

func (*NodeMetadata) XXX_Marshal

func (m *NodeMetadata) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*NodeMetadata) XXX_Merge

func (m *NodeMetadata) XXX_Merge(src proto.Message)

func (*NodeMetadata) XXX_Size

func (m *NodeMetadata) XXX_Size() int

func (*NodeMetadata) XXX_Unmarshal

func (m *NodeMetadata) XXX_Unmarshal(b []byte) error

type PoolModel

type PoolModel struct {
	Rnd            *rand.Rand
	KeepBest       bool
	ExtraOperators []eaopt.ExtraOperator
	EarlyStop      func(*PoolModel) bool

	// Sorting params
	SortFunc       SortFunction
	SortPrecission int

	// Callbacks
	BestCallback       func(*PoolModel)
	GenerationCallback func(*PoolModel)

	NCandidates int
	CrossRate   float64
	MutRate     float64

	PopSize        int
	MaxEvaluations int
	BestSolution   eaopt.Individual

	// Clients communications
	WebPort int
	WebPath string

	// Client Settings
	Delegate ServiceDelegate

	// Server communications
	BroadcastedIndividual chan Individual

	NMigrate int
	// contains filtered or unexported fields
}

PoolModel is a pool-based evolutionary algorithm

TODO: Wrap configuration into ConfigTypes (e.g. ClientConfig, IslandConfig, ModelConfig...)

func MakePool

func MakePool(
	popSize int, grpcPort, clusterPort, metricsPort int, boostrapPeers []string,
	rnd *rand.Rand, generator GenomeGenerator) *PoolModel

MakePool Creates a new pool with default configuration

func (*PoolModel) GetAverageFitness

func (pool *PoolModel) GetAverageFitness() float64

GetAverageFitness returns the vaerage fitness of the population

func (*PoolModel) GetPopulationSnapshot

func (pool *PoolModel) GetPopulationSnapshot() []eaopt.Individual

GetPopulationSnapshot returns an snapshot of the population at a given moment

func (*PoolModel) GetTotalEvaluations

func (pool *PoolModel) GetTotalEvaluations() int

GetTotalEvaluations returns the total number of evaluations carried out so far

func (*PoolModel) Minimize

func (pool *PoolModel) Minimize()

Minimize runs the algorithm by connecting the pipes

type Population

type Population struct {
	// contains filtered or unexported fields
}

Population stores individuals

func MakePopulation

func MakePopulation() *Population

MakePopulation returns an empty population

func (*Population) Add

func (pop *Population) Add(indiv eaopt.Individual)

Add an individual

func (*Population) Fold

func (pop *Population) Fold(f func(eaopt.Individual) bool)

Fold applies function f to each individual in the population until the end is reached or f returns false

func (*Population) Length

func (pop *Population) Length() int

Length returns the size of the population

func (*Population) RandIndividual

func (pop *Population) RandIndividual(rnd *rand.Rand) (eaopt.Individual, error)

RandIndividual takes an snapshot of the population and returns a random entry from it

func (*Population) Remove

func (pop *Population) Remove(indiv eaopt.Individual)

Remove an individual

func (*Population) Snapshot

func (pop *Population) Snapshot() map[string]cache.Item

Snapshot returns the state of the population at the moment it was called. TODO: Wrap the cache.Item type to eaopt.Individual

type ProblemDescription

type ProblemDescription struct {
	ClientID             string   `protobuf:"bytes,1,opt,name=clientID,proto3" json:"clientID,omitempty"`
	Payload              []byte   `protobuf:"bytes,2,opt,name=payload,proto3" json:"payload,omitempty"`
	XXX_NoUnkeyedLiteral struct{} `json:"-"`
	XXX_unrecognized     []byte   `json:"-"`
	XXX_sizecache        int32    `json:"-"`
}

func NewPopulatedProblemDescription

func NewPopulatedProblemDescription(r randyService, easy bool) *ProblemDescription

func (*ProblemDescription) Descriptor

func (*ProblemDescription) Descriptor() ([]byte, []int)

func (*ProblemDescription) Equal

func (this *ProblemDescription) Equal(that interface{}) bool

func (*ProblemDescription) GoString

func (this *ProblemDescription) GoString() string

func (*ProblemDescription) Marshal

func (m *ProblemDescription) Marshal() (dAtA []byte, err error)

func (*ProblemDescription) MarshalTo

func (m *ProblemDescription) MarshalTo(dAtA []byte) (int, error)

func (*ProblemDescription) ProtoMessage

func (*ProblemDescription) ProtoMessage()

func (*ProblemDescription) Reset

func (m *ProblemDescription) Reset()

func (*ProblemDescription) Size

func (m *ProblemDescription) Size() (n int)

func (*ProblemDescription) String

func (this *ProblemDescription) String() string

func (*ProblemDescription) Unmarshal

func (m *ProblemDescription) Unmarshal(dAtA []byte) error

func (*ProblemDescription) XXX_DiscardUnknown

func (m *ProblemDescription) XXX_DiscardUnknown()

func (*ProblemDescription) XXX_Marshal

func (m *ProblemDescription) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*ProblemDescription) XXX_Merge

func (m *ProblemDescription) XXX_Merge(src proto.Message)

func (*ProblemDescription) XXX_Size

func (m *ProblemDescription) XXX_Size() int

func (*ProblemDescription) XXX_Unmarshal

func (m *ProblemDescription) XXX_Unmarshal(b []byte) error

type Server

type Server struct {
	Log    *logrus.Logger
	Input  chan eaopt.Individual
	Output chan eaopt.Individual
	Pool   *PoolModel
	// contains filtered or unexported fields
}

Server implements DistributedEA service

func (*Server) BorrowIndividual

func (s *Server) BorrowIndividual(ctx context.Context, e *empty.Empty) (*Individual, error)

BorrowIndividual implements DistributedEA service

func (*Server) GetProblemDescription

func (s *Server) GetProblemDescription(ctx context.Context, in *empty.Empty) (*ProblemDescription, error)

GetProblemDescription returs the configuration for the problem execution to the client

func (*Server) GetStats

func (s *Server) GetStats(ctx context.Context, in *empty.Empty) (*Stats, error)

GetStats returs the statistics of the overall execution on the algorithm

func (*Server) MigrateIndividuals

func (s *Server) MigrateIndividuals(ctx context.Context, batch *IndividualsBatch) (*empty.Empty, error)

MigrateIndividuals implements DistributedEA service

func (*Server) ReturnIndividual

func (s *Server) ReturnIndividual(ctx context.Context, msg *Individual) (*empty.Empty, error)

ReturnIndividual implements DistributedEA service

type ServiceDelegate

type ServiceDelegate interface {
	// Write serialized problem description
	SerializeProblemDescription() []byte

	// Deserialize problem description into own scope
	DeserializeProblemDescription([]byte)

	// Serialize Genome to array
	SerializeGenome(eaopt.Genome) []byte

	// Deserialize array to Genome
	DeserializeGenome([]byte) eaopt.Genome
}

ServiceDelegate must be implemented by the client and server function

type SortFunction

type SortFunction = func(slice []eaopt.Individual, precission int) []eaopt.Individual

SortFunction gets an array of individuals and sorts them with a given precission

type Stats

type Stats struct {
	Evaluations          int64    `protobuf:"varint,2,opt,name=evaluations,proto3" json:"evaluations,omitempty"`
	BestFitness          float64  `protobuf:"fixed64,3,opt,name=bestFitness,proto3" json:"bestFitness,omitempty"`
	AvgFitness           float64  `protobuf:"fixed64,4,opt,name=avgFitness,proto3" json:"avgFitness,omitempty"`
	XXX_NoUnkeyedLiteral struct{} `json:"-"`
	XXX_unrecognized     []byte   `json:"-"`
	XXX_sizecache        int32    `json:"-"`
}

func NewPopulatedStats

func NewPopulatedStats(r randyService, easy bool) *Stats

func (*Stats) Descriptor

func (*Stats) Descriptor() ([]byte, []int)

func (*Stats) Equal

func (this *Stats) Equal(that interface{}) bool

func (*Stats) GoString

func (this *Stats) GoString() string

func (*Stats) Marshal

func (m *Stats) Marshal() (dAtA []byte, err error)

func (*Stats) MarshalTo

func (m *Stats) MarshalTo(dAtA []byte) (int, error)

func (*Stats) ProtoMessage

func (*Stats) ProtoMessage()

func (*Stats) Reset

func (m *Stats) Reset()

func (*Stats) Size

func (m *Stats) Size() (n int)

func (*Stats) String

func (this *Stats) String() string

func (*Stats) Unmarshal

func (m *Stats) Unmarshal(dAtA []byte) error

func (*Stats) XXX_DiscardUnknown

func (m *Stats) XXX_DiscardUnknown()

func (*Stats) XXX_Marshal

func (m *Stats) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*Stats) XXX_Merge

func (m *Stats) XXX_Merge(src proto.Message)

func (*Stats) XXX_Size

func (m *Stats) XXX_Size() int

func (*Stats) XXX_Unmarshal

func (m *Stats) XXX_Unmarshal(b []byte) error

type UnimplementedDistributedEAServer

type UnimplementedDistributedEAServer struct {
}

UnimplementedDistributedEAServer can be embedded to have forward compatible implementations.

func (*UnimplementedDistributedEAServer) BorrowIndividual

func (*UnimplementedDistributedEAServer) BorrowIndividual(ctx context.Context, req *empty.Empty) (*Individual, error)

func (*UnimplementedDistributedEAServer) GetProblemDescription

func (*UnimplementedDistributedEAServer) GetProblemDescription(ctx context.Context, req *empty.Empty) (*ProblemDescription, error)

func (*UnimplementedDistributedEAServer) GetStats

func (*UnimplementedDistributedEAServer) MigrateIndividuals

func (*UnimplementedDistributedEAServer) ReturnIndividual

func (*UnimplementedDistributedEAServer) ReturnIndividual(ctx context.Context, req *Individual) (*empty.Empty, error)

Jump to

Keyboard shortcuts

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