fitness

package
v1.20.0 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2025 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CalculateSoftmax added in v1.15.0

func CalculateSoftmax(scores []float64) []float64

calculateSoftmax converts scores to probabilities using numerically stable softmax

func CalculateTreeFitness

func CalculateTreeFitness(tree *individual.TreeNode, targetResults []float64, testCases []map[string]float64) float64

func ExtractGameScore added in v1.11.0

func ExtractGameScore(observation map[string]interface{}) float64

ExtractGameScore extracts a numeric score from observation data

func Round

func Round(x float64, places int) float64

func SampleAction added in v1.15.0

func SampleAction(probabilties []float64) int

func SetupEvalFunction

func SetupEvalFunction(evalFunction string, variableSet []string, testCaseCount int) ([]map[string]float64, []float64)

Types

type ActionExecutor added in v1.11.0

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

ActionExecutor evaluates action trees and converts outputs to game actions

func NewActionExecutor added in v1.11.0

func NewActionExecutor(actions []individual.ActionTuple) *ActionExecutor

NewActionExecutor creates a new action executor

func (*ActionExecutor) ExecuteActionTreesWithSoftmax added in v1.11.0

func (ae *ActionExecutor) ExecuteActionTreesWithSoftmax(actionTreeIndividual *individual.ActionTreeIndividual, weights *individual.WeightsIndividual, inputs map[string]float64, owned_cells [][]bool) ([]int, error)

ExecuteActionTreesWithSoftmax evaluates all action trees with given inputs and returns selected action using softmax

type ActionRequest added in v1.11.0

type ActionRequest struct {
	Type   string      `json:"type"`
	Action interface{} `json:"action"`
}

type ActionTreeFitnessCalculator added in v1.11.0

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

ActionTreeFitnessCalculator implements fitness calculation for ActionTree individuals

func NewActionTreeFitnessCalculator added in v1.11.0

func NewActionTreeFitnessCalculator(serverAddr string, opponentType string, actions []individual.ActionTuple, maxSteps int, populations []*[]individual.Evolvable, selectionPercentage float64, poolSize int, testCaseCount int, timeout time.Duration) *ActionTreeFitnessCalculator

NewActionTreeFitnessCalculator creates a new action tree fitness calculator

func (*ActionTreeFitnessCalculator) CalculateFitness added in v1.11.0

func (atfc *ActionTreeFitnessCalculator) CalculateFitness(evolvable individual.Evolvable)

CalculateFitness evaluates the fitness of an ActionTree individual

func (*ActionTreeFitnessCalculator) Close added in v1.14.0

func (atfc *ActionTreeFitnessCalculator) Close() error

Close closes the connection pool and cleans up resources

func (*ActionTreeFitnessCalculator) SetupGameAndRun added in v1.12.0

func (atfc *ActionTreeFitnessCalculator) SetupGameAndRun(weightsInd *individual.WeightsIndividual, actionTreeInd *individual.ActionTreeIndividual) (float64, string, error)

type ActionValidator added in v1.14.0

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

ActionValidator validates actions based on game observations

func NewActionValidator added in v1.14.0

func NewActionValidator() *ActionValidator

NewActionValidator creates a new action validator

func (*ActionValidator) DirectionActionMask added in v1.15.0

func (av *ActionValidator) DirectionActionMask(x, y int) ([]float64, error)

func (*ActionValidator) SelectValidAction added in v1.15.0

func (av *ActionValidator) SelectValidAction(actionOutputs [][]float64, owned_cells [][]bool) ([]int, error)

func (*ActionValidator) SetMountains added in v1.15.0

func (av *ActionValidator) SetMountains(mountain_array [][]bool)

func (*ActionValidator) ValidXActionMask added in v1.15.0

func (av *ActionValidator) ValidXActionMask(owned_cells [][]bool) ([]float64, error)

ValidXActionMask Returns all x values that have a potential y value

func (*ActionValidator) ValidYActionMask added in v1.15.0

func (av *ActionValidator) ValidYActionMask(x int, owned_cells [][]bool) ([]float64, error)

ValidYActionMask Returns all x values that have a potential y value

