-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGA Art Three.py
107 lines (89 loc) · 4.01 KB
/
GA Art Three.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import numpy as np
import matplotlib.pyplot as plt
# Genetic Algorithm Parameters
population_size = 30
num_generations = 100
mutation_rate = 0.1
image_resolution = (300, 300)
# Fitness Function: Enhanced to reward fractal-like patterns and symmetry
def fitness_function(image):
complexity = calculate_complexity(image)
symmetry = calculate_symmetry(image)
contrast = global_contrast_factor(image)
return complexity + symmetry + contrast
# Calculate Complexity using edge detection
def calculate_complexity(image):
gradient_x = np.abs(np.diff(image, axis=0))
gradient_y = np.abs(np.diff(image, axis=1))
complexity = np.sum(gradient_x) + np.sum(gradient_y)
return complexity / image.size
# Global Contrast Factor (GCF)
def global_contrast_factor(image):
luminance = np.mean(image, axis=2) # Average across RGB channels
contrast = np.std(luminance) # Standard deviation as a simple contrast measure
return contrast
# Calculate Symmetry
def calculate_symmetry(image):
vertical_symmetry = np.sum(np.abs(image - np.flip(image, axis=1)))
horizontal_symmetry = np.sum(np.abs(image - np.flip(image, axis=0)))
total_symmetry = -(vertical_symmetry + horizontal_symmetry) / image.size # Negate to reward symmetry
return total_symmetry
# Initialize Population with fractal-like patterns
def initialize_population(size, resolution):
population = []
for _ in range(size):
x = np.linspace(-2.0, 2.0, resolution[0])
y = np.linspace(-2.0, 2.0, resolution[1])
x, y = np.meshgrid(x, y)
fractal = np.sin(x ** 2 + y ** 2) * 127 + 128
fractal = fractal.astype(np.uint8)
image = np.stack([fractal, fractal, fractal], axis=2) # Grayscale to RGB
population.append(image)
return population
# Crossover Operation
def crossover(parent1, parent2):
crossover_point = np.random.randint(0, parent1.shape[1])
child = np.concatenate((parent1[:, :crossover_point], parent2[:, crossover_point:]), axis=1)
return child
# Mutation Operation (structured fractal adjustments)
def mutate(image, rate):
mutated_image = image.copy()
num_pixels = np.prod(image.shape[:2])
num_mutations = int(rate * num_pixels)
for _ in range(num_mutations):
x, y = np.random.randint(0, image.shape[0]), np.random.randint(0, image.shape[1])
mutated_image[x, y] = np.clip(mutated_image[x, y] + np.random.randint(-50, 50), 0, 255)
return mutated_image
# Main GA Loop
population = initialize_population(population_size, image_resolution)
for generation in range(num_generations):
print(f"Generation {generation + 1}: Evaluating fitness")
# Evaluate Fitness
fitness_scores = [fitness_function(image) for image in population]
best_fitness = max(fitness_scores)
print(f" Best fitness: {best_fitness:.2f}")
# Select Parents (Roulette Wheel Selection)
total_fitness = sum(fitness_scores)
probabilities = [score / total_fitness for score in fitness_scores]
indices = np.arange(len(population)) # Indices for the population
selected_indices = np.random.choice(indices, size=population_size, p=probabilities, replace=True)
parents = [population[i] for i in selected_indices]
print(" Parents selected")
# Generate New Population
new_population = []
for i in range(0, len(parents), 2):
parent1, parent2 = parents[i], parents[(i + 1) % len(parents)]
child = crossover(parent1, parent2)
child = mutate(child, mutation_rate)
new_population.append(child)
population = new_population
print(" New population generated")
# Safely find the best image
best_index = np.argmax(fitness_scores)
best_image = population[best_index]
# Display the best image
plt.imshow(best_image, cmap='inferno')
plt.title(f"Generation {generation + 1} - Best Fitness: {best_fitness:.2f}")
plt.axis('off')
plt.pause(0.5)
print("Evolutionary art generation complete!")