NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

import numpy as np
import time

# random seed
np.random.seed(int(time.time()))

# init blank board
matrix = np.zeros((8,8))

# parents and children array
MAXGEN = 4
parents = []
children = [[]]


# attack & fitness values
sa = []
da = []
OGfit = []
fit = []
MAXFIT = 28
MAXQUEEN = 8
res = 0

# randomly assign initial parents
for i in range(0, MAXGEN):
parentTemp = []
for j in range(0,MAXQUEEN):
parentTemp.append(np.random.randint(0,8))

if i == len(parents):
parents.append(np.array(parentTemp))
else:
parents[i] = np.array(parentTemp)

# funcs to detect conflicts
def side(x):
res = 0
for i in range(0, len(x)-1):
for j in range(i+1, len(x)):
if(x[i] == x[j]):
res += 1

return res

def diag(x):
res = 0
for i in range(0, len(x)-1):
for j in range(i+1, len(x)):
r = abs(x[i] - x[j])
c = abs(i - j)
if c == r:
res += 1

return res

# func to calc fitness
def fitness(sa, da):
return MAXFIT - (sa+da)


# sorting algo
def bubbleSort(arr1, arr2):
for i in range(len(arr1)):
for j in range(0, len(arr1)-i-1):
if arr1[j] > arr1[j+1] :
arr1[j], arr1[j+1] = arr1[j+1], arr1[j]
arr2[j], arr2[j+1] = arr2[j+1], arr2[j]

OGfit.append(0)
while max(OGfit) < MAXFIT:
sa = []
da = []
fit = []
OGfit = []

# calculate total fitness of parents
for i in range(0, MAXGEN-1):
saTemp = 0
daTemp = 0
for j in range(i+1, MAXGEN):
saTemp += side(parents[i])
daTemp += diag(parents[i])

sa.append(saTemp)
da.append(daTemp)
OGfit.append(fitness(sa[i], da[i]))


# check if solution is in parents
if max(OGfit) >= MAXFIT:
print(parents)
res = parents[OGfit.index(max(OGfit))]
break

# calculate relative fitness
fitSum = np.sum(OGfit)
for i in range(0, len(OGfit)):
fit.append((OGfit[i] / fitSum) * 100)

# sort and select best parents
bubbleSort(fit, parents)
fit.pop(0)
parents.pop(0)

# crossover
# crossover variables
cPoint = np.random.randint(0, MAXQUEEN)
chHead = []
chTail = []

# select crossover values
for i in range(0, len(parents)):
chHead.append(parents[i][:cPoint])
chTail.append(parents[i][cPoint:])

# swap of corssing over
for i in range(0, len(chTail)-1):
for j in range(i+1, len(chTail)):
if len(children) >= MAXGEN:
break
else:
children.append(np.concatenate((chHead[i], chTail[j])))
children.append(np.concatenate((chHead[j], chTail[i])))

children.pop(0)

# mutation
for i in range(len(children)):
mutI = np.random.randint(0, len(children[i]))
children[i][mutI] = np.random.randint(0, len(children[i]))

parents = children
MAXGEN = len(children)

print(res)
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.