Code a python program that implements machine learning and natural selection.
- User
Code a python program that implements machine learning and natural selection.
- Assistant
Here’s a simple example of combining a basic neural‐network “machine‐learning” model with a genetic‐algorithm (“natural selection”) optimizer. In this toy example we evolve the weights of a tiny 2–2–1 feed-forward network to learn the XOR function.
import numpy as np # —————————————————————————————————————————— # 1) Define the problem (XOR) # —————————————————————————————————————————— X = np.array([[0,0], [0,1], [1,0], [1,1]]) Y = np.array([[0], [1], [1], [0]]) # sigmoid activation def sigmoid(x): return 1 / (1 + np.exp(-x)) # forward pass: decode genome→weights, compute NN output def forward(genome, x): # genome: 9 floats W1 = genome[0:6].reshape((3,2)) # 2 inputs + bias → 2 hidden W2 = genome[6:9].reshape((3,1)) # 2 hidden + bias → 1 output # input→hidden x_aug = np.append(x, 1) # add bias input h = sigmoid(x_aug.dot(W1)) # hidden activations # hidden→output h_aug = np.append(h, 1) # add bias to hidden y_hat = sigmoid(h_aug.dot(W2)) return y_hat # fitness: higher is better → we use 1/(1+MSE) def fitness(genome): preds = np.array([forward(genome, x) for x in X]) mse = np.mean((preds - Y)**2) return 1.0 / (1.0 + mse) # —————————————————————————————————————————— # 2) Genetic‐algorithm components # —————————————————————————————————————————— def init_population(pop_size): # random genomes in [-1,1] return [np.random.uniform(-1,1, size=9) for _ in range(pop_size)] def tournament_selection(pop, fits, k=3): # pick k random individuals, return the best idx = np.random.choice(len(pop), k, replace=False) best = idx[np.argmax([fits[i] for i in idx])] return pop[best] def crossover(parent1, parent2): # one‐point crossover point = np.random.randint(1, len(parent1)) child = np.concatenate([parent1[:point], parent2[point:]]) return child def mutate(genome, mutation_rate=0.1, sigma=0.5): # each gene has chance to be perturbed by gaussian noise for i in range(len(genome)): if np.random.rand() < mutation_rate: genome[i] += np.random.normal(0, sigma) # keep in bounds np.clip(genome, -5, 5, out=genome) return genome # —————————————————————————————————————————— # 3) Evolution loop # —————————————————————————————————————————— def evolve(pop_size=100, generations=200): pop = init_population(pop_size) best_ever, best_fit = None, -np.inf for gen in range(generations): # evaluate fitness fits = [fitness(ind) for ind in pop] # record the best i_max = np.argmax(fits) if fits[i_max] > best_fit: best_fit = fits[i_max] best_ever = pop[i_max].copy() # print progress if gen % 20 == 0 or gen == generations-1: print(f"Gen {gen:3d} | Best fitness = {best_fit:.4f}") # create next generation new_pop = [] while len(new_pop) < pop_size: # select parents p1 = tournament_selection(pop, fits) p2 = tournament_selection(pop, fits) # crossover & mutate child = crossover(p1, p2) child = mutate(child) new_pop.append(child) pop = new_pop return best_ever, best_fit # —————————————————————————————————————————— # 4) Run evolution & test # —————————————————————————————————————————— if __name__ == "__main__": best_genome, best_fit = evolve(pop_size=200, generations=300) print("\nBest fitness:", best_fit) print("Testing on XOR:") for x, y in zip(X, Y): y_hat = forward(best_genome, x) print(f" input {x} → predicted {y_hat[0]:.3f} (target {y[0]})")Explanation of the parts:
-
Neural network model
- A 2-2-1 feed-forward net (sigmoid activations), encoded as a flat genome of length 9:
- 6 weights from inputs (+ bias) → hidden
- 3 weights from hidden (+ bias) → output
- A 2-2-1 feed-forward net (sigmoid activations), encoded as a flat genome of length 9:
-
Fitness function
- Mean squared error (MSE) on the 4 XOR patterns
- Converted to a maximization problem via
1/(1+MSE)
-
Genetic operators
- Initialization: random genomes in [−1,1]
- Selection: tournament (size=3)
- Crossover: one-point
- Mutation: gaussian perturbation with clipping
-
Evolution loop
- Over N generations we repeatedly select, mate, mutate, and replace to improve fitness
After a few hundred generations you should see the fitness approach 1.0 and the net will correctly model XOR. Feel free to tweak population size, mutation rate, or extend the network to experiment further! 🚀
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.