optimus

package
v0.4.31 Latest Latest
Warning

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

Go to latest
Published: May 15, 2020 License: GPL-3.0 Imports: 47 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrDegenerateVector = errors.New("all elements in the vector the same")
)

Functions

func NewDecisionOrdersNewGenome added in v0.4.6

func NewDecisionOrdersNewGenome(knapsack *Knapsack, orders []*MarketOrder) gago.NewGenome

func NewPackedOrdersNewGenome added in v0.4.6

func NewPackedOrdersNewGenome(knapsack *Knapsack, orders []*MarketOrder) gago.NewGenome

func SCAKKT added in v0.4.6

func SCAKKT(A [][]float64, b []float64, eps float64, maxIterations int) ([]float64, int, error)

SCAKKT solves the non-negative least squares problem.

Argument "A" represents input matrix. Each element of "A" must have the same length. Vector "b" represents a measurement or output vector. "Eps" is a tolerance for stopping iteration.

The result tuple consists of three parameters. The first one returns coefficients of the fitted linear function. It will have the same length as elements of "A". The second value is the number of iterations performed. An error is returned if "A" and "b" are not the same length.

func SortOrders

func SortOrders(orders []WeightedOrder)

Types

type AbsolutePriceThreshold added in v0.4.10

type AbsolutePriceThreshold struct {
	*sonm.Price
}

func NewAbsolutePriceThreshold added in v0.4.10

func NewAbsolutePriceThreshold(threshold *sonm.Price) (*AbsolutePriceThreshold, error)

func ParseAbsolutePriceThreshold added in v0.4.10

func ParseAbsolutePriceThreshold(threshold string) (*AbsolutePriceThreshold, error)

func (*AbsolutePriceThreshold) Exceeds added in v0.4.10

func (m *AbsolutePriceThreshold) Exceeds(price, otherPrice *big.Int) bool

type AxeModel added in v0.4.13

type AxeModel struct {
	Log *zap.SugaredLogger
	// contains filtered or unexported fields
}

func (*AxeModel) Optimize added in v0.4.13

func (m *AxeModel) Optimize(ctx context.Context, ks *Knapsack, orders []*MarketOrder) error

type AxeModelConfig added in v0.4.13

type AxeModelConfig struct{}

type AxeModelFactory added in v0.4.13

type AxeModelFactory struct {
	AxeModelConfig
}

func (*AxeModelFactory) Config added in v0.4.13

func (a *AxeModelFactory) Config() interface{}

func (*AxeModelFactory) Create added in v0.4.13

func (a *AxeModelFactory) Create(orders, matchedOrders []*MarketOrder, log *zap.SugaredLogger) OptimizationMethod

type BatchModel added in v0.4.6

type BatchModel struct {
	Methods []OptimizationMethod
	Log     *zap.SugaredLogger
}

func (*BatchModel) Optimize added in v0.4.6

func (m *BatchModel) Optimize(ctx context.Context, knapsack *Knapsack, orders []*MarketOrder) error

type BatchModelConfig added in v0.4.6

type BatchModelConfig struct {
	Brute   bruteConfig                 `yaml:"brute"`
	Methods []optimizationMethodFactory `yaml:"models"`
}

type BatchModelFactory added in v0.4.6

type BatchModelFactory struct {
	BatchModelConfig
}

func (*BatchModelFactory) Config added in v0.4.6

func (m *BatchModelFactory) Config() interface{}

func (*BatchModelFactory) Create added in v0.4.6

func (m *BatchModelFactory) Create(orders, matchedOrders []*MarketOrder, log *zap.SugaredLogger) OptimizationMethod

type Blacklist added in v0.4.4

type Blacklist interface {
	Update(ctx context.Context) error
	IsAllowed(addr common.Address) bool
}

type BranchBoundModel added in v0.4.6

type BranchBoundModel struct {
	Log *zap.SugaredLogger
}

func (*BranchBoundModel) Optimize added in v0.4.6

func (m *BranchBoundModel) Optimize(ctx context.Context, knapsack *Knapsack, orders []*MarketOrder) error

type BranchBoundModelConfig added in v0.4.6