type BinaryFitnessCalculator

type BinaryFitnessCalculator struct{}

func (*BinaryFitnessCalculator) CalculateFitness

func (binaryFitness *BinaryFitnessCalculator) CalculateFitness(evolvable individual.Evolvable)

type Client added in v1.19.0

type Client struct {
	ID      string
	Fitness float64
}

type ConnectRequest added in v1.11.0

type ConnectRequest struct {
	Type         string `json:"type"`
	ClientId     string `json:"client_id"`
	AgentType    string `json:"agent_type"`
	OpponentType string `json:"opponent_type"`
}

Message structures matching Python payloads

type ConnectedResponse added in v1.11.0

type ConnectedResponse struct {
	Type       string `json:"type"`
	AgentID    string `json:"agent_id"`
	OpponentID string `json:"opponent_id"`
	Message    string `json:"message"`
}

type ErrorResponse added in v1.11.0

type ErrorResponse struct {
	Type    string `json:"type"`
	Message string `json:"message"`
	Details string `json:"details,omitempty"`
}

type FitnessCalculator

type FitnessCalculator interface {
	CalculateFitness(evolvable individual.Evolvable)
}

func FitnessCalculatorFactory

func FitnessCalculatorFactory(info FitnessSetupInformation) FitnessCalculator

func FitnessCalculatorFactoryWithConfig added in v1.14.0

func FitnessCalculatorFactoryWithConfig(info FitnessSetupInformation, config *cfg.Config) FitnessCalculator

type FitnessSetupInformation

type FitnessSetupInformation struct {
	EvalFunction                  string
	VariableSet                   []string
	GenomeType                    individual.GenomeType
	TestCaseCount                 int
	Grammar                       map[string]individual.Node
	ServerAddr                    string
	OpponentType                  string
	MaxSteps                      int
	Actions                       []individual.ActionTuple
	Population                    []*[]individual.Evolvable
	ActionTreeSelectionPercentage float64
}

func GenerateFitnessInfoFromConfig

func GenerateFitnessInfoFromConfig(config *cfg.Config, genomeType individual.GenomeType, grammar map[string]individual.Node, populations []*[]individual.Evolvable) FitnessSetupInformation

type GameOverResponse added in v1.11.0

type GameOverResponse struct {
	Type         string             `json:"type"`
	Winner       *string            `json:"winner"`
	FinalRewards map[string]float64 `json:"final_rewards"`
	Reason       string             `json:"reason"`
}

type GrammarTreeFitnessCalculator

type GrammarTreeFitnessCalculator struct {
	TestCases     []map[string]float64
	TargetResults []float64
	Grammar       map[string]individual.Node
}

func (*GrammarTreeFitnessCalculator) CalculateFitness

func (gtreeCalculator *GrammarTreeFitnessCalculator) CalculateFitness(evolvable individual.Evolvable)

func (*GrammarTreeFitnessCalculator) SetupEvalFunction

func (fitnessCalc *GrammarTreeFitnessCalculator) SetupEvalFunction(evalFunction string, variableSet []string, testCaseCount int)

type MessageType added in v1.11.0

type MessageType string

MessageType constants matching the Python server

const (
	Connect     MessageType = "connect"
	Action      MessageType = "action"
	Reset       MessageType = "reset"
	Connected   MessageType = "connected"
	Observation MessageType = "observation"
	Error       MessageType = "error"
	GameOver    MessageType = "game_over"
	SaveReplay  MessageType = "save_replay"
)

type ObservationResponse added in v1.11.0

type ObservationResponse struct {
	Type        string             `json:"type"`
	Observation map[string]float64 `json:"observation"`
	Reward      float64            `json:"reward"`
	Terminated  bool               `json:"terminated"`
	Truncated   bool               `json:"truncated"`
	Info        [][]bool           `json:"info"`
}

type SaveReplayRequest added in v1.19.0

type SaveReplayRequest struct {
	Type string `json:"type"`
}

type ServerHealthChecker added in v1.14.0

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

ServerHealthChecker performs health checks on game server

func NewServerHealthChecker added in v1.14.0

