def generateSamples(numTeams, numSam, sampleDir): print("Generating " + str(numSam) + " samples for " + str(numTeams) + " teams from Java api") start = time.clock() # cU.buildDirectory(sampleDir) cU.removeCSVFiles(sampleDir) args = [ os.path.join(os.getcwd(), "static", "SportScheduleGenerator.jar"), str(numTeams), str(numSam), sampleDir ] # Any number of args to be passed to the jar file print(sU.jarWrapper(*args)) print("Generated ", numSam, " samples in ", time.clock() - start, " secs")
def generate4DSamples(numTeams, numSam, numCycles, sampleDir, mbounds): print("Generating " + str(numSam) + " samples for " + str(numTeams) + "teams and " + str(numCycles) + " cycles teams from Gurobi api") start = time.clock() cU.buildDirectory(sampleDir) cU.removeCSVFiles(sampleDir) sampler.generate_multi_dim_sample( bounds=mbounds, directory=sampleDir, num_teams=numTeams, num_md_per_cycle=calculateMatchDaysPerCycle(numTeams), numSam=100, numCycle=numCycles) print("Generated ", numSam, " samples in ", time.clock() - start, " secs")
def generate4DSamples(numTeams, numSam, numCycles, sampleDir, mbounds, unsolvable, lost_data, skill_group=None, skillbounds0=None, skillbounds1=None,skillbounds_home0=None,skillbounds_home1=None): ''' Generates a numSam of solutions for the sport scheduling problem with numTeams teams and stores them in sampleDir :param numTeams: Number of teams for the SSP :param numSam: Number of samples to generate :param numCycles: number of cycles in the ssp :param sampleDir: directory where the generated samples end up in :param mbounds: ''' missed_cycle = False print("Generating " + str(numSam) + " samples for " + str(numTeams) + "teams and " + str( numCycles) + " cycles teams from Gurobi") start = time.clock() cU.buildDirectory(sampleDir) cU.removeCSVFiles(sampleDir) num_games_cycle = sU.calculateMatchDaysPerCycle(numTeams) sampler.generate_multi_dim_sample(bounds=mbounds, directory=sampleDir, num_teams=numTeams, num_md_per_cycle=sU.calculateMatchDaysPerCycle(numTeams), numSam=numSam, numCycle=numCycles, unlearnable=unsolvable, sk=skill_group, bounds0=skillbounds0, bounds1=skillbounds1,bounds_home0 = skillbounds_home0,bounds_home1=skillbounds_home1) if lost_data: files = next(os.walk(sampleDir))[2] for idx, file in enumerate(files): random.seed(idx) file_path = os.path.join(sampleDir, file) data = readCSV(file_path) if missed_cycle: dropped = np.delete(data, np.s_[int((data.shape[1] + 1) / 2):data.shape[1]], axis=1) mirror = np.delete(dropped, 0, axis=1) mirror[0] = np.repeat('C1', mirror.shape[1]) mirror[3:] = np.concatenate(np.split(np.transpose(mirror[3:]), num_games_cycle), axis=1) result = np.concatenate((dropped, mirror), axis=1) pd.DataFrame(result).to_csv(file_path, header=False, index=False) else: first_col = data[3:][:, 0] blocks = np.delete(data[3:], 0, axis=1) splitted = np.split(blocks, (num_games_cycle * 2), axis=1) idx1 = random.randint(0, (num_games_cycle * 2)-1) idx2 = random.randint(0, (num_games_cycle * 2)-1) if idx1 == idx2: idx1 -= 1 splitted[idx1], splitted[idx2] = splitted[idx2], splitted[idx1] newset = np.concatenate((splitted),axis=1) stack = np.column_stack((first_col,newset)) data[3:] = stack pd.DataFrame(data).to_csv(file_path, header=False, index=False) print("Generated ", numSam, " samples in ", time.clock() - start, " secs")
def randomSplit(dir, learnsplit): path, dirs, files = next(os.walk(dir)) cU.buildDirectory(os.path.join(dir, "learn")) cU.removeCSVFiles(os.path.join(dir, "learn")) cU.buildDirectory(os.path.join(dir, "test")) cU.removeCSVFiles(os.path.join(dir, "test")) amt_files = len(files) amt_learnfiles = learnsplit * amt_files learnfiles = random.sample(files, int(amt_learnfiles)) for file in learnfiles: shutil.move(os.path.join(dir, file), os.path.join(dir, "learn", file)) for file in next(os.walk(dir))[2]: shutil.move(os.path.join(dir, file), os.path.join(dir, "test", file))
def generate4DSamples(numTeams, numSam, numCycles, sampleDir, mbounds): ''' Generates 4D samples of the SSP :param numTeams: Number of teams for the SSP :param numSam: Number of samples to generate :param numCycles: number of cycles in the ssp :param sampleDir: directory where the generated samples end up in :param mbounds: :return: ''' print("Generating " + str(numSam) + " samples for " + str(numTeams) + "teams and " + str(numCycle) + " cycles teams from Gurobi") start = time.clock() cU.buildDirectory(sampleDir) cU.removeCSVFiles(sampleDir) sampler.generate_multi_dim_sample( bounds=mbounds, directory=sampleDir, num_teams=numTeams, num_md_per_cycle=sU.calculateMatchDaysPerCycle(numTeams), numSam=numSam, numCycle=numCycles, theoretical=True) print("Generated ", numSam, " samples in ", time.clock() - start, " secs")
def buildSolutionAndResultDirs(directory): if not os.path.exists(directory): os.makedirs(directory) my_csv, csvWriter = sU.openMainCsv(directory) det_csv, detCsvWriter = sU.openDetCsv(directory) soln = os.path.join(directory, "solutions") result = os.path.join(directory, "results", "learnedBounds") prec = os.path.join(directory, "results", "validation") if not os.path.exists(soln): os.makedirs(soln) if not os.path.exists(result): os.makedirs(result) if not os.path.exists(prec): os.makedirs(prec) cU.removeCSVFiles(soln) cU.removeCSVFiles(result) cU.removeCSVFiles(prec) return soln, result, prec, csvWriter, detCsvWriter, det_csv, my_csv
constrMaxval) tot_time[seed] = ( (timeTaken * numSol) / numSam) + (time.clock() - start) if (not (np.array_equal(bounds_learned, bounds_prev))): selbounds = np.array( [lbounds[i] for i in range(len(lbounds)) if i not in selRows]) for i in range(len(selbounds)): accept = sU.moreConstrained(bounds_learned, selbounds[i], num_constrType, num_constr) recall += accept tot_rec[seed] = (recall * 100) / (numSam - numSol) tmpDir = os.path.join(directory, "tmp") cU.buildDirectory(tmpDir) cU.removeCSVFiles(tmpDir) soln = os.path.join(tmpDir, "solutions") cU.buildDirectory(soln) cU.removeCSVFiles(soln) result = os.path.join(tmpDir + "results") cU.buildDirectory(result) cU.removeCSVFiles(result) print("\nGenerating samples using learned constraints") start = time.clock() sampler.generateSample(numTeams, num_Matchdays, numSam, bounds_learned, soln) print("Generated ", numSam, " samples in ", time.clock() - start, ' secs')
# learn the combined model from the java schedules and store it in results/learnedBounds timeTaken = learnConstraintsFromFiles(learndir, sampled_files, result) print( str(seed) + ": Learned bounds for ", len(sampled_files), " samples in ", timeTaken, ' secs \n') tag = str(numSol) + "Amt_T" + str(numTeams) file = os.path.join(directory, "results", "learnedBounds", "_" + tag + "0.csv") lbounds = sU.readBounds(file, num_constrType, num_constr) aggr_bounds = sU.aggrBounds(lbounds, num_constrType, num_constr, constrMaxval) tmpDir = os.path.join(directory, "tmp") tmpResult = os.path.join(tmpDir, "learnedBounds") cU.buildDirectory(tmpDir) cU.removeCSVFiles(tmpDir) numsam = len(next(os.walk(os.path.join(soln, "test")))[2]) # build numSam samples from the learned constraints sampler.generate_multi_dim_sample(num_teams=numTeams, num_md_per_cycle=num_Matchdays, numSam=numsam, numCycle=numCycle, bounds=aggr_bounds, directory=tmpDir, theoretical=False) # provide aggr bounds recall = calculateRecallFromFile(file=file, testsolndir=os.path.join(
def saveConstraintsForAll(dataTensor, variables, indicator, directory, tag, orderingNotImp=[2, 3]): repeatDim = () rep = set([ variable for variable in range(len(variables)) if variable not in repeatDim ]) subsets = cF.split(rep, (), repeatDim) concatdir = directory if indicator == 0: cU.buildDirectory(concatdir) cU.removeCSVFiles(concatdir) with open(os.path.join(concatdir, "_" + tag + ".csv"), "a", newline='') as my_csv: csvWriter = csv.writer(my_csv, delimiter=',') # First run in this script provides the header row if indicator == 0: row = ([''] * 2) for subset in subsets: row.extend(subset) row.extend([''] * 5) csvWriter.writerow(row) else: row = [] for l in range(len(subsets)): subset = subsets[l] newset = subset[0] + subset[1] # this value is used to filter max constraints maxPossible = 1 for i in range(len(subset[1])): maxPossible *= len(variables[subset[1][i]]) idTensor = cF.tensorIndicator(dataTensor, newset, variables) sumSet = range(len(subset[0]), len(newset)) sumTensor_max, sumTensor_min = cF.tensorSum( idTensor, sumSet, np.array(variables)[list(newset)], 0) # row.extend([sumTensor_min,sumTensor_max]) # filter all the max poss values out (trivial constraints for everything) if sumTensor_min == maxPossible or sumTensor_min == 0: row.extend(['']) else: row.extend([sumTensor_min]) if sumTensor_max == maxPossible or sumTensor_max == 0: row.extend(['']) else: row.extend([sumTensor_max]) # first filter base on orderingNotImp if len(set(subset[1])) == 1 and len( set(orderingNotImp) & set(subset[1])) == 0: # second filter based on the (0,)(x,) constraints who are useles for us and filter constraints with # all the dimensions involved in |D|, also filter everything out with orderingnotImp completely in S # also filter everything out with cycle ordering in M, since this also is irrelevant to us if not (len(newset) == 2 and newset[1] == 1) and not (len( newset) == len(rep)) and not (subset[0] == tuple( orderingNotImp)) and not (subset[1] == 0): minConsZero, maxConsZero, minConsNonZero, maxConsNonZero = cF.tensorConsZero( idTensor, sumSet, np.array(variables)[list(newset)]) # row.extend([minConsZero,maxConsZero,minConsNonZero,maxConsNonZero]) if minConsZero == maxPossible or minConsZero == 0: row.extend(['']) else: row.extend([minConsZero]) if maxConsZero == maxPossible or maxConsZero == 0: row.extend(['']) else: row.extend([maxConsZero]) if minConsNonZero == maxPossible or minConsNonZero == 0: row.extend(['']) else: row.extend([minConsNonZero]) if maxConsNonZero == maxPossible or maxConsNonZero == 0: row.extend(['']) else: row.extend([maxConsNonZero]) # row.extend(['']*4) else: row.extend([''] * 4) else: # when we dont capture the max cons sequence, we extend the set by four whitespaces row.extend([''] * 4) row.extend(['']) csvWriter.writerow(row)
def saveConstraintsForAll(dataTensor, variables, indicator, directory, tag, orderingNotImp=[2, 3]): repeatDim = () rep = set([ variable for variable in range(len(variables)) if variable not in repeatDim ]) subsets = cF.split(rep, (), repeatDim) concatdir = directory if indicator == 0 and tag[-2] != '0': cU.buildDirectory(concatdir) cU.removeCSVFiles(concatdir) with open(os.path.join(concatdir, "_" + tag + ".csv"), "a", newline='') as my_csv: csvWriter = csv.writer(my_csv, delimiter=',') # First run in this script provides the header row if indicator == 0: row = ([''] * 2) for subset in subsets: row.extend(subset) row.extend([''] * 11) csvWriter.writerow(row) else: row = [] for l in range(len(subsets)): subset = subsets[l] newset = subset[0] + subset[1] # this value is used to filter max constraints maxPossible = 1 for i in range(len(subset[1])): maxPossible *= len(variables[subset[1][i]]) idTensor = cF.tensorIndicator(dataTensor, newset, variables) sumSet = range(len(subset[0]), len(newset)) sumTensor_max, sumTensor_min = cF.tensorSum( idTensor, sumSet, np.array(variables)[list(newset)]) # row.extend([sumTensor_min,sumTensor_max]) # filter all the max poss values out (trivial constraints for everything) if sumTensor_min == maxPossible or sumTensor_min == 0: row.extend(['']) else: row.extend([sumTensor_min]) if sumTensor_max == maxPossible or sumTensor_max == 0: row.extend(['']) else: row.extend([sumTensor_max]) # first filter base on orderingNotImpd if len(set(subset[1])) == 1 and len( set(orderingNotImp) & set(subset[1])) == 0: # second filter based on the (0,)(x,) constraints who are useles for us and filter constraints with # all the dimensions involved in |D|, also filter everything out with orderingnotImp completely in S # also filter everything out with cycle ordering in M, since this also is irrelevant to us if not (len(newset) == 2 and newset[1] == 1) and not (len( newset) == len(rep)) and not (subset[0] == tuple( orderingNotImp)) and not (subset[1] == 0): minConsZero, maxConsZero, minConsNonZero, maxConsNonZero = cF.tensorConsZero( idTensor, sumSet, np.array(variables)[list(newset)]) # row.extend([minConsZero,maxConsZero,minConsNonZero,maxConsNonZero]) if minConsZero == maxPossible or minConsZero == 0: row.extend(['']) else: row.extend([minConsZero]) if maxConsZero == maxPossible or maxConsZero == 0: row.extend(['']) else: row.extend([maxConsZero]) if minConsNonZero == maxPossible or minConsNonZero == 0: row.extend(['']) else: row.extend([minConsNonZero]) if maxConsNonZero == maxPossible or maxConsNonZero == 0: row.extend(['']) else: row.extend([maxConsNonZero]) # row.extend(['']*4) else: row.extend([''] * 4) else: # when we dont capture the max cons sequence, we extend the set by four whitespaces row.extend([''] * 4) if (idTensor.shape[len(newset) - 1] == idTensor.shape[len(newset) - 2]) & (len(newset) == len(rep)): # first filter, see that the last 2 dimension are of equal length # second filter, see that M and S contain all the dimensions in D tracing_min, tracing_max = cF.tensorTracing(idTensor) row.extend([tracing_min, tracing_max]) else: row.extend([''] * 2) # filter: newset = |D|, |M|=1, |S| = |D|-1 + having a swapping length + make sure last 2 dimension of id tensor are equal if (len(newset) == len(rep)) & (len( subset[0]) == len(rep) - 1) & (len(subset[1]) == 1) & ( subset[1][0] == 2 or subset[1][0] == 3) & (idTensor.shape[-1] == idTensor.shape[-2]): # paraamterize this comp_newset = (newset[0], newset[1], newset[-1], newset[-2]) id_tensor_comp = cF.tensorIndicator( dataTensor, comp_newset, variables) countplus_min, count_plus_max = cF.count_plus( id_tensor_orig=idTensor, sumset=sumSet, var_orig=np.array(variables)[list(newset)], id_comp=id_tensor_comp, var_comp=np.array(variables)[list(comp_newset)]) row.extend([countplus_min, count_plus_max]) else: row.extend([''] * 2) if False: between_games_min, between_games_max = 0, 0 row.extend([between_games_min, between_games_max]) else: row.extend([''] * 2) row.extend(['']) csvWriter.writerow(row)