Skip to content

Instantly share code, notes, and snippets.

@dfbarrero
dfbarrero / astar.py
Last active March 26, 2019 08:22 — forked from jamiees2/astar.py
A* Algorithm implementation in Python 3.x.
# Enter your code here. Read input from STDIN. Print output to STDOUT
class Node:
def __init__(self,value,point):
self.value = value
self.point = point
self.parent = None
self.H = 0
self.G = 0
def move_cost(self,other):
return 0 if self.value == '.' else 1
@dfbarrero
dfbarrero / tunner.sh
Created September 27, 2019 08:58
Access a remote Jupyter notebook thorught a SSH tunnel in localhost
# On remote server
jupyter notebook --no-browser
# On local host
ssh -N -f -L localhost:8888:localhost:8889 remoteuser@remotehost
@dfbarrero
dfbarrero / onemax-ga.py
Last active February 23, 2022 17:57
Genetic Algorithm implemented with Inspyred to solve the one-max problem, with command-line argument parsing. Some evolution statistics are printed in stdout with CSV format, general info is printed in stderr.
""" Genetic Algorithm that solves the one-max problem.
Genetic Algorithm that solves the one-max problem, many parameters in the GA
can be changed by command line. This script has been designed for teaching
GA. Its code is based on the inspyred library examples.
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
@dfbarrero
dfbarrero / dogs.py
Last active March 13, 2020 09:57
Python OOP naive example.
class Dog:
def __init__(self):
self.name = "Unknown"
self.age = 10
def bit(self):
print(self.name + " has bitten")
def describe(self):
print("Name: ", self.name)
@dfbarrero
dfbarrero / animals.py
Last active March 13, 2020 10:53
Example of inheritance.
class Animal:
def __init__(self):
self.name = "Unknown"
self.age = 10
def describe(self):
print("Name: ", self.name)
print("Age: ", self.age)
class Dog(Animal):
@dfbarrero
dfbarrero / animalsPolymorphism.py
Created March 13, 2020 11:11
Example of polymorphism in Python.
class Animal:
def __init__(self):
self.name = "Unknown"
self.age = 10
def describe(self):
print("Name: ", self.name)
print("Age: ", self.age)
def attack(self):
@dfbarrero
dfbarrero / access.py
Created March 13, 2020 12:07
Example of private attributes in Python.
class Dog:
def __init__(self):
self.__name = "Unknown"
self.__age = 10
def setName(self, name):
self.__name = name
def getName(self):
return self.__name
@dfbarrero
dfbarrero / moonshot.py
Last active April 1, 2020 19:59
Moonshot Inspyred EC algorithm in Python 3.
# Title: Moon probe evolution example
# Author: Mike Vella
# Adapted by David F. Barrero
#start_imports
import os
import math
import pylab
import itertools
from matplotlib import pyplot as plt
import keras
from sacred import Experiment
from sacred.observers import MongoObserver, FileStorageObserver
ex = Experiment("keras-sacred")
#ex.observers.append(MongoObserver())
ex.observers.append(FileStorageObserver("cnn-experiment"))
# Title: Moon probe evolution example
# Author: Mike Vella
#
# Modified by David F. Barrero
# TODO: Change blend crossover by an arithmetic crossover
#start_imports
import os
import math