本文整理汇总了Golang中github.com/dnesting/alife/goalife/grid2d/org.Organism类的典型用法代码示例。如果您正苦于以下问题:Golang Organism类的具体用法?Golang Organism怎么用?Golang Organism使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Organism类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Run
// Run executes Step repeatedly, until Step returns an error, at which point this
// method will invoke o.Die and return.
func (c *Cpu) Run(o *org.Organism) error {
Logger.Printf("%v.Run(%v)\n", c, o)
for {
if err := c.Step(o); err != nil {
Logger.Printf("%v.Run: %v\n", c, err)
o.Die()
return err
}
}
}
示例2: opSenseOthers
// opSenseOthers: sense energy ahead, excluding those with the same bytecode, capped at 255
func opSenseOthers(o *org.Organism, c *Cpu) error {
filter := func(n interface{}) float64 {
if org, ok := n.(*org.Organism); ok {
if nc, ok := org.Driver.(*Cpu); ok {
if c.Hash() == nc.Hash() {
return 0.0
}
}
}
return 1.0
}
c.R[0] = clip(int(o.Sense(filter)), 0, 255)
return nil
}
示例3: Step
// Step executes one CPU operation. Any error returned either assessing the
// operation's energy cost or executing it will be returned by this method.
// Execution is expected to cease (and the organism's Die method
// invoked) if an error is returned.
func (c *Cpu) Step(o *org.Organism) (err error) {
op, ip := c.readOp()
c.Ip = ip
if op == nil {
return unableToReadErr
}
Logger.Printf("%v.Step(%v): %v\n", c, o, op)
// All operations cost at least 1 energy, to avoid infinite loops.
if err := o.Discharge(1 + op.Cost); err != nil {
return err
}
if err := op.Fn(o, c); err != nil {
return err
}
return nil
}
示例4: opDivide
// opDivide: spawn a new organism in neighboring cell with same bytecode
// and energy fraction described by A/256.
func opDivide(o *org.Organism, c *Cpu) error {
lenc := len(c.Code)
if err := o.Discharge(lenc); err != nil {
return err
}
nc := c.Copy()
if rand.Float64() < MutationRate {
nc.Mutate()
}
n, err := o.Divide(nc, float64(c.R[0])/256.0)
if err == org.ErrNotEmpty {
return nil
}
if err != nil {
return err
}
go nc.Run(n)
return nil
}
示例5: opSense
// opSense: sense energy ahead, capped at 255
func opSense(o *org.Organism, c *Cpu) error {
c.R[0] = clip(int(o.Sense(nil)), 0, 255)
return nil
}
示例6: opForward
// opForward: move forward if able
func opForward(o *org.Organism, c *Cpu) error {
if err := o.Forward(); err != nil && err != org.ErrNotEmpty {
return err
}
return nil
}
示例7: opRight
// opRight: turn right
func opRight(o *org.Organism, c *Cpu) error {
o.Right()
return nil
}
示例8: opLeft
// opLeft: turn left
func opLeft(o *org.Organism, c *Cpu) error {
o.Left()
return nil
}
示例9: opEat
// opEat: Consume A*10 energy from neighbor
func opEat(o *org.Organism, c *Cpu) error {
if _, err := o.Eat(c.R[0] * 10); err != nil {
return err
}
return nil
}