type BranchBoundModelConfig struct {
	HeightLimit int `yaml:"height_limit" default:"6"`
}

type BranchBoundModelFactory added in v0.4.6

type BranchBoundModelFactory struct {
	BranchBoundModelConfig
}

func (*BranchBoundModelFactory) Config added in v0.4.6

func (m *BranchBoundModelFactory) Config() interface{}

func (*BranchBoundModelFactory) Create added in v0.4.6

func (*BranchBoundModelFactory) Create(orders, matchedOrders []*MarketOrder, log *zap.SugaredLogger) OptimizationMethod

type Config

type Config struct {
	Restrictions *RestrictionsConfig         `yaml:"restrictions"`
	Blockchain   *blockchain.Config          `yaml:"blockchain"`
	PrivateKey   privateKey                  `yaml:"ethereum" json:"-"`
	Logging      logging.Config              `yaml:"logging"`
	Node         nodeConfig                  `yaml:"node"`
	Workers      map[auth.Addr]*workerConfig `yaml:"workers"`
	Benchmarks   benchmarks.Config           `yaml:"benchmarks"`
	Marketplace  marketplaceConfig           `yaml:"marketplace"`
	Debug        *debug.Config               `yaml:"debug"`
}

func LoadConfig

func LoadConfig(path string) (*Config, error)

func (*Config) Validate

func (m *Config) Validate() error

type Consumer

type Consumer interface {
	LowerBound() []float64
	DeviceType() sonm.DeviceType
	DeviceBenchmark(id int) (*sonm.Benchmark, bool)
	SplittingAlgorithm() sonm.SplittingAlgorithm
	Result(criteria float64) interface{}
}

type DealRequestFactory added in v0.4.13

type DealRequestFactory func() *sonm.DealsRequest

func DefaultDealRequestFactory added in v0.4.13

func DefaultDealRequestFactory(cfg marketplaceConfig) DealRequestFactory

type DecisionVec added in v0.4.6

type DecisionVec []float64

func (DecisionVec) Clone added in v0.4.6

func (m DecisionVec) Clone() DecisionVec

type Deleter added in v0.4.11

type Deleter interface {
	Delete() error
}

func RestrictUsage added in v0.4.11

func RestrictUsage(cfg *RestrictionsConfig) (Deleter, error)

type DeviceManager

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

func (*DeviceManager) Clone added in v0.4.6

func (m *DeviceManager) Clone() *DeviceManager

func (*DeviceManager) Consume

func (m *DeviceManager) Consume(benchmarks sonm.Benchmarks, netflags sonm.NetFlags) (*sonm.AskPlanResources, error)

func (*DeviceManager) Contains added in v0.4.4

func (m *DeviceManager) Contains(benchmarks sonm.Benchmarks, netflags sonm.NetFlags) bool

func (*DeviceManager) GPUCount added in v0.4.26

func (m *DeviceManager) GPUCount() int

type FittingFunc added in v0.4.4

type FittingFunc struct {
	Filters []func(order *sonm.Order) error
}

func (*FittingFunc) Filter added in v0.4.4

func (m *FittingFunc) Filter(order *sonm.Order) error

type GeneticModel added in v0.4.6

type GeneticModel struct {
	NewGenomeLab NewGenomeLab
	// PopulationSize specifies the number of individuals per Population.
	PopulationSize int
	// MaxGenerations specifies the number of population generations.
	MaxGenerations int
	// MaxAge specifies the maximum age of the entire evolution process.
	MaxAge time.Duration
	// Log is used for internal logging.
	Log *zap.SugaredLogger
}

func (*GeneticModel) Optimize added in v0.4.6

func (m *GeneticModel) Optimize(ctx context.Context, knapsack *Knapsack, orders []*MarketOrder) error

func (*GeneticModel) ShouldEvolve added in v0.4.6

func (m *GeneticModel) ShouldEvolve(ga gago.GA) bool

type GeneticModelConfig added in v0.4.6

