首页 > 其他分享 >Langton's Ant

Langton's Ant

时间:2024-08-13 16:19:23浏览次数:7  
标签:turtle ant Performing self cell Ant step Langton

A virtual ant lives on an infinite board, divided into square cells. Each cell can be either black or white. For starters, we can consider an empty (all-white) board with an ant residing in its arbitrary location. The ant operates according to the following rules:
1. If the cell under the ant is white, the ant turns 90° to the right, makes the cell black, and moves one cell forward.
2. If the cell under the ant is black, the ant turns 90° to the left, makes the cell white, and moves one cell forward.
At a glance, this description does not look quite right for a proper cellular automaton: the rules are supposed to specify how a cell switches from one state to another on the basis of cell neighborhood, rather than direct a moving creature over a board. However, the rules of Langton’s ant can be rewritten to follow the usual conventions. Each cell of the board has 10 possible states: (white, no ant), (white, ant looks left), (white, ant looks up), and so on. In other words, a cell state is a combination of its color and ant status. Then the next state of a cell indeed entirely depends on its neighborhood. Say, a cell with the ant will have the opposite color and no ant on the next turn. Similarly, a cell having a left white neighbor with the up-looking ant will have the same color and the right-looking ant on the next turn.

 

import turtle
from dataclasses import dataclass


H = 81
W = 81
SLEEP_MS = 20
CELL_SIZE = 5
SHAPE_SIZE = CELL_SIZE / 20


@dataclass
class SimState:
    to_exit: bool
    paused: bool

    def exit(self):
        exit()

    def pause_resume(self):
        self.paused = not self.paused

    @classmethod
    def setup(cls):
        r = cls(False, False)
        turtle.listen()
        turtle.onkeypress(r.pause_resume, "space")
        turtle.onkeypress(r.exit, "e")
        turtle.onkeypress(r.exit, "E")
        return r


@dataclass
class CellShape:
    shape: turtle.Turtle

    @classmethod
    def create(cls, x, y):
        p = turtle.Turtle()
        p.penup()
        p.shape("circle")
        p.shapesize(SHAPE_SIZE)
        p.goto(x, y)
        p.hideturtle()
        return cls(p)

    def is_black(self):
        return self.shape.isvisible()

    def flip(self):
        if self.is_black():
            self.shape.hideturtle()
        else:
            self.shape.showturtle()


def make_ant(x, y):
    p = turtle.Turtle()
    p.penup()
    p.shape("turtle")
    p.shapesize(SHAPE_SIZE / 2)
    p.goto(x, y)
    p.color("red")
    return p


