I
I
Ivan Shalashugin2022-04-02 21:11:21
Python
Ivan Shalashugin, 2022-04-02 21:11:21

Why doesn't the code work as expected (AI)?

In the code, AI instances are created with parameters (weight answers, etc.) When the answer calculation method is executed (evale() ), the AI ​​changes its answer (newAi.yl) although it should not. The code has separate functions for working with dictionaries and lists (Part with AI starts on line 70 or class newAi)
Here is the code:

from random import *
from os import *

def copy(lst):
  o=[]
  for i in lst:o.append(i)
  return o

def drange(dct, rang, n=False):
  out=[]
  if not(n):
    olen=len(dct)-rang
    for i in range(olen):
      out.append(i)
  return out

def copy_dict(dct):
  aa={}
  for i in dct:aa[i] = dct[i]
  return aa

def sortd(dct,tp):
  if tp==1:
    return sorted(list(dct.keys()))
  else:
    return sorted(list(dct.values()))

def findObjByKey(dct, key):
  return dct[key]

def findKeyByObj(dct, obj):
  dct_key = list(copy_dict(dct).keys())
  dct_val = list(copy_dict(dct).values())
  return dct_key[dct_val.index(obj)]

def mut(prc_mut, sa, mn, mx):
  vl = []
  vl2 = []

  for i in range(100):
    vl.append(i)
  for i in range(prc_mut):
    vl2.append(i)

  s = sa
  r = [mn, mx]
  x=choice(vl)
  if x in vl2:
    s[s.index(choice(s))]+=choice(r)
  s2 = copy(s)
  return s

def mut2(prc_mut, sa, mn, mx):
  vl = []
  vl2 = []

  for i in range(100):
    vl.append(i)
  for i in range(prc_mut):
    vl2.append(i)

  s = sa
  r = [mn, mx]
  x=choice(vl)
  if x in vl2:
    s[s.index(choice(s))]=choice(r)
  s2 = copy(s)
  return s

class newAi:
  def __init__(self,xlist,ry,cl):
    self.xl = xlist
    self.yl = []
    for i in range(ry):
      self.yl.append(0)
    self.w = [[],[]]
    for i in xlist:
      self.w[0].append(uniform(-1,1))
    for i in self.yl:
      self.w[1].append(uniform(-1,1))
    self.n=0
    self.ans = 0
    self.er = 0
    self.c = cl

  def evale(self):
    temp=[]
    for i in self.xl:
      for w in self.w[0]:
        temp.append(i*w)
    self.n = sum(temp)
    temp = []
    for i in self.w[1]:
      self.yl[self.w[1].index(i)] = i*self.n

    return self.yl

  def ser(self):
    self.er = self.c[0] - self.yl[0]
    return self.er

  def mutation(self):
    w0 = mut2(50, self.w[0], uniform(-1, 0),uniform(0, 1))
    w1 = mut2(50, self.w[1], uniform(-1, 0),uniform(0, 1))
    return [w0, w1]

if __name__ == '__main__':
  system('cls')
  data = {}
  for i in range(10):
    a = newAi([2,3],1,[6])
    n = a.evale()
    e = a.ser()
    data[e] = a
  for i in data:
    print(i,':',data[i])
  
  print()
  print(min(data.keys()), ':', data[min(data.keys())])

  data_c = copy_dict(data)

  for i in data_c:
    if i < 0:
      del data[i]
  print()
  print(min(data.keys()), ':', data[min(data.keys())])
  print()

  data_c2 = copy_dict(data)
  lowestErorAis = []

  for i in drange(data_c2, 4):
  #for i in range(4):
    lowestErorAis.append(findObjByKey(data_c2, min(data_c2.keys())))
    del data_c2[max(data_c2.keys())]

  print(list(data_c2.keys()))
  keysDataC2 = sortd(data_c2, 1)
  print(keysDataC2)
  data_c3 = {}

  for i in keysDataC2:
    data_c3[i] = data_c2[findKeyByObj(data_c2, findObjByKey(data_c2,i))]

  for i in data_c3:
    print('1--',i,':',data_c3[i])

  startErors = [findKeyByObj(data_c3 , data_c3[i])]

  #data_c3 - ais with lowest erors (sorted and not sorted - data_c2)

  clone_data = {}
  for i in range(100):
    
    for i in range(1):
      for i in list(data_c3.values()):
        a = newAi([2,3],1,[6])
        ws = i.mutation()
        a.w[0] = ws[0]
        a.w[1] = ws[1]
        n = a.evale()
        #print()
        e = a.ser()
        if not e < 0:
          clone_data[e] = a

      data_c = copy_dict(clone_data)
      
      for i in data_c:
        if i < 0:
          del clone_data[i]
      
      data_c2 = copy_dict(clone_data)
      lowestErorAis = []

      for i in drange(data_c2, 4):
      #for i in range(4):
        lowestErorAis.append(findObjByKey(data_c2, min(data_c2.keys())))
        del data_c2[max(data_c2.keys())]

      #print(list(data_c2.keys()))
      keysDataC2 = sortd(data_c2, 1)
      #print(keysDataC2)
      data_c3 = {}

      for i in keysDataC2:
        data_c3[i] = data_c2[findKeyByObj(data_c2, findObjByKey(data_c2,i))]
    
  #print(*clone_data)
  print(data_c3)
  for i in data_c3:
    print(i,':',data_c3[i])

  #best_dict = {findKeyByObj(data_c3, data_c3[min(data_c3.keys())]):data_c3[min(data_c3.keys())]}
  best_dict = {findKeyByObj(clone_data, clone_data[min(clone_data.keys())]):clone_data[min(clone_data.keys())]}
  print('The best AI -', list(best_dict.values())[0], 'with eror -', list(best_dict.keys())[0])
  if not list(best_dict.keys())[0] in startErors:
    print('Evolution is working!')



  print('Enter - [2,3]')

  print(list(best_dict.values())[0].w)
  print(list(best_dict.values())[0].yl)
  print(list(best_dict.values())[0].evale())
  print(list(best_dict.values())[0].evale())
  print(list(best_dict.values())[0].w)
  print(list(best_dict.values())[0].yl)
  
  print(list(best_dict.values())[0].w)

  print('AI answer -', list(best_dict.values())[0].yl)
  print('Correct answer is 6')

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question