OpenAIの最新モデル 2つ目.


つい先日,OpeanAIから2つの新しいモデルが登場した.

今回は2つ目のo3-mini-highを使用してみた.プロンプトは先日と同じ下記のものである.

Langton's AntのシミュレーションをPythonで実装.
商品として販売できるくらい表現をリッチにして.
GIFで出力して.
エラーが出ないか全体を確認して.

(何回かエラー対応が必要であったが)出力は下記である.確かにリッチなものが仕上がった.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Langton's Ant シミュレーション
・セルは 0(明るい色)と 1(暗い色)の2状態
・アリは、白セル(0)の上にいると右90°回転、黒セル(1)の上にいると左90°回転し、セルの色を反転して前進する
・シミュレーションの様子を高品質なグラフィックとして出力し、GIFファイルにまとめる
"""

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
import imageio
import os

def run_langtons_ant_simulation(
    grid_size=300,       # 正方形グリッドのサイズ(セル数)
    num_steps=30000,     # シミュレーションの総ステップ数
    frame_interval=50,   # 何ステップごとにフレームをキャプチャするか
    fps=20,              # GIF のフレームレート
    output_filename="langtons_ant.gif"
):
    # グリッド初期化(0: 明るいセル、1: 暗いセル)
    grid = np.zeros((grid_size, grid_size), dtype=np.uint8)

    # アリの初期位置(グリッド中央)と初期向き(0: 上、1: 右、2: 下、3: 左)
    ant_y, ant_x = grid_size // 2, grid_size // 2
    direction = 0  # 0:上, 1:右, 2:下, 3:左

    # 移動時の行・列の変化量
    dy = [-1, 0, 1, 0]
    dx = [0, 1, 0, -1]

    # フレームを保存するリスト
    frames = []

    # カスタムカラーマップ(セルの色付け):
    # ここでは 0→ライトグレー、1→ダークグレー とし、後からアリを赤で描画
    cmap = ListedColormap(["#e0e0e0", "#333333"])

    # シミュレーションループ
    for step in range(num_steps):
        # 現在のセルの状態を取得
        current_color = grid[ant_y, ant_x]

        # ルールに従ってアリの回転とセルの色反転
        if current_color == 0:
            # 白セルの場合:右に90度回転、セルを黒に
            direction = (direction + 1) % 4
            grid[ant_y, ant_x] = 1
        else:
            # 黒セルの場合:左に90度回転、セルを白に
            direction = (direction - 1) % 4
            grid[ant_y, ant_x] = 0

        # アリを1セル前進(上下左右)
        ant_y += dy[direction]
        ant_x += dx[direction]

        # グリッド外に出たらシミュレーション終了
        if ant_y < 0 or ant_y >= grid_size or ant_x < 0 or ant_x >= grid_size:
            print(f"アリが境界に到達しました (step: {step})。シミュレーションを終了します。")
            break

        # 一定間隔でフレームをキャプチャ
        if step % frame_interval == 0:
            # 高品質な図を作成(図面背景などもこだわり)
            fig, ax = plt.subplots(figsize=(6, 6), dpi=100)
            fig.patch.set_facecolor('#f0f0f0')  # 図全体の背景色
            ax.set_facecolor('#f0f0f0')

            # グリッドを表示(カラーマップ、最近傍補間でセルの状態を鮮明に)
            ax.imshow(grid, cmap=cmap, interpolation='nearest')

            # アリの現在位置を赤いマーカーで表示
            # ※ imshow の x 軸は列番号、y 軸は行番号
            ax.plot(ant_x, ant_y, marker='o', color='red', markersize=8, markeredgecolor='black', markeredgewidth=1)

            # ステップ数を左上に注釈表示
            ax.text(0.02, 0.95, f"Step: {step}", transform=ax.transAxes,
                    color='blue', fontsize=12, fontweight='bold',
                    bbox=dict(facecolor='white', alpha=0.8, edgecolor='none'))

            # 軸は非表示
            ax.set_xticks([])
            ax.set_yticks([])

            # レイアウトを調整してから描画
            plt.tight_layout()

            # キャンバスの内容を NumPy 配列として取得
            fig.canvas.draw()
            width, height = fig.canvas.get_width_height()
            # buffer_rgba を使って RGBA のバッファを取得し、RGB部分のみ抽出
            image = np.frombuffer(fig.canvas.buffer_rgba(), dtype=np.uint8)
            image = image.reshape((height, width, 4))[:, :, :3]  # アルファチャンネルを除去
            frames.append(image)
            plt.close(fig)  # 図を閉じてメモリ解放

    # キャプチャしたフレームを GIF として保存
    if frames:
        imageio.mimsave(output_filename, frames, fps=fps)
        print(f"GIFが正常に保存されました: {os.path.abspath(output_filename)}")
    else:
        print("フレームがキャプチャされなかったため、GIFは作成されませんでした。")

if __name__ == '__main__':
    run_langtons_ant_simulation()

参考文献

  • Langton, C. G. (1986). Studying artificial life with cellular automata. Physica D: nonlinear phenomena, 22(1-3), 120-149.

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です