type GeneticModelConfig struct {
	Genome         GenomeConfig  `yaml:"genome"`
	PopulationSize int           `yaml:"population_size" default:"256"`
	MaxGenerations int           `yaml:"max_generations" default:"128"`
	MaxAge         time.Duration `yaml:"max_age" default:"5m"`
}

type GeneticModelFactory added in v0.4.6

type GeneticModelFactory struct {
	GeneticModelConfig
}

func (*GeneticModelFactory) Config added in v0.4.6

func (m *GeneticModelFactory) Config() interface{}

func (*GeneticModelFactory) Create added in v0.4.6

func (m *GeneticModelFactory) Create(orders, matchedOrders []*MarketOrder, log *zap.SugaredLogger) OptimizationMethod

type GenomeConfig added in v0.4.6

type GenomeConfig struct {
	NewGenomeLab `json:"-"`
	Type         string
}

func (*GenomeConfig) MarshalText added in v0.4.6

func (m *GenomeConfig) MarshalText() (text []byte, err error)

func (*GenomeConfig) UnmarshalYAML added in v0.4.6

func (m *GenomeConfig) UnmarshalYAML(unmarshal func(interface{}) error) error

type GreedyLinearRegressionModel added in v0.4.4

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

GreedyLinearRegressionModel implements greedy knapsack optimization algorithm. The basic idea is to train the model using BID orders from the marketplace by optimizing multidimensional linear regression over order benchmarks to reduce the number of parameters to a single one - predicted price. This price can be used to assign weights to orders to be able to determine which orders are better to buy than others.

func (*GreedyLinearRegressionModel) Optimize added in v0.4.4

func (m *GreedyLinearRegressionModel) Optimize(ctx context.Context, knapsack *Knapsack, orders []*MarketOrder) error

type GreedyLinearRegressionModelConfig added in v0.4.6

type GreedyLinearRegressionModelConfig struct {
	WeightLimit     float64                `yaml:"weight_limit" default:"1e-3"`
	ExhaustionLimit int                    `yaml:"exhaustion_limit" default:"128"`
	Model           regressionModelFactory `yaml:"regression"`
}

type GreedyLinearRegressionModelFactory added in v0.4.6

type GreedyLinearRegressionModelFactory struct {
	GreedyLinearRegressionModelConfig
}

func (*GreedyLinearRegressionModelFactory) Config added in v0.4.6

func (m *GreedyLinearRegressionModelFactory) Config() interface{}

func (*GreedyLinearRegressionModelFactory) Create added in v0.4.6

func (m *GreedyLinearRegressionModelFactory) Create(orders, matchedOrders []*MarketOrder, log *zap.SugaredLogger) OptimizationMethod

type Knapsack added in v0.4.4

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

func NewKnapsack added in v0.4.4

func NewKnapsack(deviceManager *DeviceManager) *Knapsack

func (*Knapsack) Clone added in v0.4.6

func (m *Knapsack) Clone() *Knapsack

func (*Knapsack) PPSf64 added in v0.4.6

func (m *Knapsack) PPSf64() float64

func (*Knapsack) Plans added in v0.4.4

func (m *Knapsack) Plans() []*sonm.AskPlan

func (*Knapsack) Price added in v0.4.4

func (m *Knapsack) Price() *sonm.Price

func (*Knapsack) Put added in v0.4.4

func (m *Knapsack) Put(order *sonm.Order) error

type MarketCache added in v0.4.4

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

MarketCache is a communication bus between fetching market orders and its consumers. Required, because there are multiple workers can be targeted by Optimus.

func (*MarketCache) ActiveOrders added in v0.4.4

func (m *MarketCache) ActiveOrders(ctx context.Context) ([]*MarketOrder, error)

func (*MarketCache) ExecutedOrders added in v0.4.12

func (m *MarketCache) ExecutedOrders(ctx context.Context, orderType sonm.OrderType) ([]*MarketOrder, error)

type MarketOrder

type MarketOrder = sonm.DWHOrder

type MarketScanner added in v0.4.4

type MarketScanner interface {
	ActiveOrders(ctx context.Context) ([]*MarketOrder, error)
	ExecutedOrders(ctx context.Context, orderType sonm.OrderType) ([]*MarketOrder, error)
}

