def evaluate(self, jobs): ''' Evaluates a given list of jobs, avoiding calculating duplicates. :param jobs: list of jobs with scores to be computed :return: list of individuals with scores computed ''' self.results = [] job_keys = set(map(lambda x: x.get_key(), jobs)) inner_jobs = [] for phenotype, epochs, seed in job_keys: individual = Individual(phenotype) j = Job(individual, epochs, seed) inner_jobs.append(j) inner_results = {} expected_results = len(inner_jobs) while len(inner_results) < expected_results: # makes adding new workers while the script # is running possible self.__add_workers() for worker in self.workers[:]: if worker.is_available() and inner_jobs: job = inner_jobs.pop() print("Worker available. Assigning job ({}/{}): {}".format( expected_results - len(inner_jobs), expected_results, job)) success = worker.assign_job(job) if not success: print("Worker %s broken in assigning job," " removing from pool and reasigning job" % worker.ip) inner_jobs.append(worker.get_current_job()) self.workers.remove(worker) for worker in self.workers[:]: try: if not worker.is_available(): if worker.has_result(): result = worker.get_result() job = worker.get_current_job() inner_results[job.get_key()] = result worker.free_worker() except Exception as e: print(e) print("Worker %s broken in result collection," " removing from pool and reasigning job" % worker.ip) inner_jobs.append(worker.get_current_job()) self.workers.remove(worker) time.sleep(1) for job in jobs: key = job.get_key() result_copy = copy.deepcopy(inner_results[key]) self.results.append(result_copy) return self.results
def evaluate_scores(population, workmanager, computed_scores, m=5, layers=4, seed=1337): jobs = [] computed = [] for individual in population: get_already_computed(individual, computed_scores, m, layers) if not individual.score: jobs.append(Job(individual, 1, seed)) else: computed.append(individual) computed.extend(workmanager.evaluate(jobs)) return computed
from master.queue_handling import WorkManager from common.helper_classes import Individual, Job wm = WorkManager() i1 = Individual((1,1,1,1)) i2 = Individual((1,1,1,1)) j1 = Job(i1) j2 = Job(i2) l = [j1, j2] a = wm.evaluate(l) for r in a: print(r)
from common.helper_classes import Job, Individual from queue_handling import WorkManager i = Individual((1, 1, 1, 1)) j = Job(i) wm = WorkManager() scores = [] jobs = [] for i in range(100): jobs.append(j) population = wm.evaluate(jobs) with open("variance.txt", 'w') as f: for individual in population: f.write("%s\n" % individual.score)
import sys from common.helper_classes import Job, Individual import master.queue_handling as qh from master.csv_handling import * sys.path.append("..") if __name__ == '__main__': filename = "cnn1.csv" codes_to_calculate = get_set_of_codes_from_filename(filename, N=100) jobs = [] for code in set(codes_to_calculate): jobs.append(Job(Individual(genotype_to_phenotype(code, n=4, m=5)), epochs=10, seed=1337)) wm = qh.WorkManager() calculated = wm.evaluate(jobs) save_csv_and_history("zbiorczy.csv", calculated, iteration=0, n=4, m=5)