def __init__(self, withGrapics=True): self.bestbest = 0 self.done = False self.pool = Pool(Config.WIDTH, Config.HEIGHT, self.callback) self.graphics = Graphics() if withGrapics: self.graphics.init("PoolGame", Config.SIZE)
def __init__(self, pool_size, start_training, country, region=None, covid_data=None, statistic='Cases'): # The object containing all case, fatality, and movement data self.covid_data = covid_data if covid_data is None: self.covid_data = DataGenerator() # The country and possibly specific region to generate a model for self.country = country self.region = region # The number of models for each generation self.pool_size = pool_size # What metric to create a model for, either 'Cases' or 'Fatalities' self.statistic = statistic # The date when training takes data from through 2 weeks later self.start_training = start_training # The object which handles all the models in a generation self.pool = Pool(pool_size) # The actual data which the predictions will be compared to self.test_case = [] self.generate_test_case()
def __init__(self): self.conv1 = Conv(3, 8, 1) self.relu1 = Relu() self.pool1 = Pool() self.conv2 = Conv(3, 16, 8) self.relu2 = Relu() self.pool2 = Pool() #self.fcl1 = FCL(12 * 6 * 16, 120) #self.fcl2 = FCL(120, 80) #self.relu3 = Relu() #self.relu4 = Relu() #self.relu = Relu() #self.fcl1 = FCL(256, 256) #self.relu1 = Relu() self.fcl = Reg(6 * 6 * 16, 2) self.soft = Softmax()
def Predict( self, model, # can be the model itself or model path. spectrograms, # spectrograms that are generated from the given track. output_directory=None, track_name=None): ''' this function takes a series of spectrograms and then predict them in parallel then returns a concatenated spectrograms''' if type(model) == str: # if model is a path not real object. if not os.path.exists(model): print('Model does not exist') return None model = tf.keras.models.load_model(model) # Prediction part.---------------------------------------------- start_time = time.time() manager = Pool() outputs = manager.go(model.predict, spectrograms) outputs = [outputs[i][0] for i in range(len(spectrograms))] full_output_stem = reduce(lambda a, b: np.concatenate([a, b], axis=0), outputs) prediction_time = time.time() - start_time print( f'[LOG] prediction done. Prediction time: {prediction_time:.3f} seconds' ) return full_output_stem
def initializePool(env): pool = Pool(env) for i in range(0, Population): basic = Genome(pool) basic.basicGenome() pool.addToSpecies(basic) pool.initializeRun() return pool
def __init__(self, main_instance): AbstractRole.__init__(self, main_instance) self.virt_co = libvirt.open(Config.libvirt_uri) self.has_run = False self.queue = Queue.Queue() self.pool = Pool(Config.ulteo_pool_name,self.virt_co) self.virtual_machine = {} self.network = Network(Config.network_name, self.virt_co) self.webserver = None
def make_player(use_block, result_data, board_size): mock_pool = Pool(len(result_data), Blocks()) mock_pool.get_blocks = Mock() mock_pool.get_blocks.return_value = use_block mock_brain = Brain(Blocks()) mock_brain.get_setting_info = Mock() mock_brain.get_setting_info.side_effect = \ lambda *a,**k : (copy.deepcopy(deque(result_data)) , 0) return Player(mock_pool, Board(board_size), mock_brain, max=10)
def __init__(self): self.conv1 = Conv(3, 8, 1) # self.relu1 = Relu() self.pool1 = Pool() # self.conv2 = Conv(3, 16, 8) # self.relu2 = Relu() # self.pool2 = Pool() # self.conv3 = Conv(3, 24, 16) # self.pool3 = Pool() # self.conv4 = Conv(3, 30, 24) # self.pool4 = Pool() self.fcl1 = FCL(24 * 24 * 8, 10) self.soft = Soft()
def initScene(self): self.pool = Pool(self) # plight = PointLight('plight') # plight.setColor(VBase4(0.0, 0.8, 0.8, 1)) # plnp = render.attachNewNode(plight) # plnp.setPos(0, 3, 1) # render.setLight(plnp) # self.model = self.loader.loadModel("panda") # self.model.reparentTo(self.mainNode) # # self.model.setScale(0.25,0.25,0.25) # self.model.setPos(-8,42,0) # self.camera.setPosHpr(0,0,2,0,0,0) # base.enableMouse() print "Simulator Initialized" RosManager()
def main(): if len(sys.argv) <= 1: print("Please provide more arguments") return pool = Pool() while True: score = pool.execute_trees() print(score) #for tree in pool.trees: #print(tree.lines()) write_file(pool.best_tree.lines()) if score == 0: break pool.alter_best_tree() code = pool.best_tree.lines() f = open(sys.argv[1], "w+") for line in code: f.write(line + "\n") f.close()
def test_count_worker(self): worker = Pool() self.assertEqual(10, worker.map(self.task, self.generate_data(30, 100000))[0])
from CardDeck import CardDeck from User import User from Opponent import Opponent from Pool import Pool from Game import Game # Create a card deck, user, 2 x opponent, a pool and a game object carddeck = CardDeck() # Merges Suits and Numbers carddeck.merge_suits_numbers() # Initialize objects by giving them names and card deck lists user = User("User", carddeck.card_deck) opponent = Opponent("Opponent", carddeck.card_deck) opponent2 = Opponent("Opponent2", carddeck.card_deck) pool = Pool("Pool", carddeck.card_deck) game = Game() # The game will be executed as long as the user's life has not reached 0 while user.life > 0: # Display life points, cards in the pool and user cards print("-" * 75, "\n") print( "User Life: {user_life} | Opponent Life: {opponent_life} | Opponent2 Life: {opponent2_life}\n" .format(user_life=user.life, opponent_life=opponent.life, opponent2_life=opponent2.life)) print("-" * 75, "\n") print("My cards: {user_hand}\nPool: {pool_cards}\n".format( user_hand=user.hand, pool_cards=pool.hand))
from Logger import Logger from Blocks import Blocks from os import getcwd from datetime import datetime from sys import argv from itertools import product from functools import reduce from math import gcd #main if __name__ == "__main__": board_size = int(argv[1] if len(argv) > 1 else 5) learn_switch = argv[2] if len(argv) > 2 else "" logname = datetime.strftime(datetime.now(), "%Y%m%d%H%M%S") player = Player(Pool(3, Blocks()), Board(board_size), Brain(Blocks(), weights=(100, 1, 10, 1)), max=1000, logger=Logger(getcwd() + f"\\log\\woody_log_{logname}.log")) if (learn_switch == "-l"): w1 = (0, 1, 10) w2 = (0, 1, 10) w3 = (0, 1, 10) w4 = (0, 1, 10) already = [] for a1, a2, a3, a4 in product(w1, w2, w3, w4): its_gcd = reduce(gcd, [a1, a2, a3, a4]) its_gcd = 1 if its_gcd == 0 else its_gcd
import numpy as np import copy import random import operator from NEAT_MapleLegend import constants from Pool import Pool pool = Pool() def newInnovation(): pool.innovation = pool.innovation+1 return pool.innovation class Genome(object): class Neuron(object): def __init__(self): self.incoming = {} self.value = 0.0 class Gene(object): def __init__(self, into = 0, out = 0, weight = 0.0, enable = True, innovation = 0): self.into = into self.out = out self.weight = weight self.enable = enable self.innovation = innovation def copy(self): return copy.deepcopy(self) def __init__(self, Inputs = 0, Outputs = 0): self.Inputs = Inputs
def test_memory(self): worker = Pool() self.assertLessEqual(30, int(worker.map(self.task, self.generate_data(30, 100000))[1]))
def test_count_worker_max(self): worker = Pool(1, 2, 512) self.assertEqual(2, worker.map(self.task, self.generate_data(30, 100000))[0])
Log("Average Fitness: {}".format(np.mean(scores))) best_game = games[best_indices[0]] worst_game = games[best_indices[-1]] return best_game, worst_game BOARD_SIZE = 10 MAX_MOVES = 100 ALPHA = 0.5 # mutation rate POOL_NAME = "green" FPS = 25 network = Network() pool = Pool(BOARD_SIZE, MAX_MOVES, network) pool.load(POOL_NAME) if(pool.get_size() == 0): pool.seed(100) pool.race(top=10, games_per_snake=5) for epoch in range(5): Log("--- Epoch #{} ---".format(epoch)) pool.populate(pool_size=100, alpha = ALPHA) snakes, scores = pool.race(top=10, games_per_snake=5) for i,score in enumerate(scores): snake = snakes[i]
def test_count_worker_min(self): worker = Pool(15, 20, 512) self.assertRaisesRegexp(MemoryError,"The number of required workers is less than the minimum specified.", worker.map,self.task, self.generate_data(30, 100000))
def __process_message(self, peer: Peer, command: bytes, message: bytes) -> bool: """ Processes the received message :param peer: :param command: :param message: :return: True, if connection should be closed """ if command == Protocol.Flags.HELLO: logging.warning("HELLO message received again") peer.send(Protocol.hello_message()) elif command == Protocol.Flags.LOGIN: logging.warning("User \"" + peer.name + "\" is already logged in") peer.send( Protocol.server_message(Protocol.ServerFlags.ACK, "You are already logged in")) elif command == Protocol.Flags.PING: logging.info("PING message received") peer.send(Protocol.pong_message()) elif command == Protocol.Flags.PONG: logging.info("PONG message received") elif command == Protocol.Flags.EXIT: logging.info("EXIT message received, connection closed") # peer.send(Protocol.server_message(Protocol.ServerFlags.NORMAL, "See you later")) return True elif command == Protocol.Flags.LOGOUT: if not peer.logged_in: return False logging.info("LOGOUT message received from \"" + peer.name + "\"") peer.logged_in = False peer.name = None elif command == Protocol.Flags.JOIN: if not peer.logged_in: return False pool_name = message.split(bytes([Protocol.Flags.SEPARATOR ]))[0].decode() passwd = message.split(bytes([Protocol.Flags.SEPARATOR ]))[1].decode() hashed = str( hashpw(passwd.encode("utf-8"), b"$2a$12$" + b"SZ4R4Z3G3SZ4DJ4LS0RT..")).split("..")[1][:-1] logging.info("JOIN from \"" + peer.name + "\" for pool \"" + pool_name + "\"") pool_id = SQLModule.PoolsSQLModule.get_id(pool_name) if pool_id == -1 or hashed == SQLModule.PoolsSQLModule.get_hashed_pwd( pool_name): if pool_name in self.__pools: logging.debug("Pool already exists") self.__pools[pool_name].add_peer(peer) else: logging.debug("Pool not exists, creating") self.__pools[pool_name] = Pool(pool_name) self.__pools[pool_name].add_peer(peer) if pool_id == -1: SQLModule.PoolsSQLModule.add_pool(pool_name, hashed) logging.info("Pool created with name \"" + pool_name + "\"") peer.send( Protocol.server_message( Protocol.ServerFlags.ACK, "Pool created with name \"" + pool_name + "\"")) pool_id = SQLModule.PoolsSQLModule.get_id(pool_name) peer_id = SQLModule.PeersSQLModule.get_id(peer.name) SQLModule.SwitchTable.add_peer_pool(peer_id, pool_id) logging.info("\"" + peer.name + "\" has joined \"" + pool_name + "\" succesfully") peer.send( Protocol.server_message(Protocol.ServerFlags.ACK, "Successful join")) peer.pool = self.__pools[pool_name] peer.pool.send_message( Protocol.server_message(Protocol.ServerFlags.NORMAL, peer.name + " has joined the room"), peer) else: logging.warning("\"" + pool_name + "\" failed to log in") peer.send( Protocol.server_message(Protocol.ServerFlags.NAK, "Wrong password for this pool")) elif command == Protocol.Flags.LEAVE: if not peer.logged_in: return False # pool_name = message.split(bytes([Protocol.Flags.SEPARATOR]))[0].decode() pool_name = peer.pool.name logging.info("LEAVE from \"" + peer.name + "\" for pool \"" + pool_name + "\"") peer_id = SQLModule.PeersSQLModule.get_id(peer.name) pool_id = SQLModule.PoolsSQLModule.get_id(pool_name) if SQLModule.SwitchTable.remove_peer_pool(peer_id, pool_id): peer.send( Protocol.server_message(Protocol.ServerFlags.NORMAL, "You've left the group")) peer.leave_pool() elif command == Protocol.Flags.USER: if not peer.logged_in: peer.send( Protocol.server_message(Protocol.ServerFlags.NORMAL, "You gotta log in first")) return False if peer.pool is None: peer.send( Protocol.server_message(Protocol.ServerFlags.NORMAL, "You gotta join a room first")) return False logging.info("USER message received") # peer.pool.send_message(message.split(bytes([Protocol.Flags.SEPARATOR]))[-1], peer) # peer.send(Protocol.Protocol.user_message(self.__name, peer.name, message.decode())) peer.pool.send_message( Protocol.user_message( peer.pool.name, peer.name, message.split(bytes([Protocol.Flags.SEPARATOR ]))[-1].decode()), peer) elif command == Protocol.Flags.SERVER: logging.warning( "Server received SERVER message, connection closed") return True else: peer.send( Protocol.server_message(Protocol.ServerFlags.NORMAL, "Invalid message received")) logging.warning("Invalid message received") return False
def test_max_memory(self): worker = Pool() count_worker = worker.map(self.task, self.generate_data(30, 100000))[0] mem_for_worker = worker.map(self.task, self.generate_data(30, 100000))[1] self.assertLessEqual(count_worker * mem_for_worker, 500)
def callback(gen, avg_score, best_score, best_json): global bestbest global done line = "Generation\t%d\tAverage Score\t%f\tBest Score\t%f" % (gen, avg_score, best_score) write_log(Config.JSON_FOLDER + "/" + "logfile.txt", line) filename = "brain-g%03d-%04d.json" % (gen, best_score * 1000) if best_score >= bestbest: if best_json != None: write_file(Config.JSON_FOLDER + "/" + filename, best_json) bestbest = best_score if gen == Config.GENERATIONS: done = True def write_log(filename, line): print(line) with open(filename, "a") as outfile: outfile.write(line + "\n") def write_file(filename, data): with open(filename, "w") as outfile: outfile.write(data + "\n") pool = Pool(Config.WIDTH, Config.HEIGHT, callback) while done == False: pool.tick()
def main(): print("Starting Network...") print("-------------------------------------------------------") print("Reading Data sets...") # MNIST Data sets #train_img, test_img, train_lbl, test_lbl = load(file_name="mnist") # CIFAR-10 Data sets train_img, test_img, train_lbl, test_lbl = load(file_name="cifar") Y = train_lbl[:].astype(int) X = train_img[:] / 255. Y_test = test_lbl[:].astype(int) X_test = test_img[:] / 255. #preprocess data #X = preprocess_data(X) #X_test = preprocess_data(X_test) #model model = Model() model.add( ConvNet(filter_size=(5, 5), filter_no=6, zero_padding=0, stride=(1, 1), activation="relu")) model.add(Pool(pool_size=(2, 2), stride=(2, 2), pool_type="max")) model.add( ConvNet(filter_size=(5, 5), filter_no=6, zero_padding=0, stride=(1, 1), activation="relu")) model.add(Pool(pool_size=(2, 2), stride=(2, 2), pool_type="max")) model.add(Flatten()) model.add( FCLayer(activation="relu", n_neurons=32, l_rate=0.001, is_drop_out=True, drop_out=0.7)) model.add(FCLayer(activation="softmax", n_neurons=10, l_rate=0.001)) print("-------------------------------------------------------") print("CNN Layers:") print("-------------------------------------------------------") model.print_layers() print("-------------------------------------------------------") print("Begin Training...") model.train(X, Y, n_epochs=150, print_loss=True, batch_size=32) print("End Training.") print("-------------------------------------------------------") print("Begin Testing...") train_accuracy = model.test(X, Y) test_accuracy = model.test(X_test, Y_test) print("End Testing.") print("-------------------------------------------------------") print('Training Accuracy: {0:0.2f} %'.format(train_accuracy)) print('Test Accuracy: {0:0.2f} %'.format(test_accuracy)) model.show_graph()
def my_pool(): pool_size = 3 blocks = Blocks() yield Pool(pool_size, blocks)
def __init__(self, para): Net.__init__(self, para) convPara1 = { 'instanceName': 'RN18' + '_Conv1', 'padding': True, 'padShape': (1, 1), 'stride': 1, 'outChannel': para['c1OutChannel'], 'kernelShape': (3, 3), 'bias': False } self.conv1 = Conv2D(convPara1) self.norm1 = Normalize({'instanceName': 'RN18' + '_Norm1'}) self.scale1 = Scale({'instanceName': 'RN18' + '_Scale1'}) self.activation1 = Activation({ 'instanceName': 'RN18' + '_Activation1', 'activationType': 'ReLU' }) self.layerList.append(self.conv1) self.layerList.append(self.norm1) self.layerList.append(self.scale1) self.layerList.append(self.activation1) convPara2 = { 'instanceName': 'RN18' + '_Conv2', 'padding': True, 'padShape': (1, 1), 'stride': 2, 'outChannel': para['c2OutChannel'], 'kernelShape': (3, 3), 'bias': False } self.conv2 = Conv2D(convPara2) self.norm2 = Normalize({'instanceName': 'RN18' + '_Norm2'}) self.scale2 = Scale({'instanceName': 'RN18' + '_Scale2'}) self.activation2 = Activation({ 'instanceName': 'RN18' + '_Activation2', 'activationType': 'ReLU' }) self.layerList.append(self.conv2) self.layerList.append(self.norm2) self.layerList.append(self.scale2) self.layerList.append(self.activation2) self.rnb1 = ResNetBlock({ 'instanceName': 'RN18' + '_RNB1', 'skipMode': 'identity', 'skipStride': 0, 'stride1': 1, 'outChannel1': int(para['rnb1OutChannel'] / 4), 'outChannel2': int(para['rnb1OutChannel'] / 4), 'outChannel3': para['rnb1OutChannel'], 'activationType': 'ReLU' }) self.layerList.append(self.rnb1) self.rnb2 = ResNetBlock({ 'instanceName': 'RN18' + '_RNB2', 'skipMode': 'identity', 'skipStride': 0, 'stride1': 1, 'outChannel1': int(para['rnb1OutChannel'] / 4), 'outChannel2': int(para['rnb1OutChannel'] / 4), 'outChannel3': para['rnb1OutChannel'], 'activationType': 'ReLU' }) self.layerList.append(self.rnb2) self.rnb3 = ResNetBlock({ 'instanceName': 'RN18' + '_RNB3', 'skipMode': 'identity', 'skipStride': 0, 'stride1': 1, 'outChannel1': int(para['rnb1OutChannel'] / 4), 'outChannel2': int(para['rnb1OutChannel'] / 4), 'outChannel3': para['rnb1OutChannel'], 'activationType': 'ReLU' }) self.layerList.append(self.rnb3) self.rnb4 = ResNetBlock({ 'instanceName': 'RN18' + '_RNB4', 'skipMode': 'conv', 'skipStride': 2, 'stride1': 2, 'outChannel1': int(para['rnb4OutChannel'] / 4), 'outChannel2': int(para['rnb4OutChannel'] / 4), 'outChannel3': para['rnb4OutChannel'], 'activationType': 'ReLU' }) self.layerList.append(self.rnb4) self.rnb5 = ResNetBlock({ 'instanceName': 'RN18' + '_RNB5', 'skipMode': 'identity', 'skipStride': 1, 'stride1': 1, 'outChannel1': int(para['rnb4OutChannel'] / 4), 'outChannel2': int(para['rnb4OutChannel'] / 4), 'outChannel3': para['rnb4OutChannel'], 'activationType': 'ReLU' }) self.layerList.append(self.rnb5) self.pool1 = Pool({ 'instanceName': 'RN18' + '_pool1', 'poolType': 'ave', 'stride': para['pSize'], 'kernelShape': (para['pSize'], para['pSize']) }) self.layerList.append(self.pool1) self.fc1 = FullyConnected({ 'instanceName': 'RN18' + '_fc1', 'outChannel': para['classNum'], 'bias': True }) self.layerList.append(self.fc1) self.softmax = Softmax({'instanceName': 'RN18' + '_softmax'}) self.layerList.append(self.softmax) self.bottomInterface = self.conv1 self.topInterface = self.softmax self.softmax.setNet(self)
def uploadChanges(self, taskfile, projectname, path): task = self._retrieveTask(taskfile) ids, descriptors, modifydescriptors, contentlocatoruri, metadataprovideruri, description = self._extractParameters( task) metadataprovider = ServiceStub.MetadataProvider(metadataprovideruri) try: songlisting = shelve.open(path + "/" + projectname + '.info.dict', writeback=True) except KeyError: raise TaskerError( "Workflow error\nCould not restore configuration, make sure you follow the usage process." ) modifiedlist = self.listModified(taskfile, projectname, path).split('\n') try: uploadfile = 'uploadfile.xml.gz' self.printfunction(u"\n == Uploading ==\n") self.printfunction(u" - Generating file to upload\n") uf = gzip.GzipFile(uploadfile, 'w') uf.write("<?xml version='1.0' encoding='UTF-8'?>\n") uf.write("<UploadPools>\n") count = 0 for song in songlisting.keys(): #if it has been modified if song in modifiedlist: count += 1 self.printfunction(u"\n - Packing descriptors of: %s\n" % song) uf.write("<Song id='%s'>" % songlisting[song]) poolfile = path + "/" + song + ".pool" pool = Pool(poolfile) temppool = Pool() #Extract desired attributes for (scope, name) in modifydescriptors: temppool.InsertAttribute(pool, scope, name, scope, name) xml.dom.ext.PrettyPrint(temppool.doc.documentElement, uf) uf.write("</Song>\n") uf.write("</UploadPools>\n") uf.close() if count == 0: self.printfunction( u" - No valid song descriptor has been found. Not doing any upload\n" ) os.remove('uploadfile.xml.gz') return self.printfunction( u" - Uploading the modified descriptor file to the server\n") result = metadataprovider.UploadPackedDescriptors(uploadfile) if result != "OK": self.printfunction( u"\n - ERROR in the server when processing the file: %s\n" % result) else: self.printfunction( u" - File successfully uploaded: received %s\n" % result) #Clean! os.remove('uploadfile.xml.gz') songlisting.close() return 0 except: songlisting.close() return -1 raise TaskerError( "Upload Error\nError preparing files to upload. Maybe the files no longer exist?" )