type Model

type Model interface {
	// Train runs the training process, returning the trained model on success.
	// The "trainingSet" argument is a MxN matrix, while "expectation" must be
	// a M-length vector.
	Train(trainingSet [][]float64, expectation []float64) (TrainedModel, error)
}

Model represents a ML model that can be trained using provided training set with some expectation.

type NewGenomeLab added in v0.4.6

type NewGenomeLab func(knapsack *Knapsack, orders []*MarketOrder) gago.NewGenome

type Normalizer

type Normalizer interface {
	Normalize(x float64) float64
	NormalizeBatch(x []float64)
	Denormalize(x float64) float64
	IsDegenerated() bool
}

type OptimizationConfig added in v0.4.6

type OptimizationConfig struct {
	Model optimizationMethodFactory `yaml:"model"`
}

type OptimizationMethod added in v0.4.4

type OptimizationMethod interface {
	Optimize(ctx context.Context, knapsack *Knapsack, orders []*MarketOrder) error
}

type OptimizationMethodFactory added in v0.4.6

type OptimizationMethodFactory interface {
	Config() interface{}
	Create(orders, matchedOrders []*MarketOrder, log *zap.SugaredLogger) OptimizationMethod
}

type Optimus

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

func NewOptimus

func NewOptimus(cfg *Config, options ...Option) (*Optimus, error)

func (*Optimus) Run

func (m *Optimus) Run(ctx context.Context) error

type Option added in v0.4.5

type Option func(o *options)

func WithLog added in v0.4.5

func WithLog(log *zap.SugaredLogger) Option

func WithVersion added in v0.4.5

func WithVersion(version string) Option

type OrderClassification

type OrderClassification struct {
	WeightedOrders []WeightedOrder
	Predictor      *OrderPredictor
}

OrderClassification is a struct that is returned after market orders classification. Contains weighted orders for some epoch and is able to predict some order's market price.

func (*OrderClassification) RecalculateWeightsAndSort

func (m *OrderClassification) RecalculateWeightsAndSort(orders []WeightedOrder)

type OrderClassifier

type OrderClassifier interface {
	Classify(orders []*MarketOrder) ([]WeightedOrder, error)
}

TODO: Docs.

type OrderPredictor

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

func (*OrderPredictor) PredictPrice

func (m *OrderPredictor) PredictPrice(order *MarketOrder) (float64, error)

type OrderRequestFactory added in v0.4.12

type OrderRequestFactory func() *sonm.OrdersRequest

func DefaultOrderRequestFactory added in v0.4.12

func DefaultOrderRequestFactory(cfg marketplaceConfig) OrderRequestFactory

type Orders added in v0.4.6

type Orders []*MarketOrder

Orders is a slice of market orders.

Introduced to simplify cloning operation.

func (Orders) Clone added in v0.4.6

func (m Orders) Clone() Orders

Clone performs a shallow copy of this orders slice.

func (*Orders) Dedup added in v0.4.13

func (m *Orders) Dedup()

func (Orders) Shuffle added in v0.4.6

func (m Orders) Shuffle(rng *rand.Rand)

Shuffle randomly shuffles this orders slice. This is the modern version of the Fisher–Yates shuffle, because we must support go1.9.

type PredefinedMarketCache added in v0.4.9

type PredefinedMarketCache struct {
	Orders []*MarketOrder
}

func NewPredefinedMarketCache added in v0.4.9

func NewPredefinedMarketCache(orders []*sonm.BigInt, market blockchain.MarketAPI) (*PredefinedMarketCache, error)

func (*PredefinedMarketCache) ActiveOrders added in v0.4.9

func (m *PredefinedMarketCache) ActiveOrders(ctx context.Context) ([]*MarketOrder, error)

func (*PredefinedMarketCache) ExecutedOrders added in v0.4.12

func (m *PredefinedMarketCache) ExecutedOrders(ctx context.Context, orderType sonm.OrderType) ([]*MarketOrder, error)

type PredictorConfig added in v0.4.10

