@@ -2751,7 +2751,8 @@ def populate_species(self,count, parent_count):
2751
2751
self .incest_mutation_multiplyer = 2.
2752
2752
self .incest_mutation_count_multiplyer = 2.
2753
2753
else :
2754
- if random .random ()< .01 : print_ (self .species_distance2 (parent1 , parent2 ))
2754
+ pass
2755
+ # if random.random()<.01 : print_(self.species_distance2(parent1, parent2))
2755
2756
start_gene = random .randint (0 ,self .genes_count )
2756
2757
end_gene = (max (1 ,random .randint (0 ,self .genes_count ),int (self .genes_count / 4 ))+ start_gene ) % self .genes_count
2757
2758
if end_gene < start_gene :
@@ -2809,24 +2810,82 @@ def test(self,test_function):
2809
2810
self .population [i ][0 ] = (b [3 ]- b [1 ])* (b [2 ]- b [0 ])
2810
2811
self .population .sort ()
2811
2812
2812
-
2813
2813
def test_spiece_centroid (self ,spiece ) :
2814
2814
poly = Polygon ( self .polygons [spiece [0 ][0 ]].polygon [:])
2815
- poly .rotate (spiece [0 ][2 ]* math .pi2 )
2815
+ poly .rotate (spiece [0 ][1 ]* math .pi2 )
2816
2816
surface = Polygon (poly .polygon )
2817
- i = 0
2818
2817
for p in spiece [1 :] :
2819
- i += 1
2820
2818
poly = Polygon (self .polygons [p [0 ]].polygon [:])
2821
- poly .rotate (p [2 ]* math .pi2 )
2822
2819
c = surface .centroid ()
2820
+ surface .move (- c [0 ],- c [1 ])
2823
2821
c1 = poly .centroid ()
2824
- direction = [math .cos (p [1 ]* math .pi2 ), - math .sin (p [1 ]* math .pi2 )]
2825
- poly .move (c [0 ]- c1 [0 ]- direction [0 ]* 100 ,c [1 ]- c1 [1 ]- direction [1 ]* 100 )
2826
- poly .drop_into_direction (direction ,surface )
2822
+ poly .move (- c1 [0 ],- c1 [1 ])
2823
+ poly .rotate (p [1 ]* math .pi2 + p [2 ]* math .pi2 )
2824
+ surface .rotate (p [2 ]* math .pi2 )
2825
+ poly .drop_down (surface )
2827
2826
surface .add (poly )
2827
+ surface .rotate (- p [2 ]* math .pi2 )
2828
2828
return surface
2829
2829
2830
+
2831
+ def test_inline (self ) :
2832
+ ###
2833
+ ### Fast test function using weave's from scipy inline function
2834
+ ###
2835
+ try :
2836
+ converters is None
2837
+ except :
2838
+ try :
2839
+ from scipy import weave
2840
+ from scipy .weave import converters
2841
+ except :
2842
+ options .self .error ("For this function Scipy is needed. See http://www.cnc-club.ru/gcodetools for details." ,"error" )
2843
+
2844
+ # Prepare vars
2845
+ poly_ , subpoly_ , points_ = [], [], []
2846
+ for poly in self .polygons :
2847
+ p = poly .polygon
2848
+ poly_ += [len (subpoly_ ), len (subpoly_ )+ len (p )* 2 ]
2849
+ for subpoly in p :
2850
+ subpoly_ += [len (points_ ), len (points_ )+ len (subpoly )* 2 + 2 ]
2851
+ for point in subpoly :
2852
+ points_ += point
2853
+ points_ += subpoly [0 ] # Close subpolygon
2854
+
2855
+ test_ = []
2856
+ population_ = []
2857
+ for spiece in self .population :
2858
+ test_ .append ( spiece [0 ] if spiece [0 ] != None else - 1 )
2859
+ for sp in spiece [1 ]:
2860
+ population_ += sp
2861
+
2862
+ lp_ , ls_ , l_ , lt_ = len (poly_ ), len (subpoly_ ), len (points_ ), len (test_ )
2863
+
2864
+ f = open ('inline_test.c' , 'r' )
2865
+ code = f .read ()
2866
+ f .close ()
2867
+
2868
+ f = open ('inline_test_functions.c' , 'r' )
2869
+ functions = f .read ()
2870
+ f .close ()
2871
+
2872
+ stdout_ = sys .stdout
2873
+ s = ''
2874
+ sys .stdout = s
2875
+
2876
+ test = weave .inline (
2877
+ code ,
2878
+ ['points_' ,'subpoly_' ,'poly_' , 'lp_' , 'ls_' , 'l_' , 'lt_' ,'test_' , 'population_' ],
2879
+ compiler = 'gcc' ,
2880
+ support_code = functions ,
2881
+ )
2882
+ if s != '' : options .self .error (s ,"warning" )
2883
+ sys .stdout = stdout_
2884
+
2885
+ for i in range (len (test_ )):
2886
+ self .population [i ][0 ] = test_ [i ]
2887
+
2888
+
2830
2889
2831
2890
2832
2891
#surface.draw()
@@ -2906,7 +2965,7 @@ def arrangement(self) :
2906
2965
time_ = time .time ()
2907
2966
pop = copy .deepcopy (population )
2908
2967
population_count = self .options .arrangement_population_count
2909
- last_champ = []
2968
+ last_champ = - 1
2910
2969
champions_count = 0
2911
2970
2912
2971
@@ -2932,29 +2991,27 @@ def arrangement(self) :
2932
2991
population.order_mutation_factor = 1./(i%100-79) if 80<=i%100<100 else 1.
2933
2992
population.populate_species(250, 10)
2934
2993
"""
2935
- population .test (population .test_spiece_centroid )
2994
+ if self .options .arrangement_inline_test :
2995
+ population .test_inline ()
2996
+ else :
2997
+ population .test (population .test_spiece_centroid )
2998
+
2936
2999
print_ ("Test done at %s" % (time .time ()- time_ ))
2937
3000
draw_new_champ = False
2938
3001
print_ ()
2939
- for x in population .population [:10 ]:
2940
- print_ (x [0 ])
3002
+
2941
3003
2942
3004
if population .population [0 ][0 ]!= last_champ :
2943
3005
draw_new_champ = True
3006
+ improve = last_champ - population .population [0 ][0 ]
2944
3007
last_champ = population .population [0 ][0 ]* 1
2945
-
2946
3008
2947
- k = ""
2948
- #for j in range(10) :
2949
- # k += "%s " % population.population[j][0]
3009
+
2950
3010
print_ ("Cicle %s done in %s" % (i ,time .time ()- time_ ))
2951
3011
time_ = time .time ()
2952
-
2953
-
2954
3012
print_ ("%s incests been found" % population .inc )
2955
3013
print_ ()
2956
- #print_(k)
2957
- #print_()
3014
+
2958
3015
if i == 0 or i == population_count - 1 or draw_new_champ :
2959
3016
colors = ["blue" ]
2960
3017
@@ -2963,7 +3020,7 @@ def arrangement(self) :
2963
3020
x ,y = 400 * (champions_count % 10 ), 700 * int (champions_count / 10 )
2964
3021
surface .move (x - b [0 ],y - b [1 ])
2965
3022
surface .draw (width = 2 , color = colors [0 ])
2966
- draw_text ("Step = %s\n Square = %f\n Time from start = %f" % (i ,(b [2 ]- b [0 ])* (b [3 ]- b [1 ]),time .time ()- start_time ),x ,y - 40 )
3023
+ draw_text ("Step = %s\n Square = %f\n Square improvement = %f \ n Time from start = %f" % (i ,(b [2 ]- b [0 ])* (b [3 ]- b [1 ]),improve , time .time ()- start_time ),x ,y - 50 )
2967
3024
champions_count += 1
2968
3025
"""
2969
3026
spiece = population.population[0][1]
@@ -3074,16 +3131,18 @@ def __init__(self):
3074
3131
3075
3132
self .OptionParser .add_option ("" , "--arrangement-material-width" , action = "store" , type = "float" , dest = "arrangement_material_width" , default = 500 , help = "Materials width for arrangement" )
3076
3133
self .OptionParser .add_option ("" , "--arrangement-population-count" ,action = "store" , type = "int" , dest = "arrangement_population_count" , default = 100 , help = "Genetic algorithm populations count" )
3134
+ self .OptionParser .add_option ("" , "--arrangement-inline-test" , action = "store" , type = "inkbool" , dest = "arrangement_inline_test" , default = False , help = "Use C-inline test (some additional packets will be needed)" )
3135
+
3077
3136
3078
3137
self .OptionParser .add_option ("" , "--postprocessor" , action = "store" , type = "string" , dest = "postprocessor" , default = '' , help = "Postprocessor command." )
3079
3138
self .OptionParser .add_option ("" , "--postprocessor-custom" , action = "store" , type = "string" , dest = "postprocessor_custom" , default = '' , help = "Postprocessor custom command." )
3080
3139
3081
- self .OptionParser .add_option ("" , "--graffiti-max-seg-length" , action = "store" , type = "float" , dest = "graffiti_max_seg_length" , default = 1. , help = "Ggraffiti maximum segment length." )
3082
- self .OptionParser .add_option ("" , "--graffiti-min-radius" , action = "store" , type = "float" , dest = "graffiti_min_radius" , default = 10. , help = "Ggraffiti minimal connector's radius." )
3083
- self .OptionParser .add_option ("" , "--graffiti-start-pos" , action = "store" , type = "string" , dest = "graffiti_start_pos" , default = "(0;0)" , help = "Ggraffiti Start position (x;y)." )
3140
+ self .OptionParser .add_option ("" , "--graffiti-max-seg-length" , action = "store" , type = "float" , dest = "graffiti_max_seg_length" , default = 1. , help = "Graffiti maximum segment length." )
3141
+ self .OptionParser .add_option ("" , "--graffiti-min-radius" , action = "store" , type = "float" , dest = "graffiti_min_radius" , default = 10. , help = "Graffiti minimal connector's radius." )
3142
+ self .OptionParser .add_option ("" , "--graffiti-start-pos" , action = "store" , type = "string" , dest = "graffiti_start_pos" , default = "(0;0)" , help = "Graffiti Start position (x;y)." )
3084
3143
self .OptionParser .add_option ("" , "--graffiti-create-linearization-preview" , action = "store" , type = "inkbool" , dest = "graffiti_create_linearization_preview" , default = True , help = "Ggraffiti create linearization preview." )
3085
- self .OptionParser .add_option ("" , "--graffiti-create-preview" , action = "store" , type = "inkbool" , dest = "graffiti_create_preview" , default = True , help = "Ggraffiti create preview." )
3086
- self .OptionParser .add_option ("" , "--graffiti-preview-size" , action = "store" , type = "int" , dest = "graffiti_preview_size" , default = 800 , help = "Ggraffiti preview's size." )
3144
+ self .OptionParser .add_option ("" , "--graffiti-create-preview" , action = "store" , type = "inkbool" , dest = "graffiti_create_preview" , default = True , help = "Graffiti create preview." )
3145
+ self .OptionParser .add_option ("" , "--graffiti-preview-size" , action = "store" , type = "int" , dest = "graffiti_preview_size" , default = 800 , help = "Graffiti preview's size." )
3087
3146
self .OptionParser .add_option ("" , "--graffiti-preview-emmit" , action = "store" , type = "int" , dest = "graffiti_preview_emmit" , default = 800 , help = "Preview's paint emmit (pts/s)." )
3088
3147
3089
3148
0 commit comments