pre-alpha multithreading
This commit is contained in:
parent
de3a0569a9
commit
052d8ca7ce
22
src/AI/GA.py
22
src/AI/GA.py
@ -1,12 +1,15 @@
|
|||||||
import random
|
import random
|
||||||
import numpy
|
import numpy
|
||||||
|
import copy
|
||||||
|
|
||||||
from src.AI.Affinities import Affinities
|
from src.AI.Affinities import Affinities
|
||||||
|
from src.AI.ThreadedSimulation import ThreadedSimulation
|
||||||
from src.entities.Enums import Classifiers
|
from src.entities.Enums import Classifiers
|
||||||
from src.entities.Player import Player
|
from src.entities.Player import Player
|
||||||
|
from src.game.Map import Map
|
||||||
|
|
||||||
|
|
||||||
def geneticAlgorithm(map, iter, solutions, mutationAmount=0.05):
|
def geneticAlgorithm(map, iter, solutions, mutationAmount=0.05, multithread=False, threadCount=4):
|
||||||
"""
|
"""
|
||||||
This algorithm will attempt to find the best affinities for player's goal choices.
|
This algorithm will attempt to find the best affinities for player's goal choices.
|
||||||
|
|
||||||
@ -22,14 +25,23 @@ def geneticAlgorithm(map, iter, solutions, mutationAmount=0.05):
|
|||||||
initialPopulation = numpy.random.uniform(low=0.0, high=1.0, size=(solutions, weightsCount))
|
initialPopulation = numpy.random.uniform(low=0.0, high=1.0, size=(solutions, weightsCount))
|
||||||
population = initialPopulation
|
population = initialPopulation
|
||||||
for i in range(iter):
|
for i in range(iter):
|
||||||
print("Running {} generation...".format(i))
|
print("\nRunning {} generation...".format(i))
|
||||||
fitness = []
|
fitness = []
|
||||||
for player in population:
|
if not multithread:
|
||||||
fitness.append(doSimulation(player, map))
|
for player in population:
|
||||||
|
fitness.append(doSimulation(player, map))
|
||||||
|
else:
|
||||||
|
threads = []
|
||||||
|
for p in range(len(population)):
|
||||||
|
threads.append(ThreadedSimulation(p+1, p+1, population[p], map))
|
||||||
|
threads[-1].start()
|
||||||
|
for t in threads:
|
||||||
|
t.join()
|
||||||
|
fitness.append(t.getResult())
|
||||||
parents = selectMatingPool(population, fitness, int(solutions / 2))
|
parents = selectMatingPool(population, fitness, int(solutions / 2))
|
||||||
print("Best fitness: {}".format(max(fitness)))
|
print("Best fitness: {}".format(max(fitness)))
|
||||||
offspring = mating(parents, solutions, mutationAmount)
|
offspring = mating(parents, solutions, mutationAmount)
|
||||||
|
print("Best offspring: ", offspring[0])
|
||||||
population = offspring
|
population = offspring
|
||||||
|
|
||||||
|
|
||||||
|
27
src/AI/ThreadedSimulation.py
Normal file
27
src/AI/ThreadedSimulation.py
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
import threading
|
||||||
|
import time
|
||||||
|
|
||||||
|
|
||||||
|
exitFlag = 0
|
||||||
|
|
||||||
|
|
||||||
|
class ThreadedSimulation(threading.Thread):
|
||||||
|
def __init__(self, threadID, counter, player, map):
|
||||||
|
threading.Thread.__init__(self)
|
||||||
|
self.threadID = threadID
|
||||||
|
self.counter = counter
|
||||||
|
|
||||||
|
self.player = player
|
||||||
|
|
||||||
|
self.result = None
|
||||||
|
|
||||||
|
self.map = map
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
from src.AI.GA import doSimulation
|
||||||
|
from src.game.Map import Map
|
||||||
|
newMap = Map(self.map.filename, None)
|
||||||
|
self.result = doSimulation(self.player, newMap)
|
||||||
|
|
||||||
|
def getResult(self):
|
||||||
|
return self.result
|
Loading…
Reference in New Issue
Block a user