Untitled

 avatar
unknown
plain_text
3 years ago
3.1 kB
6
Indexable
import random
import numpy as np
read_file = open("CSE422_Lab-3.txt",'r')
temp = read_file.readline()
temp = temp.split()
p_count = int(temp[0])
target_run = int(temp[1])
#print(p_count)
#print(target_run)
dict_1 = {}
player_arr = []

for i in range(p_count):
  #print(i)
  x = read_file.readline() 
  x = x.split()
  #print(x)
  dict_1[i] = int(x[1])
  player_arr.append(x[0])
  #print(dict_1)

read_file.close()
print(dict_1)
print(player_arr)

def select_individual():
  indv = []

  for i in range(p_count):
    t = random.randint(0, 1)
    indv.append(t)
    #f = fitness(indv,player_arr)
    #sort(f)

  #print(indv)
  return indv

def generate_population():
  population = []
  for i in range(4):
    population.append(select_individual())

  return population
    

def fitness(indv,dict_1,target_run):
  #print('individual child:',indv)
  #print(type(indv))
  count_achieved_run = 0
  if type(indv)== list:
    for i in range(len(indv)):
      if (indv[i] == 1):
        count_achieved_run += dict_1[i]

  fit = abs(target_run - count_achieved_run)

  return fit
  
def mutation(child):
  mutation_probability = 0.2
  random_probabilty =  random.uniform(0, 0.5)
  #random_probabilty = np.random.random(0,0.5)
  #random_probabilty = round(random_probabilty,2)
  #print("random probabilty",random_probabilty)

  if (mutation_probability >= random_probabilty):
    idx = random.randint(0, 7)
    print('index',idx)
    if(child[idx] == 0):
      child[idx] = 1
    else:
      child[idx] = 0

  return child

def crossover(x,y):
  x = str(x)
  y = str(y)
  new_child = []
  idx = random.randint(0, 8)
  #print(x,y)
  new_child.append( x[0:idx+1:] )
  new_child.append( y[idx+1::] )

  return new_child


def find_parent(fit_arr):
  f_arr = fit_arr
  p_arr = {}
  #print("fit array before sorting:", fit_arr)
 
  p_arr  = dict( sorted(fit_arr.items()) )
  #print("fit array after sort:", p_arr)

  p_arr.popitem()

  #print("p_arr:",p_arr)

  x = random.choice(list(p_arr.values()))
  y = random.choice(list(p_arr.values()))
  

  if(x == y):
    find_parent(f_arr)
  else:
    #print("parent:","x:",x,"y:",y)
    return (x,y)

def GA(dict_1, n, target_run): # n is max gen count

  flag = False

  p = generate_population()

  for j in range(n):
    fit_arr = {}
    for i in p:
      f = fitness(i,dict_1,target_run)
      fit_arr[f] = i
      if ( f == target_run):
        child = i
        flag = True
        break

      
    for i in range(len(p)):
      parent_1,parent_2 = find_parent(fit_arr)

      print("t  & z:",parent_1,parent_2)        
      #parent_1 = t[0]
      #parent_2 = t[1]
      child = crossover(parent_1,parent_2)
      muted_child = mutation(child)
      #f = fitness(i,player_arr,target_run)

      
      if(len(p) < 4):
        p.append(child)
      elif( (i == 0) or len(p)>= 4):
        p = []
    
    
  if(flag == True):
    return child
  else:
    return -1

ans = GA(dict_1, 1, target_run)
print(ans)
Editor is loading...