<s> '''<STR_LIT>''' <EOL> import unittest <EOL> import pyvision as pv <EOL> import pyvision . optimize . genetic as ga <EOL> import numpy as np <EOL> import random <EOL> import time <EOL> def callback ( population ) : <EOL> plot = pv . Plot ( x_range = [ <NUM_LIT:0> , <NUM_LIT:10> ] , y_range = [ <NUM_LIT:0> , <NUM_LIT:10> ] ) <EOL> pts = [ [ each [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] . value , each [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] . value ] for each in population ] <EOL> plot . points ( pts ) <EOL> def unitRectCallback ( population ) : <EOL> if random . random ( ) < <NUM_LIT> : <EOL> return <EOL> im = pv . Image ( np . zeros ( ( <NUM_LIT:1000> , <NUM_LIT:1000> ) , dtype = np . float32 ) ) <EOL> for each in population : <EOL> rect = each [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] . generate ( ) <EOL> im . annotateRect ( <NUM_LIT:1000> * rect , color = '<STR_LIT>' ) <EOL> rect = population [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] . generate ( ) <EOL> im . annotatePolygon ( ( <NUM_LIT:1000> * rect ) . asPolygon ( ) , color = '<STR_LIT>' , width = <NUM_LIT:3> ) <EOL> target_rect = pv . CenteredRect ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) <EOL> im . annotatePolygon ( ( <NUM_LIT:1000> * target_rect ) . asPolygon ( ) , color = '<STR_LIT>' , width = <NUM_LIT:3> ) <EOL> class Fitness : <EOL> '''<STR_LIT>''' <EOL> def __call__ ( self , * args , ** kwargs ) : <EOL> val = <NUM_LIT:0.0> <EOL> x , y = args <EOL> cx , cy , sigma = <NUM_LIT> , <NUM_LIT> , <NUM_LIT:1.0> <EOL> dist2 = ( x - cx ) ** <NUM_LIT:2> + ( y - cy ) ** <NUM_LIT:2> <EOL> scale = <NUM_LIT:1.0> / ( <NUM_LIT:2> * np . pi * sigma ** <NUM_LIT:2> ) <EOL> val += scale * np . exp ( - dist2 / ( <NUM_LIT:2> * sigma ** <NUM_LIT:2> ) ) <EOL> cx , cy , sigma = <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0.5> <EOL> dist2 = ( x - cx ) ** <NUM_LIT:2> + ( y - cy ) ** <NUM_LIT:2> <EOL> scale = <NUM_LIT:1.0> / ( <NUM_LIT:2> * np . pi * sigma ** <NUM_LIT:2> ) <EOL> val += scale * np . exp ( - dist2 / ( <NUM_LIT:2> * sigma ** <NUM_LIT:2> ) ) <EOL> cx , cy , sigma = <NUM_LIT> , <NUM_LIT> , <NUM_LIT> <EOL> dist2 = ( x - cx ) ** <NUM_LIT:2> + ( y - cy ) ** <NUM_LIT:2> <EOL> scale = <NUM_LIT:1.0> / ( <NUM_LIT:2> * np . pi * sigma ** <NUM_LIT:2> ) <EOL> val += scale * np . exp ( - dist2 / ( <NUM_LIT:2> * sigma ** <NUM_LIT:2> ) ) <EOL> return - val <EOL> class FitnessInt : <EOL> '''<STR_LIT>''' <EOL> def __call__ ( self , * args , ** kwargs ) : <EOL> val = <NUM_LIT:0.0> <EOL> x , y = args <EOL> x = <NUM_LIT> * x <EOL> y = <NUM_LIT> * y <EOL> cx , cy , sigma = <NUM_LIT> , <NUM_LIT> , <NUM_LIT:1.0> <EOL> dist2 = ( x - cx ) ** <NUM_LIT:2> + ( y - cy ) ** <NUM_LIT:2> <EOL> scale = <NUM_LIT:1.0> / ( <NUM_LIT:2> * np . pi * sigma ** <NUM_LIT:2> ) <EOL> val += scale * np . exp ( - dist2 / ( <NUM_LIT:2> * sigma ** <NUM_LIT:2> ) ) <EOL> cx , cy , sigma = <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0.5> <EOL> dist2 = ( x - cx ) ** <NUM_LIT:2> + ( y - cy ) ** <NUM_LIT:2> <EOL> scale = <NUM_LIT:1.0> / ( <NUM_LIT:2> * np . pi * sigma ** <NUM_LIT:2> ) <EOL> val += scale * np . exp ( - dist2 / ( <NUM_LIT:2> * sigma ** <NUM_LIT:2> ) ) <EOL> cx , cy , sigma = <NUM_LIT> , <NUM_LIT> , <NUM_LIT> <EOL> dist2 = ( x - cx ) ** <NUM_LIT:2> + ( y - cy ) ** <NUM_LIT:2> <EOL> scale = <NUM_LIT:1.0> / ( <NUM_LIT:2> * np . pi * sigma ** <NUM_LIT:2> ) <EOL> val += scale * np . exp ( - dist2 / ( <NUM_LIT:2> * sigma ** <NUM_LIT:2> ) ) <EOL> return - val <EOL> class FitnessNapsack : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self ) : <EOL> self . napsack = [ [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] ] <EOL> self . napsack = <NUM_LIT:50> + np . array ( self . napsack ) <EOL> self . max_weight = <NUM_LIT> <EOL> def solve ( self ) : <EOL> weights = self . napsack [ : , <NUM_LIT:0> ] . flatten ( ) <EOL> values = self . napsack [ : , <NUM_LIT:1> ] . flatten ( ) <EOL> density = values / weights <EOL> order = density . argsort ( ) [ : : - <NUM_LIT:1> ] <EOL> weights = weights [ order ] <EOL> values = values [ order ] <EOL> density = density [ order ] <EOL> _ = self . greedy ( weights , values , self . max_weight ) <EOL> def greedy ( self , weights , values , remaining ) : <EOL> if len ( weights ) == <NUM_LIT:0> : <EOL> return [ ] , <NUM_LIT:0.0> , remaining <EOL> if weights [ <NUM_LIT:0> ] <= remaining : <EOL> solution , score , remaining = self . greedy ( weights [ <NUM_LIT:1> : ] , values [ <NUM_LIT:1> : ] , remaining - weights [ <NUM_LIT:0> ] ) <EOL> return [ True ] + solution , values [ <NUM_LIT:0> ] + score , remaining <EOL> else : <EOL> solution , score , remaining = self . greedy ( weights [ <NUM_LIT:1> : ] , values [ <NUM_LIT:1> : ] , remaining ) <EOL> return [ False ] + solution , score , remaining <EOL> def greedy_soft ( self , weights , values , remaining ) : <EOL> if len ( weights ) == <NUM_LIT:0> : <EOL> return <NUM_LIT:0.0> <EOL> if weights [ <NUM_LIT:0> ] <= remaining : <EOL> score = self . greedy_soft ( weights [ <NUM_LIT:1> : ] , values [ <NUM_LIT:1> : ] , remaining - weights [ <NUM_LIT:0> ] ) <EOL> return values [ <NUM_LIT:0> ] + score <EOL> else : <EOL> frac = float ( remaining ) / weights [ <NUM_LIT:0> ] <EOL> score = frac * values [ <NUM_LIT:0> ] <EOL> return score <EOL> def search ( self , weights , values , remaining , best , score , solution ) : <EOL> if len ( weights ) == <NUM_LIT:0> and score > best : <EOL> return score <EOL> soft = self . greedy_soft ( weights , values , remaining ) <EOL> if score + soft > best : <EOL> if weights [ <NUM_LIT:0> ] <= remaining : <EOL> new_score = self . search ( weights [ <NUM_LIT:1> : ] , values [ <NUM_LIT:1> : ] , remaining - weights [ <NUM_LIT:0> ] , best , score + values [ <NUM_LIT:0> ] , solution + [ True ] ) <EOL> if new_score > best : <EOL> best = new_score <EOL> new_score = self . search ( weights [ <NUM_LIT:1> : ] , values [ <NUM_LIT:1> : ] , remaining , best , score , solution + [ False ] ) <EOL> if new_score > best : <EOL> best = new_score <EOL> return best <EOL> def __call__ ( self , * args , ** kwargs ) : <EOL> ranking = args [ <NUM_LIT:0> ] <EOL> weight = <NUM_LIT:0.0> <EOL> value = <NUM_LIT:0.0> <EOL> for i in ranking : <EOL> weight += self . napsack [ i , <NUM_LIT:0> ] <EOL> if weight < self . max_weight : <EOL> value += self . napsack [ i , <NUM_LIT:1> ] <EOL> else : <EOL> break <EOL> return - value <EOL> def fitnessUnitRect ( rect , ** kwargs ) : <EOL> target_rect = pv . CenteredRect ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) <EOL> return - target_rect . overlap ( rect ) <EOL> def fitnessUnitVector ( vec , ** kwargs ) : <EOL> target = [ <NUM_LIT:1.0> , <NUM_LIT:1.0> , <NUM_LIT:1.0> , <NUM_LIT:1.0> , <NUM_LIT:1.0> , <NUM_LIT:1.0> , <NUM_LIT:1.0> , <NUM_LIT:1.0> , <NUM_LIT:1.0> ] <EOL> sum_sq = <NUM_LIT:0.0> <EOL> for i in range ( len ( vec ) ) : <EOL> sum_sq += ( target [ i ] - vec [ i ] ) ** <NUM_LIT:2> <EOL> score = np . sqrt ( sum_sq ) <EOL> return score <EOL> class GeneticAlgorithmTest ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> pass <EOL> def tearDown ( self ) : <EOL> pass <EOL> def test2DSurfaceFloatMP ( self ) : <EOL> alg = ga . GeneticAlgorithm ( Fitness ( ) , [ ga . GAFloat ( <NUM_LIT:0.0> , <NUM_LIT> ) , ga . GAFloat ( <NUM_LIT:0.0> , <NUM_LIT> ) ] , n_processes = <NUM_LIT:4> ) <EOL> _ = alg . optimize ( max_iter = <NUM_LIT:1000> ) <EOL> def test2DSurfaceFloatSP ( self ) : <EOL> alg = ga . GeneticAlgorithm ( Fitness ( ) , [ ga . GAFloat ( <NUM_LIT:0.0> , <NUM_LIT> ) , ga . GAFloat ( <NUM_LIT:0.0> , <NUM_LIT> ) ] , n_processes = <NUM_LIT:1> ) <EOL> _ = alg . optimize ( max_iter = <NUM_LIT> , callback = callback ) <EOL> def test2DSurfaceIntSP ( self ) : <EOL> alg = ga . GeneticAlgorithm ( FitnessInt ( ) , [ ga . GAInteger ( <NUM_LIT:0> , <NUM_LIT:1000> ) , ga . GAInteger ( <NUM_LIT:0> , <NUM_LIT:1000> ) ] , n_processes = <NUM_LIT:1> ) <EOL> _ = alg . optimize ( max_iter = <NUM_LIT:1000> ) <EOL> def testNapsack ( self ) : <EOL> fitness = FitnessNapsack ( ) <EOL> alg = ga . GeneticAlgorithm ( fitness , [ ga . GARanking ( <NUM_LIT:100> ) ] , n_processes = <NUM_LIT:1> ) <EOL> result = alg . optimize ( max_iter = <NUM_LIT:1000> ) <EOL> def testRestart ( self ) : <EOL> fitness = FitnessNapsack ( ) <EOL> ilog = pv . ImageLog ( ) <EOL> restart_dir = ilog . dir <EOL> alg = ga . GeneticAlgorithm ( fitness , [ ga . GARanking ( <NUM_LIT:100> ) ] , n_processes = <NUM_LIT:1> ) <EOL> result = alg . optimize ( max_iter = <NUM_LIT> , ilog = ilog ) <EOL> time . sleep ( <NUM_LIT:2> ) <EOL> print "<STR_LIT>" <EOL> ilog = pv . ImageLog ( ) <EOL> alg = ga . GeneticAlgorithm ( fitness , [ ga . GARanking ( <NUM_LIT:100> ) ] , n_processes = <NUM_LIT:1> ) <EOL> result = alg . optimize ( max_iter = <NUM_LIT:1000> , ilog = ilog , restart_dir = restart_dir ) <EOL> def testGAUnitVector ( self ) : <EOL> alg = ga . GeneticAlgorithm ( fitnessUnitVector , [ ga . GAUnitVector ( <NUM_LIT:9> ) ] , population_size = <NUM_LIT:20> , n_processes = <NUM_LIT:1> ) <EOL> _ = alg . optimize ( max_iter = <NUM_LIT> ) <EOL> def testGAUnitRect ( self ) : <EOL> alg = ga . GeneticAlgorithm ( fitnessUnitRect , [ ga . GAUnitRect2 ( min_width = <NUM_LIT> , min_height = <NUM_LIT> , max_height = <NUM_LIT:1.0> , max_width = <NUM_LIT:1.0> ) ] , population_size = <NUM_LIT:20> , n_processes = <NUM_LIT:4> ) <EOL> _ = alg . optimize ( max_iter = <NUM_LIT:1000> , callback = unitRectCallback ) <EOL> def testCircularRange ( self ) : <EOL> self . assertAlmostEqual ( ga . _circularRange ( <NUM_LIT:10> , - np . pi , np . pi ) , <NUM_LIT:10> - <NUM_LIT:4> * np . pi ) <EOL> self . assertAlmostEqual ( ga . _circularRange ( <NUM_LIT> , - np . pi , np . pi ) , <NUM_LIT> - <NUM_LIT:16> * np . pi ) <EOL> self . assertAlmostEqual ( ga . _circularRange ( - <NUM_LIT:10> , - np . pi , np . pi ) , - <NUM_LIT:10> + <NUM_LIT:4> * np . pi ) <EOL> self . assertAlmostEqual ( ga . _circularRange ( - <NUM_LIT> , - np . pi , np . pi ) , - <NUM_LIT> + <NUM_LIT:16> * np . pi ) <EOL> self . assertAlmostEqual ( ga . _circularRange ( - <NUM_LIT> , - np . pi , np . pi ) , - <NUM_LIT> ) <EOL> self . assertAlmostEqual ( ga . _circularRange ( <NUM_LIT:0.5> , - np . pi , np . pi ) , <NUM_LIT:0.5> ) <EOL> def testGAAngle ( self ) : <EOL> for _ in range ( <NUM_LIT:100> ) : <EOL> ang = ga . GAAngle ( ) <EOL> for _ in range ( <NUM_LIT:100> ) : <EOL> ang = ga . GAAngle ( mutation_rate = <NUM_LIT:0.5> ) <EOL> ang . mutate ( ) <EOL> for _ in range ( <NUM_LIT:100> ) : <EOL> ang1 = ga . GAAngle ( ) <EOL> ang2 = ga . GAAngle ( ) <EOL> ang2 . value = ang1 . value + <NUM_LIT:0.1> <EOL> ang2 . clipRange ( ) <EOL> ang1 . combine ( ang2 ) <EOL> for _ in range ( <NUM_LIT:100> ) : <EOL> ang1 = ga . GAAngle ( ) <EOL> ang2 = ga . GAAngle ( ) <EOL> ang2 . value = ang1 . value + <NUM_LIT:1> <EOL> ang2 . clipRange ( ) <EOL> ang1 . combine ( ang2 ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> print "<STR_LIT>" <EOL> unittest . main ( ) </s>
