Better non-threading version
This commit is contained in:
58
__main__.py
58
__main__.py
@@ -1,10 +1,13 @@
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
import threading
|
import threading
|
||||||
|
import queue
|
||||||
|
import time
|
||||||
|
|
||||||
INPUT_FILE = "data/medium.in"
|
INPUT_FILE = "data/medium.in"
|
||||||
POPULATION = 1000
|
POPULATION = 50
|
||||||
MUTATION_AMOUNT = 250
|
MUTATION_AMOUNT = 1000
|
||||||
ITERATIONS = 30
|
ITERATIONS = 30
|
||||||
|
THREAD_COUNT = 20
|
||||||
|
|
||||||
data = [line for line in open(INPUT_FILE)]
|
data = [line for line in open(INPUT_FILE)]
|
||||||
params = list(map(int, data[0].split(" ")))
|
params = list(map(int, data[0].split(" ")))
|
||||||
@@ -19,53 +22,6 @@ print(clusters[0])
|
|||||||
values = {}
|
values = {}
|
||||||
first = True
|
first = True
|
||||||
|
|
||||||
class myThread (threading.Thread):
|
|
||||||
def __init__(self, cluster, clean, id):
|
|
||||||
threading.Thread.__init__(self)
|
|
||||||
self.cluster = cluster
|
|
||||||
self.clean = clean
|
|
||||||
self.id = id
|
|
||||||
self.result = None
|
|
||||||
self.first = False
|
|
||||||
self.values = {}
|
|
||||||
self.vfunc = np.vectorize(self.myfunc)
|
|
||||||
|
|
||||||
def run(self):
|
|
||||||
# calc fitness
|
|
||||||
self.values = {}
|
|
||||||
self.first = True
|
|
||||||
self.vfunc(self.cluster, data)
|
|
||||||
self.result = get_fitness(self.values, clean=self.clean)
|
|
||||||
print("Exit thread", self.id)
|
|
||||||
|
|
||||||
def myfunc(self, a, b):
|
|
||||||
if self.first:
|
|
||||||
self.first = False
|
|
||||||
return
|
|
||||||
if a not in values:
|
|
||||||
self.values[a] = [0, 0]
|
|
||||||
self.values[a][b] += 1
|
|
||||||
|
|
||||||
|
|
||||||
def get_fitnesses(clusts, clean=False):
|
|
||||||
threads = []
|
|
||||||
for i, cluster in enumerate(clusters):
|
|
||||||
if i % 20 == 0:
|
|
||||||
print("fitness", i, iteration)
|
|
||||||
# Create new threads
|
|
||||||
thread = myThread(cluster, clean, i)
|
|
||||||
# Start new Threads
|
|
||||||
thread.start()
|
|
||||||
|
|
||||||
# Add threads to thread list
|
|
||||||
threads.append(thread)
|
|
||||||
|
|
||||||
# Wait for all threads to complete
|
|
||||||
for t in threads:
|
|
||||||
t.join()
|
|
||||||
print("Exiting Main Thread")
|
|
||||||
return np.array([thread.result for thread in threads])
|
|
||||||
|
|
||||||
def get_fitness(vals, clean=False):
|
def get_fitness(vals, clean=False):
|
||||||
fit = 0
|
fit = 0
|
||||||
for key, val in vals.items():
|
for key, val in vals.items():
|
||||||
@@ -180,16 +136,12 @@ def myfunc(a, b):
|
|||||||
vfunc = np.vectorize(myfunc)
|
vfunc = np.vectorize(myfunc)
|
||||||
|
|
||||||
# mutation
|
# mutation
|
||||||
xx = MUTATION_AMOUNT
|
|
||||||
for i in range(POPULATION):
|
for i in range(POPULATION):
|
||||||
if i % 20 == 0:
|
if i % 20 == 0:
|
||||||
print("mutation", i)
|
print("mutation", i)
|
||||||
MUTATION_AMOUNT = 500
|
|
||||||
clusters[i] = mutation(clusters[i])
|
clusters[i] = mutation(clusters[i])
|
||||||
MUTATION_AMOUNT = xx
|
|
||||||
|
|
||||||
for iteration in range(ITERATIONS):
|
for iteration in range(ITERATIONS):
|
||||||
fitnesses = get_fitnesses(clusters, clean=False)
|
|
||||||
# calc fitness
|
# calc fitness
|
||||||
fitnesses = np.zeros((POPULATION, ))
|
fitnesses = np.zeros((POPULATION, ))
|
||||||
for i, cluster in enumerate(clusters):
|
for i, cluster in enumerate(clusters):
|
||||||
|
|||||||
Reference in New Issue
Block a user