133 lines
2.7 KiB
Go
133 lines
2.7 KiB
Go
package auctionsim
|
|
|
|
import (
|
|
"math/rand"
|
|
)
|
|
|
|
/**
|
|
* A Distribution is any stream of float64s.
|
|
*/
|
|
type Distribution interface {
|
|
/**
|
|
* Draw returns the next float64 from the distribution and true, or
|
|
* zero and false if the distribution is exhausted or otherwise unusable.
|
|
*/
|
|
Draw() (float64, bool)
|
|
}
|
|
|
|
/**
|
|
* A BidderGenerator is any stream of bidders.
|
|
*/
|
|
type BidderGenerator interface {
|
|
/**
|
|
* Generate returns the next Bidder and true, or a zero Bidder and false if
|
|
* the generator is exhausted or otherwise unusable.
|
|
*/
|
|
Generate() (Bidder, bool)
|
|
}
|
|
|
|
/**
|
|
* BiddersFromDistributions is a BidderGenerator that draws the bidders' values,
|
|
* irrationality levels, and cash on hand from the provided distributions.
|
|
*/
|
|
type BiddersFromDistributions struct {
|
|
Values Distribution
|
|
Irrationalities Distribution
|
|
Bankrolls Distribution
|
|
}
|
|
|
|
/**
|
|
* Generate implements BidderGenerator.
|
|
*/
|
|
func (b *BiddersFromDistributions) Generate() (Bidder, bool) {
|
|
value, ok := b.Values.Draw()
|
|
if !ok {
|
|
return Bidder{}, false
|
|
}
|
|
irrationality, ok := b.Irrationalities.Draw()
|
|
if !ok {
|
|
return Bidder{}, false
|
|
}
|
|
cash, ok := v.Bankrolls.Draw()
|
|
if !ok {
|
|
return Bidder{}, false
|
|
}
|
|
|
|
return Bidder{
|
|
Value: value,
|
|
Irrationality: irrationality,
|
|
Cash: cash,
|
|
}, true
|
|
}
|
|
|
|
/**
|
|
* NormalDistribution is a Distribution representing the normal distribution
|
|
* with some specified standard deviation and mean. Multiple instances of
|
|
* this type can share a rand.Rand as long as they are not queried concurrently
|
|
* (since rand.Rand is not threadsafe).
|
|
*/
|
|
type NormalDistribution struct {
|
|
Rand *rand.Rand
|
|
StdDev float64
|
|
Mean float64
|
|
}
|
|
|
|
/**
|
|
* Draw implements Distribution.
|
|
*/
|
|
func (n *NormalDistribution) Draw() (float64, bool) {
|
|
return n.Rand.NormFloat64()*n.StdDev + n.Mean, true
|
|
}
|
|
|
|
/**
|
|
* ConstDistribution is a Distribution that always returns the same value.
|
|
*/
|
|
type ConstDistribution float64
|
|
|
|
/**
|
|
* Draw implements Distribution.
|
|
*/
|
|
func (c ConstDistribution) Draw() (float64, bool) {
|
|
return float64(c), true
|
|
}
|
|
|
|
/**
|
|
* CappedDistribution returns some fixed number of values from a Distribution,
|
|
* then stops returning values.
|
|
*/
|
|
type CappedDistribution struct {
|
|
D Distribution
|
|
Lim int64
|
|
}
|
|
|
|
/**
|
|
* Draw implements Distribution.
|
|
*/
|
|
func (c *CappedDistribution) Draw() (float64, bool) {
|
|
if c.Lim <= 0 {
|
|
return 0, false
|
|
}
|
|
c.Lim--
|
|
return c.D.Draw()
|
|
}
|
|
|
|
/**
|
|
* CappedBidderGenerator returns some fixed number of bidders from a
|
|
* BidderGenerator, then stops returning values.
|
|
*/
|
|
type CappedBidderGenerator struct {
|
|
G BidderGenerator
|
|
Lim int64
|
|
}
|
|
|
|
/**
|
|
* Generate implements BidderGenerator.
|
|
*/
|
|
func (c *CappedBidderGenerator) Generate() (float64, bool) {
|
|
if c.Lim <= 0 {
|
|
return Bidder{}, false
|
|
}
|
|
c.Lim--
|
|
return c.G.Generate()
|
|
}
|