The Simulation (การจำลอง)

คลาส Simulation สำหรับบทนี้อ้างอิงมาจากคลาสในหัวข้อ "Simulation" โดยมีความแตกต่างอยู่แค่ใน __init__ และ step เท่านั้น

นี่คือเมธอด __init__ :

class PDSimulation(Simulation):

    def __init__(self, tournament, agents):
        self.tournament = tournament
        self.agents = np.asarray(agents)
        self.instruments = []

ออบเจกต์ Simulation ประกอบด้วยออบเจกต์ Tournament ลำดับของ agent และลำดับของออบเจกต์ Instrument (เหมือนในหัวข้อ "Evidence of Evolution" )

นี่คือเมธอด step :

    def step(self):
        self.tournament.melee(self.agents)
        Simulation.step(self)

เมธอด step เวอร์ชันนี้ใช้ Tournament.melee ซึ่งกำหนดค่า attribute fitness ของ agent แต่ละตัว จากนั้นจึงเรียกเมธอด step จากคลาส Simulation ซึ่งแสดงไว้ที่นี่:

# class Simulation

    def step(self):
        n = len(self.agents)
        fits = self.get_fitnesses()

        # see who dies
        index_dead = self.choose_dead(fits)
        num_dead = len(index_dead)

        # replace the dead with copies of the living
        replacements = self.choose_replacements(num_dead, fits)
        self.agents[index_dead] = replacements

        # update any instruments
        self.update_instruments()

Simulation.step จะรวบรวมค่า fitness ของ agent ไว้ใน array จากนั้นเรียก choose_dead เพื่อตัดสินว่า agent ตัวไหนจะตาย และเรียก choose_replacements เพื่อตัดสินว่า agent ตัวไหนจะสืบพันธุ์

simulation ของผมมี differential survival (การอยู่รอดที่แตกต่างกัน) ตามที่กล่าวไว้ในหัวข้อ "Differential Survival" แต่ไม่มี differential reproduction (การสืบพันธุ์ที่แตกต่างกัน) คุณสามารถดูรายละเอียดได้ใน notebook ของบทนี้ และในแบบฝึกหัดข้อหนึ่ง คุณจะมีโอกาสได้สำรวจผลของ differential reproduction ด้วยตัวเอง