func NewServerHealthChecker(serverAddr string, timeout time.Duration) *ServerHealthChecker

NewServerHealthChecker creates a new health checker

func (*ServerHealthChecker) CheckServerHealth added in v1.14.0

func (shc *ServerHealthChecker) CheckServerHealth() error

CheckServerHealth performs a basic health check on the game server

func (*ServerHealthChecker) CheckServerHealthWithRetry added in v1.14.0

func (shc *ServerHealthChecker) CheckServerHealthWithRetry() error

CheckServerHealthWithRetry performs health check with a single retry

type TCPClient added in v1.11.0

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

TCPClient handles communication with the game server

func NewTCPClient added in v1.11.0

func NewTCPClient(serverAddr string) *TCPClient

NewTCPClient creates a new TCP client

func (*TCPClient) Connect added in v1.11.0

func (tc *TCPClient) Connect() error

Connect establishes connection to the game server

func (*TCPClient) ConnectToGame added in v1.11.0

func (tc *TCPClient) ConnectToGame(clientId string, opponentType string) (*ConnectedResponse, error)

ConnectToGame sends connect request and waits for connected response

func (*TCPClient) Disconnect added in v1.11.0

func (tc *TCPClient) Disconnect() error

Disconnect closes the connection

func (*TCPClient) ReceiveMessage added in v1.11.0

func (tc *TCPClient) ReceiveMessage() (map[string]interface{}, error)

ReceiveMessage receives a JSON message from the server

func (*TCPClient) ReceiveObservation added in v1.11.0

func (tc *TCPClient) ReceiveObservation() (*ObservationResponse, error)

ReceiveObservation waits for an observation response

func (*TCPClient) SendAction added in v1.11.0

func (tc *TCPClient) SendAction(action interface{}) error

SendAction sends an action to the server

func (*TCPClient) SendDisconnect added in v1.19.0

func (tc *TCPClient) SendDisconnect() error

SendAction sends an action to the server

func (*TCPClient) SendMessage added in v1.11.0

func (tc *TCPClient) SendMessage(message interface{}) error

SendMessage sends a JSON message to the server

func (*TCPClient) WaitForGameOver added in v1.11.0

func (tc *TCPClient) WaitForGameOver() (*GameOverResponse, error)

WaitForGameOver waits specifically for a game over message

type TCPConnectionPool added in v1.14.0

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

func NewTCPConnectionPool added in v1.14.0

func NewTCPConnectionPool(serverAddr string, maxConnections int, timeout time.Duration) *TCPConnectionPool

func (*TCPConnectionPool) Close added in v1.14.0

func (p *TCPConnectionPool) Close() error

─────────────────────────────

Close()

─────────────────────────────

func (*TCPConnectionPool) GetConnection added in v1.14.0

func (p *TCPConnectionPool) GetConnection() (*TCPClient, error)

─────────────────────────────

BLOCKING GetConnection()

─────────────────────────────

func (*TCPConnectionPool) GetStats added in v1.14.0

func (p *TCPConnectionPool) GetStats() map[string]interface{}

─────────────────────────────

Stats

─────────────────────────────

func (*TCPConnectionPool) HealthCheck added in v1.14.0

func (p *TCPConnectionPool) HealthCheck() error

─────────────────────────────

HealthCheck() external API

─────────────────────────────

func (*TCPConnectionPool) ReturnConnection added in v1.14.0

func (p *TCPConnectionPool) ReturnConnection(client *TCPClient) error

─────────────────────────────

ReturnConnection

─────────────────────────────

type TreeFitnessCalculator

type TreeFitnessCalculator struct {
	TestCases     []map[string]float64
	TargetResults []float64
}

func (*TreeFitnessCalculator) CalculateFitness

func (fitnessCalc *TreeFitnessCalculator) CalculateFitness(evolvable individual.Evolvable)

func (*TreeFitnessCalculator) SetupEvalFunction

func (fitnessCalc *TreeFitnessCalculator) SetupEvalFunction(evalFunction string, variableSet []string, testCaseCount int)

Jump to

Keyboard shortcuts

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