Skip to content

Instantly share code, notes, and snippets.

View fmder's full-sized avatar

François-Michel De Rainville fmder

  • Québec, Canada
View GitHub Profile
@fmder
fmder / count_calls.py
Created November 2, 2011 14:25
Count function calls decorator
class countCalls(object):
"""Decorator that keeps track of the number of times a function is called.
::
>>> @countCalls
... def foo():
... return "spam"
...
>>> for _ in range(10)
... foo()
import random
from deap import algorithms, base, creator, tools
import sortingnetwork as sn
INIT_SIZE, INPUTS, MAXGEN = 40, 12, 250
H_CXPB, H_MUTPB = 0.5, 0.3
P_CXPB, PMUT_PB = 0.5, 0.3
def evalNetwork(host, parasite, dimension):
network = sn.SortingNetwork(dimension, host)
@fmder
fmder / scoop_tree.py
Created July 31, 2012 14:16
Scoop test with a huge tree
from scoop.futures import map
def root():
for _ in range(100):
r = list(map(node, [range(1000)] * 100))
def node(data):
for _ in range(5):
r1 = sum(map(leaf, [range(1000)] * 10))
@fmder
fmder / pydot_history.py
Last active May 2, 2019 13:35
History example with dot layout
# This file is part of DEAP.
#
# DEAP is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
#
# DEAP is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@fmder
fmder / datalogger.py
Last active December 17, 2015 02:08 — forked from cmd-ntrf/datalogger.py
try:
import numpy
except ImportError:
import platform
if platform.python_implementation() == "PyPy":
import numpypy as numpy
else:
raise ImportError("DEAP requires Numpy.")
from collections import OrderedDict
@fmder
fmder / init.py
Last active December 28, 2015 16:29
Initialization of individuals with other type of generators
import ghalton
import random
import json
from deap import base
from deap import creator
from deap import tools
creator.create("FitnessMax", base.Fitness, weights=(1.0,))
creator.create("Individual", list, fitness=creator.FitnessMax)
@fmder
fmder / generate_update.py
Last active August 29, 2015 13:57
Generate update function with individual logging
from operator import attrgetter
logbook = tools.Logbook()
logbook.header = ['gen', 'nevals'] + (stats.fields if stats else [])
for gen in xrange(ngen):
# Generate a new population
population = toolbox.generate()
# Evaluate the individuals
fitnesses = toolbox.map(toolbox.evaluate, population)
@fmder
fmder / MOCMA.hpp
Created April 9, 2014 16:14
MOCMA.h modification
if(!function.isFeasible(m_pop[mu()+i].searchPoint())){
RealVector t(m_pop[mu()+i].searchPoint());
function.closestFeasible( t );
double dist = norm_sqr( t - m_pop[mu()+i].searchPoint() );
std::cout << dist << std::endl;
m_invalidCount++;
}
import subprocess
try:
tree = subprocess.check_output(["git", "rev-parse", "HEAD"]).strip()
except OSError:
import warnings
warnings.warn("Cannot link examples because we cannot retrieve the git version", Warning)
else:
extlinks = {'example': ('https://github.com/DEAP/deap/blob/{tree}/examples/%s.py'.format(tree=tree), "examples/")}
@fmder
fmder / gist:d3bf5edf7cb535566835
Created June 15, 2015 00:13
Multi distance penality
class ClosestValidMultiPenality(object):
def __init__(self, feasibility, feasible, alpha, distance=None):
self.fbty_fct = feasibility
self.fbl_fct = feasible
self.alpha = alpha
self.dist_fct = distance
def __call__(self, func):
@wraps(func)
def wrapper(individual, *args, **kwargs):