numpy 在Python 3中使两个方法交互

wko9yo5t  于 2023-06-23  发布在  Python
关注(0)|答案(1)|浏览(104)

我正在尝试创建一个简单的模拟。有一个猎人的职业是猎鹿的。我已经给了猎人一个击中目标的可能性,给了鹿一个躲避目标的可能性。
我首先介绍我将在整个脚本中使用的库和一些值:

import random
import simpy
import numpy as np

DEER = 40
DISTANCE = 100
EVASION_CHANCE = 0.85
HIT_CHANCE = 0.60
SIM_TIME = 100

deer_hunted = 0

然后定义Hunter类:

class Hunter:
    def __innit__(self, env, deer, evasion_chance):
        self.env = env
        self.game= simpy.Resource(env, deer)
        self.evasion_chance = evasion_chance

    def attack(self, env, hunter, hit_chance):
        random_time = max(1, np.random.normal(self.evasion_chance, 0.60))
        yield self.env.timeout(random_time)
        self.env = env
        self.sam = simpy.Resource(env, hunter)
        self.hit_chance = hit_chance

然后是场景或动作:

def scenario(env, engagement):
    global deer_hunted
    print(f"deer enters the zone.")
    with engagement.deer() as request:
        yield request
        print(f"deer enters danger zone.")
        yield env.process(engagement.attack)
        print(f"deer has been hit")
        deer_downed += 1

然后是模拟指令:

def setup(env, deer, evasion_chance, hit_chance):
    engagement = Hunter(env, deer, evasion_chance, hit_chance)

    for i in range(1 , 5):
        env.process(engagement(env, i, engagement))

        while True:
            yield env.timeout(random.randint(hit_chance -1, hit_chance +1))
            i += 1
            env.process(engagement(env, deer, engagement))

print("Starting Simulation")
env = simpy.Environment()
env.process(setup(env, deer_hunted, EVASION_CHANCE, HIT_CHANCE))
env.run(until1=SIM_TIME)

print("Deer Hunted: " +str(deer_hunted))

我正在看猎人如何对付前面定义的40只鹿,考虑到准确性和错过的机会。我也想在稍后的时间点集成距离,但在此之前,我需要让这个脚本自己工作。尽管我的屏幕上没有任何语法错误的信号,但我在VS Code上得到了语法错误。我做错了什么?

lf5gs5x2

lf5gs5x21#

很可能只是__init__()的拼写错误让你搞砸了。
即这个

class Hunter:
    def __innit__(self, env, deer, evasion_chance):
        self.env = env
        self.game= simpy.Resource(env, deer)
        self.evasion_chance = evasion_chance

应该是这样的:

class Hunter:
    def __init__(self, env, deer, evasion_chance):
        self.env = env
        self.game= simpy.Resource(env, deer)
        self.evasion_chance = evasion_chance

相关问题