/large-i-mean-venti

(Re)implementations of interesting problems for learning and referencing

Primary LanguagePython

Topics

clustering

k_means.py

Image color clustering into k groups using the k-means method and pixel color visualization on 3D space (where x=R, y=G and z=B).

Example usage

>>> python k_means.py i\mondrian.jpg 5 o\
 (normalize) Δt: 0.0010 seconds
 (cluster) Δt: 0.4169 seconds
 Image saved to o\mondrian5.jpg
Original image Result image (with k=5)

Result image color palette

Pixels with original color Pixels grouped into k=5 clusters

computer-graphics

halftone/

Halftone images made with boxes on a 3D plane using orthographic projection, in both Processing and p5.

Based on this post by Tim Rodenbröker.

Live demo

Demo on the p5.js Web Editor. Use the scroll wheel to change the amount of "particles". You can also try other images by linking them on the preload() function.

1d-fluid-sim/

Simple fluid simulation of two waves in one dimension, visualized with ASCII codes in the terminal.

Based on Doyub Kim's book "Fluid Engine Development"(https://fluidenginedevelopment.org/).

genetic-algorithms

to_be_or_not_to_be.py

A genetic algorithm to find the phrase "To be or not to be." in a pool of random strings.

Based on Daniel Shiffman's "The Nature of Code", chapter 9.

Example usage

>>> python to_be_or_not_to_be.py

procedural-generation

terrain/

3D terrain generation with Perlin noise, based on a video by Daniel Shiffman.

Live demo

Check out a live demo on OpenProcessing (click on Open Controls in the top right to change the terrain height and flight speed).

simulation

boids/

Build upon Daniel Shiffman's Flocking Simulation, based on Craig Reynolds Boids.

Live demo

Live demo on the p5.js Web Editor:

Click on Open Controls in the top right to change the boids' behavior:

swarm-intelligence

particle_swarm.py

A simple implementation of Particle Swarm Optimization (PSO) for minimizing the Rastrigin function.

Heavily based on James McCaffrey's talk.

Example usage

>>> python particle_swarm.py
 Solving Rastrigin's function in 3 variables (known min = 0.0 at (0, 0, 0))
 Setting num_particles  = 50
         max_epochs     = 100
        [min_x, max_x]  = [-10.0, 10.0]

 Epoch = 10, best error = 6.569
 Epoch = 20, best error = 5.884
 Epoch = 30, best error = 2.446
 Epoch = 40, best error = 1.851
 Epoch = 50, best error = 1.239
 Epoch = 60, best error = 0.159
 Epoch = 70, best error = 0.042
 Epoch = 80, best error = 0.002
 Epoch = 90, best error = 0.000

 Best solution found:
  0.0003 0.0007 0.0002
 Error of best solution = 0.000111

honeybee_swarm.py

A simple implementation of Artificial Bee Colony (ABC), a numerical optimization meta-heuristic.

The Hive problem model class

Arguments

  • lower_bound: Variable domain lower bound
  • upper_bound: Variable domain upper bound
  • swarm_size: Total number of bees
  • max_cycles: Maximum number of cycles before halting
  • objective_func: Objective function
  • objective_value: Objective function target value
  • max_unimproved_trials: Maximum number of cycles a bee can exploit it's "food source" before becoming a scout bee

Example usage

Model

target = "supercalifragilistic"
def poppins(vector):
    score = 0
    for gene, target_char in zip(vector, target):
        if gene == ord(target_char):
            score += 1
    return score
dim = len(target)

model = Hive(lower_bound=[ord('a')]*dim,
             upper_bound=[ord('z')]*dim,
             swarm_size=100,
             max_cycles=3000,
             objective_func=poppins,
             objective_value=dim)

Execution

>>> python honeybee_swarm.py
 @cycle  100: best = sdpurkajifnagcliqtia
 @cycle  200: best = sdpercalifnagcliqtia
 @cycle  300: best = sdpercalifnagclistia
 @cycle  400: best = sudercalifragicistic
 @cycle  500: best = sudercalifragicistic
 @cycle  600: best = sudercalifragicistic
 @cycle  700: best = sudercalifragicistic
 @cycle  800: best = sudercalifragicistic
 @cycle  900: best = sudercalifragicistic
 @cycle 1000: best = supercalefragilistic
 @cycle 1006: best = supercalifragilistic
 value    : 20
 solution : ['s','u','p','e','r','c','a','l','i','f','r','a','g','i','l','i','s','t','i','c']