基于 SOC 职业分类
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
直接命令不会经过审查 Prompt;运行前请先检查来源。
npx skills add https://github.com/ffsshhttiikk/opencode-agents-skills --skill genetic-algorithms命令会保持在同一行。复制前请横向滚动并检查完整内容。
想先保存到本地?可下载 SkillsMP 当前能够提供的文件。
正在显示 SKILL.md
| name | genetic-algorithms |
| description | Genetic algorithm optimization |
| license | MIT |
| compatibility | opencode |
| metadata | {"audience":"machine-learning-engineers","category":"artificial-intelligence"} |
Use me when:
┌──────────────┐
│ Population │◀──────────────┐
│ Generation │ │
└──────┬───────┘ │
│ │
▼ │
┌──────────────┐ ┌─────────┐│
│ Evaluate │ │ Select ││
│ Fitness │───▶│ Parents││
└──────────────┘ └────┬────┘
│
▼
┌─────────────┐
│ Crossover │
│ + Mutate │
└──────┬──────┘
│
▼
┌─────────────┐
│ Replace │
│ Population │
└──────┬──────┘
│
▼
┌──────────────┐
│ Continue? │
└──────────────┘
import random
import numpy as np
class GeneticAlgorithm:
def __init__(self, pop_size=100, mutation_rate=0.1,
crossover_rate=0.8):
self.pop_size = pop_size
self.mutation_rate = mutation_rate
self.crossover_rate = crossover_rate
def init_population(self):
return [self.create_individual() for _ in range(self.pop_size)]
def fitness(self, individual):
raise NotImplemented
def selection(self, population, fitnesses):
# Tournament selection
selected = []
for _ in range(len(population)):
i, j = random.sample(range(len(population)), 2)
if fitnesses[i] > fitnesses[j]:
selected.append(population[i].copy())
else:
selected.append(population[j].copy())
return selected
def crossover(self, parent1, parent2):
random.random() > .crossover_rate:
parent1.copy(), parent2.copy()
point = random.randint(, (parent1) - )
child1 = parent1[:point] + parent2[point:]
child2 = parent2[:point] + parent1[point:]
child1, child2
():
i ((individual)):
random.random() < .mutation_rate:
individual[i] = .random_gene()
individual
():
population = .init_population()
gen (generations):
fitnesses = [.fitness(ind) ind population]
best_idx = np.argmax(fitnesses)
new_pop = [population[best_idx].copy()]
parents = .selection(population, fitnesses)
i (, (parents) - , ):
child1, child2 = .crossover(parents[i], parents[i+])
new_pop.append(.mutate(child1))
new_pop.append(.mutate(child2))
population = new_pop[:.pop_size]
(population, key=.fitness)