type PredictorConfig struct {
	Blockchain     *blockchain.Config
	DWH            *dwh.DWHConfig
	Marketplace    marketplaceConfig
	BruteThreshold uint `yaml:"brute_threshold" default:"6"`
}

type PredictorService added in v0.4.10

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

func NewPredictorService added in v0.4.10

func NewPredictorService(cfg *PredictorConfig, eth blockchain.API, benchmarkList benchmarks.BenchList, dwh sonm.DWHClient, log *zap.SugaredLogger) *PredictorService

NewPredictorService constructs a new order price predictor service. Returns nil when nil "cfg" is passed.

func (*PredictorService) Classification added in v0.4.10

func (m *PredictorService) Classification() *OrderClassification

func (*PredictorService) Predict added in v0.4.10

func (m *PredictorService) Predict(ctx context.Context, request *sonm.BidResources) (*sonm.Price, error)

func (*PredictorService) PredictSupplier added in v0.4.13

func (m *PredictorService) PredictSupplier(ctx context.Context, request *sonm.PredictSupplierRequest) (*sonm.PredictSupplierReply, error)

func (*PredictorService) Serve added in v0.4.10

func (m *PredictorService) Serve(ctx context.Context) error

type PriceThreshold added in v0.4.10

type PriceThreshold interface {
	// Exceeds returns "true" if the "price" exceeds "otherPrice" according to
	// the implementation's threshold policy.
	Exceeds(price, otherPrice *big.Int) bool
}

type ReadOnlyWorker added in v0.4.9

type ReadOnlyWorker struct {
	WorkerManagementClientAPI
	// contains filtered or unexported fields
}

ReadOnlyWorker is a worker management client wrapper that allows only immutable operations. It returns some default response for operations that mutates something.

func NewReadOnlyWorker added in v0.4.9

func NewReadOnlyWorker(worker WorkerManagementClientAPI) *ReadOnlyWorker

func (*ReadOnlyWorker) AskPlans added in v0.4.9

func (m *ReadOnlyWorker) AskPlans(ctx context.Context, in *sonm.Empty, opts ...grpc.CallOption) (*sonm.AskPlansReply, error)

func (*ReadOnlyWorker) CreateAskPlan added in v0.4.9

func (m *ReadOnlyWorker) CreateAskPlan(ctx context.Context, in *sonm.AskPlan, opts ...grpc.CallOption) (*sonm.ID, error)

func (*ReadOnlyWorker) PurgeAskPlans added in v0.4.9

func (m *ReadOnlyWorker) PurgeAskPlans(ctx context.Context, in *sonm.Empty, opts ...grpc.CallOption) (*sonm.Empty, error)

func (*ReadOnlyWorker) PurgeBenchmarks added in v0.4.9

func (m *ReadOnlyWorker) PurgeBenchmarks(ctx context.Context, in *sonm.Empty, opts ...grpc.CallOption) (*sonm.Empty, error)

func (*ReadOnlyWorker) RemoveAskPlan added in v0.4.9

func (m *ReadOnlyWorker) RemoveAskPlan(ctx context.Context, in *sonm.ID, opts ...grpc.CallOption) (*sonm.Empty, error)

func (*ReadOnlyWorker) RemoveBenchmark added in v0.4.9

func (m *ReadOnlyWorker) RemoveBenchmark(ctx context.Context, in *sonm.NumericID, opts ...grpc.CallOption) (*sonm.Empty, error)

func (*ReadOnlyWorker) ScheduleMaintenance added in v0.4.9

func (m *ReadOnlyWorker) ScheduleMaintenance(ctx context.Context, in *sonm.Timestamp, opts ...grpc.CallOption) (*sonm.Empty, error)

type Registry

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

Registry keeps all used components in itself.

func (*Registry) Close

func (m *Registry) Close()

func (*Registry) NewDWH

func (m *Registry) NewDWH(ctx context.Context, addr auth.Addr, privateKey *ecdsa.PrivateKey) (sonm.DWHClient, error)

func (*Registry) NewWorkerManagement

func (m *Registry) NewWorkerManagement(ctx context.Context, addr auth.Addr, privateKey *ecdsa.PrivateKey) (sonm.WorkerManagementClient, error)

