导入所需包
import pandas as pd
import numpy as np
import random
问题一
生成随机向量(小数三位)
np.set_printoptions(precision=3)
a1 = np.random.random(10)
a2 = np.random.random(10)
def print_(a1,a2,n1,n2):
print("{}---{}".format(n1,a1))
print("{}---{}".format(n2,a2))
print_(a1,a2,"a1","a2")
a1---[0.59 0.705 0.893 0.273 0.508 0.875 0.186 0.736 0.638 0.35 ]
a2---[0.851 0.614 0.322 0.726 0.238 0.74 0.215 0.38 0.016 0.941]
单点交叉
def duplicate_removal(a,b): # 去重函数
map1 = {}
map2 = {}
new_a = []
new_b = []
for i in range(len(a)):
if map1.get(a[i])==None:
map1[a[i]] = 1
new_a.append(a[i])
for i in range(len(b)):
if map2.get(b[i])==None:
map2[b[i]] = 1
new_b.append(b[i])
return np.array(new_a) , np.array(new_b)
def single_point_crossing(a1,a2,duplicate=False):
a1_1 = np.zeros_like(a1)
a2_1 = np.zeros_like(a2)
r = random.randint(0,len(a1)-1)
print("random---->{}".format(r))
a1_1[r:] ,a2_1[r:] = a2[r:].copy() ,a1[r:].copy()
a1_1[:r] ,a2_1[:r] = a1[:r].copy() ,a2[:r].copy()
if duplicate:
return duplicate_removal(a1_1,a2_1)
return a1_1 , a2_1
a1_1 , a2_1 = single_point_crossing(a1,a2)
random---->3
print_(a1_1,a2_1,"a1_1","a2_1")
a1_1---[0.59 0.705 0.893 0.726 0.238 0.74 0.215 0.38 0.016 0.941]
a2_1---[0.851 0.614 0.322 0.273 0.508 0.875 0.186 0.736 0.638 0.35 ]
随即交换元素
def immediately_swap_elements(a):
r1 = random.randint(0,len(a)-1)
r2 = random.randint(0,len(a)-1)
a[r1] ,a[r2] = a[r2] , a[r1]
print("{}<-------->{}".format(r1,r2))
return a
a1_2 = immediately_swap_elements(a1_1)
6<-------->6
a1_2
array([0.59 , 0.705, 0.893, 0.726, 0.238, 0.74 , 0.215, 0.38 , 0.016,
0.941])
插入变异操作
def insert_mutation_operation(a):
r1 = random.randint(0,len(a)-1)
r2 = random.randint(0,len(a)-1)
t1 = a[r1]
a = np.delete(a,r1)
print("{}<-------->{}".format(r1,r2))
return np.insert(a,r2,t1)
a2_2 = insert_mutation_operation(a2_1)
a2_2
0<-------->2
array([0.614, 0.322, 0.851, 0.273, 0.508, 0.875, 0.186, 0.736, 0.638,
0.35 ])
问题二
二点交叉
print_(a1,a2,"a1","a2")
a1---[0.59 0.705 0.893 0.273 0.508 0.875 0.186 0.736 0.638 0.35 ]
a2---[0.851 0.614 0.322 0.726 0.238 0.74 0.215 0.38 0.016 0.941]
def two_point_intersection(a1,a2,duplicate=False):
t1 = a1.copy()
t2 = a2.copy()
l = random.randint(0,len(a1)-1)
r = random.randint(0,len(a1)-1)
if l>r:
l ,r = r, l
print("random range---->({} , {})".format(l,r))
t2[l:r] ,t1[l:r]= a1[l:r] ,a2[l:r]
if duplicate:
return duplicate_removal(t1,t2)
return t1 , t2
a1_1,a2_1 = two_point_intersection(a1,a2)
print_(a1_1,a2_1,"a1_1","a2_1")
random range---->(2 , 7)
a1_1---[0.59 0.705 0.322 0.726 0.238 0.74 0.215 0.736 0.638 0.35 ]
a2_1---[0.851 0.614 0.893 0.273 0.508 0.875 0.186 0.38 0.016 0.941]
逆序变异操作
def reverse_order_mutation_operation(a1,a2):
t1 = a1.copy()
t2 = a2.copy()
l = random.randint(0,len(a1)-1)
r = random.randint(0,len(a1)-1)
if l>r:
l ,r = r, l
print("random range---->({} , {})".format(l,r))
t1[l:r] ,t2[l:r]= np.flipud(a1[l:r]) ,np.flipud(a2[l:r])
return t1 , t2
a1_2,a2_2 = reverse_order_mutation_operation(a1,a2)
print_(a1_2,a2_2,"a1_2","a2_2")
random range---->(0 , 6)
a1_2---[0.875 0.508 0.273 0.893 0.705 0.59 0.186 0.736 0.638 0.35 ]
a2_2---[0.74 0.238 0.726 0.322 0.614 0.851 0.215 0.38 0.016 0.941]
问题三
def get_randomint(start,len):
a1 = np.arange(start,len)
a2 = np.arange(start,len)
np.random.shuffle(a1)
np.random.shuffle(a2)
return a1,a2
a1,a2 = get_randomint(0,10)
print_(a1,a2,"a1","a2")
a1---[2 7 3 4 0 6 8 9 1 5]
a2---[4 5 7 1 9 3 0 6 8 2]
单点交叉
a1_1 , a2_1 = single_point_crossing(a1,a2,duplicate=True) # 去重
random---->0
print_(a1_1,a2_1,"a1_1","a2_1")
a1_1---[4 5 7 1 9 3 0 6 8 2]
a2_1---[2 7 3 4 0 6 8 9 1 5]
问题四
def get_repeat_random(start,end,len):
a1 = np.random.choice(range(start,end+1),len,replace=True)
a2 = np.random.choice(range(start,end+1),len,replace=True)
return a1,a2
a1,a2 = get_repeat_random(1,10,10)
print_(a1,a2,"a1","a2")
a1---[ 4 7 5 6 5 4 5 2 4 10]
a2---[ 7 7 10 8 8 5 9 2 2 1]
a1_1 , a2_1 = two_point_intersection(a1,a2,duplicate=True) # 去重
random range---->(8 , 8)
print_(a1_1,a2_1,"a1_1","a2_1")
a1_1---[ 4 7 5 6 2 10]
a2_1---[ 7 10 8 5 9 2 1]