DeepSeek R1 のコーディング能力を試す


以前の投稿でGemini Advancedについて色々と試してみた.今回はDeepSeekを試してみる.話題にはなっていたものの今まで触ってみたことはないので色々と見ていきたい.

まずはWeasel Programを試してみたが,ChatGPTともGeminiとも毛色が全く違う出力を出してきた.

出力は下記.(最後の保存のコードがコメントアウトされていたので,その部分のみ手動で修正した)

Weasel Program を可能な限りリッチな表現を用いたコードで書いて.
	
その過程をアニメーションとして出力できるようにして.
# -*- coding: utf-8 -*-
import random
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from matplotlib import cm

# 進化的アルゴリズムの設定
TARGET = "Hello, World!"
POP_SIZE = 1000
MUTATION_RATE = 0.02
N_SURVIVORS = int(POP_SIZE * 0.2)
ALPHABET = [chr(i) for i in range(32, 127)]

# 可視化設定
plt.style.use('dark_background')
fig, ax = plt.subplots(figsize=(12, 6))
ax.set_xlim(0, len(TARGET))
ax.set_ylim(0, POP_SIZE)
ax.set_title("Weasel Program - Evolutionary Process", fontsize=14, color='cyan')

# カラーマップの作成
colors = cm.viridis(np.linspace(0, 1, len(TARGET)))

def generate_individual():
    return [random.choice(ALPHABET) for _ in TARGET]

def calculate_fitness(individual):
    return sum(1 for a, b in zip(individual, TARGET) if a == b)

def mutate(individual):
    return [c if random.random() > MUTATION_RATE else random.choice(ALPHABET) 
            for c in individual]

# アニメーション用データ
scats = ax.scatter([], [], s=30, alpha=0.6)
generation_text = ax.text(0.02, 0.95, '', transform=ax.transAxes, color='white')
best_text = ax.text(0.02, 0.90, '', transform=ax.transAxes, color='yellow')

population = [generate_individual() for _ in range(POP_SIZE)]
best_individual = []
current_gen = 0

def update(frame):
    global population, best_individual, current_gen
    
    # 適応度計算と選択
    fitness = [calculate_fitness(ind) for ind in population]
    sorted_pop = [x for _, x in sorted(zip(fitness, population), reverse=True)]
    survivors = sorted_pop[:N_SURVIVORS]
    
    # 新世代生成
    new_pop = []
    while len(new_pop) < POP_SIZE:
        parent = random.choice(survivors)
        new_pop.append(mutate(parent))
    
    population = new_pop
    best_individual = survivors[0]
    current_gen += 1
    
    # 可視化データ生成
    x_data = np.tile(range(len(TARGET)), POP_SIZE)
    y_data = np.repeat(range(POP_SIZE), len(TARGET))
    colors_array = []
    
    for i, ind in enumerate(population):
        char_colors = [colors[j] if ind[j] == TARGET[j] else (1,1,1,0.1) 
                      for j in range(len(TARGET))]
        colors_array.extend(char_colors)
    
    # プロット更新
    scats.set_offsets(np.column_stack([x_data, y_data]))
    scats.set_color(colors_array)
    
    # テキスト更新
    generation_text.set_text(f'Generation: {current_gen}')
    best_str = ''.join(best_individual)
    best_text.set_text(f'Best Candidate: {best_str}')
    
    # 終了条件
    if ''.join(best_individual) == TARGET:
        ani.event_source.stop()
        ax.text(0.4, 0.5, 'EVOLUTION COMPLETE!', 
                fontsize=24, color='lime', ha='center')
    
    return scats, generation_text, best_text

ani = FuncAnimation(fig, update, frames=200, interval=100, blit=False)
plt.show()

# GIF保存する場合(要ImageMagick)
ani.save('weasel_evolution.gif', writer='imagemagick', fps=15)

コメントを残す

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