type RegressionModelFactory added in v0.4.6

type RegressionModelFactory interface {
	Config() interface{}
	Create(log *zap.SugaredLogger) Model
}

type RelativePriceThreshold added in v0.4.10

type RelativePriceThreshold struct {
	*big.Int
}

func NewRelativePriceThreshold added in v0.4.10

func NewRelativePriceThreshold(threshold float64) (*RelativePriceThreshold, error)

func ParseRelativePriceThreshold added in v0.4.10

func ParseRelativePriceThreshold(threshold string) (*RelativePriceThreshold, error)

func (*RelativePriceThreshold) Exceeds added in v0.4.10

func (m *RelativePriceThreshold) Exceeds(price, otherPrice *big.Int) bool

type RestrictionsConfig added in v0.4.11

type RestrictionsConfig struct {
	Name     string  `yaml:"cgroup_name" default:"/optimus"`
	CPUCount float64 `yaml:"cpu_count" default:"1.0"`
	// Upper RSS threshold in megabytes.
	//
	// Zero value, which is default, means no limit.
	MemoryLimit uint64 `yaml:"memory_limit"`
}

type SCAKKTModel added in v0.4.6

type SCAKKTModel struct {
	MaxIterations int
	Log           *zap.SugaredLogger
}

func (*SCAKKTModel) Config added in v0.4.6

func (m *SCAKKTModel) Config() interface{}

func (*SCAKKTModel) Create added in v0.4.6

func (m *SCAKKTModel) Create(log *zap.SugaredLogger) Model

func (*SCAKKTModel) Train added in v0.4.6

func (m *SCAKKTModel) Train(trainingSet [][]float64, expectation []float64) (TrainedModel, error)

type Tagger added in v0.4.5

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

func (*Tagger) Tag added in v0.4.5

func (m *Tagger) Tag() []byte

type TrainedModel

type TrainedModel interface {
	Predict(vec []float64) (float64, error)
}

type TrainedSCAKKTModel added in v0.4.6

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

func (*TrainedSCAKKTModel) Predict added in v0.4.6

func (m *TrainedSCAKKTModel) Predict(x []float64) (float64, error)

func (*TrainedSCAKKTModel) String added in v0.4.6

func (m *TrainedSCAKKTModel) String() string

type Watcher

type Watcher interface {
	OnRun()
	OnShutdown()
	Execute(ctx context.Context)
}

type WeightedOrder

type WeightedOrder struct {
	// Order is an initial market order.
	Order *MarketOrder
	// Price is an order price in USD/s.
	Price float64
	// PredictedPrice is a order price in USD/s, that is calculated during
	// scanning and analysing the market.
	PredictedPrice float64
	// Weight represents some specific order weight.
	//
	// It fits in [0; +Inf] range and is used to reduce an order attractiveness
	// if it has been laying on the market for a long time without being sold.
	Weight float64
}

func (*WeightedOrder) ID

func (m *WeightedOrder) ID() *big.Int

type WorkerManagementClientAPI added in v0.4.13

type WorkerManagementClientAPI interface {
	Devices(ctx context.Context, request *sonm.Empty, opts ...grpc.CallOption) (*sonm.DevicesReply, error)
	AskPlans(ctx context.Context, request *sonm.Empty, opts ...grpc.CallOption) (*sonm.AskPlansReply, error)
	CreateAskPlan(ctx context.Context, request *sonm.AskPlan, opts ...grpc.CallOption) (*sonm.ID, error)
	RemoveAskPlan(ctx context.Context, request *sonm.ID, opts ...grpc.CallOption) (*sonm.Empty, error)
	NextMaintenance(ctx context.Context, request *sonm.Empty, opts ...grpc.CallOption) (*sonm.Timestamp, error)
}

type WorkerManagementClientExt added in v0.4.4

type WorkerManagementClientExt interface {
	WorkerManagementClientAPI
	RemoveAskPlans(ctx context.Context, ids []string) error
}

WorkerManagementClientExt extends default "WorkerManagementClient" with an ability to remove multiple ask-plans.

Jump to

Keyboard shortcuts

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