@dataclass
class WorldState:
    board: list
    ant: turtle.Turtle
    step: int = 0

    def update(self):
        self.step += 1
        if self.step % 1000 == 0:
            print(f"Performing step: {self.step}")

        x = round(self.ant.xcor())
        y = round(self.ant.ycor())
        turn = 90 if self.board[x][y].is_black() else -90

        self.board[x][y].flip()
        self.ant.left(turn)
        self.ant.forward(1)

    @classmethod
    def setup(cls):
        board = [[CellShape.create(x, y) for y in range(H)] for x in range(W)]
        ant = make_ant(W // 2, H // 2)
        return cls(board, ant)


def setup_screen(title):
    turtle.setup(W * CELL_SIZE, H * CELL_SIZE)
    turtle.tracer(0, 0)
    turtle.title(title)
    turtle.setworldcoordinates(0, 0, W, H)


setup_screen("Langton's ant")
sim_state = SimState.setup()
world_state = WorldState.setup()


def tick():
    if not sim_state.to_exit:
        if not sim_state.paused:
            world_state.update()
            turtle.update()
        turtle.ontimer(tick, SLEEP_MS)


tick()
turtle.done()

 

zzh@ZZHPC:/zdata/Github$ /home/zzh/.pyenv/versions/3.12.3/bin/python /zdata/Github/playful-python-modeling-animation/grid/langton_ant.py
Performing step: 1000

 

 

zzh@ZZHPC:/zdata/Github$ /home/zzh/.pyenv/versions/3.12.3/bin/python /zdata/Github/playful-python-modeling-animation/grid/langton_ant.py
Performing step: 1000
Performing step: 2000
Performing step: 3000
Performing step: 4000
Performing step: 5000
Performing step: 6000
Performing step: 7000
Performing step: 8000
Performing step: 9000
Performing step: 10000
Performing step: 11000

 

 

The screenshot in Figure 6.18 shows the situation on the board after the first 1000 steps. This picture shows the lack of any conceivable patterns behind the ant’s trajectory. This impression is even stronger if the ant is watched in real time: its nearly random movements do not seem to form regular shapes. This effect shows the emergence of complex behavior from a set of simple rules, which is actually the whole point of the system. The situation becomes even more complex if we consider the interaction between several ants forming a colony. Interestingly, the trajectory of an ant on an empty board eventually stabilizes after around 10,000 iterations, when the ant begins building a straight “path”, consisting of smaller segments, each taking 104 steps to make (see Figure 6.19). Later experiments showed that Langton’s ant, like many other cellular automata (including “Life”), exhibits a property of computational universality. It means that an ant can theoretically perform any computational algorithm, encoded along with its input data in the initial configuration of the board.

标签:turtle,ant,Performing,self,cell,Ant,step,Langton
From: https://www.cnblogs.com/zhangzhihui/p/18357175

相关文章

  • 使用Vite+TS+Antd构建React项目
    安装Vitenpminstall-gvite#yarnglobaladdvite创建React项目vitecreatemy-react-app--templatereact-ts安装ReactRouternpminstallreact-router-dom@types/react-router-dom#yarnaddreact-router-dom@types/react-router-dom安装AntDesign......
  • Java Reentrantlock可重入锁原理 | 源码探究
    一、基本概念ReentrantLock是Java中提供的一个可重入互斥锁,它是java.util.concurrent.locks包中的一个接口Lock的实现类。ReentrantLock提供了比使用synchronized关键字更强大的锁定机制,例如 公平锁 和 非公平锁 选择、尝试锁定、可中断锁定等。ReentrantLock......
  • 第七期 Semantic Kernel(另一个AI框架,了解即可)
    一:LangChain和SemanticKernel对比https://blog.csdn.net/xiaoqi270620903/article/details/138334622SemanticKernel适用于需要快速构建LLM应用的场景,如智能客服、智能问答等。由于其组件关系简单,开发人员可以快速实现LLM模型的应用,并且可以根据需求进行定制化开发。La......
  • mysql 给了用户所有权限ALL PRIVILEGES,但是该用户没有grant权限
    在MySQL中,给用户ALLPRIVILEGES权限但没有grant权限的情况可能是因为MySQL版本的更新导致了语法的变化。在MySQL8.0及更高版本中,GRANTALLPRIVILEGES的用法已经不再支持,需要使用GRANTALLPRIVILEGESON*.*TO'username'@'host'WITHGRANTOPTION;的格式来授予用户全局权限和......
  • Ant Design Vue 快速上手指南 + 排坑
    引言AntDesignVue(简称ADVue)是基于Vue.js的高质量UI组件库,由蚂蚁金服设计团队开发并维护。它提供了丰富的组件和示例,能够帮助开发者快速构建出美观且易用的Web应用程序。本文将指导你如何快速入门ADVue,并分享一些常见的“坑”以及如何避免它们。安装与配置1.......
  • pydantic实现LLM ReAct
    在今天的AI项目中,大模型的集成几乎成为了一种常态,但如何在保证输出的可控性和解释性的同时利用这些模型执行各种下游任务,一直是一个技术挑战。本文将介绍一个名为ReAct的系统,该系统通过结合大规模语言模型的输出与Python开发紧密合作,提供了一种新颖的解决方案。代码开源在G......
  • [CVPR2022]DASO Distribution-Aware Semantics-Oriented Pseudo-label for Imbalanced
    问题的背景设置:半监督学习下,labeleddata和unlabeleddata的分布不同,且存在类别不平衡。文章提出了一种新的伪标签生成方法:DistributionAwareSemantics-Oriented(DASO)Pseudo-label。首先生成语义伪标签和线性为标签,然后将它们混合实现互补。另外作者的方法不需要估计无标签数......
  • antd-tabs切换数据重复渲染
    两个tabs来回切换的时候,发现一直重复渲染数据,在两个tabs的展示上加一个flag标识,然后用v-if,判断,点击到tab1的时候,flag等于tab1,然后渲染tab1,点击到tab2的时候,flag等于tab2,然后渲染tab2,这样可以避免重复渲染 如下:在handleChangeActivekey方法里设置,点击tab1的时候,让tab1等于tr......
  • ant disign vue pro 使用日期组件,无法动态赋值 解决
    原文地址:https://blog.csdn.net/weixin_43865196/article/details/121849591组件使用渲染<a-date-picker v-model="date" format="YYYY-MM-DD" valueFormat="YYYYMMDD" :allowClear="false" @change="(date,dateStr)=>{ this.da......
  • jmeter+ant+jenkins
    1、先完成jmeter安装参照文章:https://www.cnblogs.com/mimosaling/p/183467472、完成ant安装参照文章:https://www.cnblogs.com/mimosaling/p/183465473、在jmeter的bin目录下,找到jmeter.properties配置文件,修改如下红框中的参数 4、通过ant命令使用ant调用jmeter脚本在Jme......