Skip to content

Instantly share code, notes, and snippets.

View ahmedfgad's full-sized avatar

Ahmed Gad ahmedfgad

View GitHub Profile
import numpy
import ga
"""
The y=target is to maximize this equation ASAP:
y = w1x1+w2x2+w3x3+w4x4+w5x5+6wx6
where (x1,x2,x3,x4,x5,x6)=(4,-2,3.5,5,-11,-4.7)
What are the best values for the 6 weights w1 to w6?
We are going to use the genetic algorithm for the best possible values after a number of generations.
"""
@ahmedfgad
ahmedfgad / ga.py
Last active September 7, 2022 21:47
import numpy
# This project is extended and a library called PyGAD is released to build the genetic algorithm.
# PyGAD documentation: https://pygad.readthedocs.io
# Install PyGAD: pip install pygad
# PyGAD source code at GitHub: https://github.com/ahmedfgad/GeneticAlgorithmPython
def cal_pop_fitness(equation_inputs, pop):
# Calculating the fitness value of each solution in the current population.
# The fitness function caulcuates the sum of products between each input and its corresponding weight.
import time
import RPi.GPIO
# Initializing the GPIO pins. The numbering using is board.
RPi.GPIO.setmode(RPi.GPIO.BOARD)
# Configuring the GPIO pin number 8 to be an output pin.
RPi.GPIO.setup(8, RPi.GPIO.OUT)
print("Running Motor.")
import os
import pygame, sys
from pygame.locals import *
import pygame.camera
import time
import RPi.GPIO
import numpy
import os
import pygame.camera
import pygame
#####GPIO#####
# Initializing the GPIO bins. The numbering using is board.
RPi.GPIO.setmode(RPi.GPIO.BOARD)
def crossover(parents, img_shape, n_individuals=8):
new_population = numpy.empty(shape=(n_individuals, functools.reduce(operator.mul, img_shape)), dtype=numpy.uint8)
#Previous parents (best elements).
new_population[0:parents.shape[0], :] = parents
# Getting how many offspring to be generated. If the population size is 8 and number of parents mating is 4, then number of offspring to be generated is 4.
num_newly_generated = n_individuals-parents.shape[0]
# Getting all possible permutations of the selected parents.
parents_permutations = list(itertools.permutations(iterable=numpy.arange(0, parents.shape[0]), r=2))
import os
import sys
import numpy
import scipy.misc
import itertools
import GARI
"""
Reproduce a single image using Genetic Algorithm (GA) by evolving
single pixel values.
import kivy.app
import kivy.uix.label
import kivy.uix.boxlayout
class FirstApp(kivy.app.App):
def build(self):
self.label = kivy.uix.label.Label(text="Hello Kivy")
self.layout = kivy.uix.boxlayout.BoxLayout()
self.layout.add_widget(widget=self.label)
return self.layout
import kivy.app
import kivy.uix.button
import kivy.uix.boxlayout
class FirstApp(kivy.app.App):
def build(self):
self.button1 = kivy.uix.button.Button(text="Button 1")
self.button2 = kivy.uix.button.Button(text="Button 2")
self.button3 = kivy.uix.button.Button(text="Button 3")
self.button4 = kivy.uix.button.Button(text="Button 4")
import kivy.app
import kivy.uix.button
import kivy.uix.label
import kivy.uix.textinput
import kivy.uix.boxlayout
import numpy
class TestApp(kivy.app.App):
def add_nums(self, btn):