def test(problemFile, problemName): print("TEST") problem = Problem.LoadProblem(problemFile) t0 = time() while (not problem.isSolved()): vertexHeuristic, colorHeuristic = choice(HeuristicFunctionArray) vertex = vertexHeuristic(problem) color = colorHeuristic(problem, vertex) problem.assignColor(vertex, color) t1 = time() print("Finished in {}".format(t1 - t0)) print("PROBLEM SOLVED USING {} colors".format( np.count_nonzero(problem.colors))) problem.printNodes() problem.writeNodes("testTables", "RANDOMSOL-{}".format(problemName))
def main(): # problemFile = "./Instances/queen/queen8_8.col" # problemFile = "./Instances/inithx/inithx.i.2.col" problemFile = "./Instances/miles/miles1500.col" problem = Problem.LoadProblem(problemFile) problemName = problemFile.split("/")[-1] hist = np.zeros((1, 3, 7)) currentStatus = problem.Status() for i in range(0, len(hist[0])): hist[0][i] = np.concatenate((problem.NumericalStatus(), [0]), axis=0) with tf.Session() as sess: model = loadModel() t0 = time() while (not problem.isSolved()): heuristic = np.argmax(model.predict(hist)) #From one hot to index vertexHeuristic, colorHeuristic = HeuristicFunctionArray[heuristic] vertex = vertexHeuristic(problem) color = colorHeuristic(problem, vertex) problem.assignColor(vertex, color) #Update hist and predict next step hist[0][0] = hist[0][1] hist[0][1] = hist[0][2] hist[0][2] = np.concatenate( (problem.NumericalStatus(), [heuristic])) t1 = time() print("Finished in {}".format(t1 - t0)) print("PROBLEM SOLVED USING {} colors".format( np.count_nonzero(problem.colors))) problem.printNodes() problem.writeNodes("testTables", "LSTMSOL-{}".format(problemName)) print("RANDOM TEST") test(problemFile, problemName)
bestMove = moveArray[maxFitnessIndex] problem.assignColor(bestMove[0], bestMove[1]) return maxFitnessIndex, fitnessArray[maxFitnessIndex] if __name__ == '__main__': # problemFile = "./Instances/queen/queen16_16.col" problemFile = "./Instances/mulsol/mulsol.i.1.col" name = problemFile.split("/")[-1] if (not os.path.isdir("data/{}".format(name))): os.mkdir("data/{}".format(name)) problem = None for i in range(10): problem = Problem.LoadProblem(problemFile) file = open("data/{}/{}.csv".format(name, i), "w") t0 = time() file.write( "currentColorRatio, currentNodeRatio, averageNodesUsedPerColor, averageEdgesPerNode, minNeighbors, maxNeighbors, bestH\n" ) with tqdm(total=len(problem.colors)) as bar: while (not problem.isSolved()): bestH, fit = EvalPosibilities(problem) # file.write("Heuristic {} Fitness: {}\n".format(bestH, fit)) file.write("{},{}\n".format(problem.Status(), bestH)) # print(np.sum(problem.colors)) progress = np.sum(problem.colors) bar.update(progress - bar.n) # problem.printNodes() t1 = time()