def solve_dcop(self, dcop): ms = MaxSum(dcop, "max") ms.setUpdateOnlyAtEnd(False) ms.setIterationsNumber(3) #changed this line for test ms.solve_complete() return ms
nodeFunctions.append(nodefunction3) agent.addNodeVariable(nodeVariable1) agent.addNodeVariable(nodeVariable2) agent.addNodeFunction(nodefunction0) agent.addNodeFunction(nodefunction1) agent.addNodeFunction(nodefunction2) agent.addNodeFunction(nodefunction3) agents.append(agent) cop = COP_Instance(nodeVariables, nodeFunctions, agents) return cop if __name__ == "__main__": cop = create_DCop2() ms = MaxSum(cop, "max") ms.setUpdateOnlyAtEnd(False) ms.setIterationsNumber(3) ms.solve_complete() result = ms.get_results2() for variable in result: print str(variable) + ": " + str(result[variable]) #report = ms.getReport() #print report
def main(): ''' invoke the parser that takes parameters from the command line ''' args = getParser() ''' How many iterations? ''' nIterations = args.iterations ''' How many instances? ''' nIstances = args.instances ''' number of variables in Dcop ''' nVariables = args.variables ''' max/min ''' op = args.op ''' location of MaxSum report ''' reportMaxSum = args.reportMaxSum ''' location of FactorGraph report ''' infoGraphPathFile = args.reportFactorGraph ''' location of Charts ''' chartsFile = args.reportCharts ''' Constraint Optimization Problem ''' cop = None ''' average of 50 instances of problem ''' average = list() for i in range(nVariables + 1): average.append(0) state = 0 variables = list() functions = list() agents = list() agent = None finalAssignaments = list() oldAssignaments = list() core = None originalCop = None actualValue = 0 ''' list of all values of RUN for each iteration ''' averageValues = list() iterations = list() iterationsInstances = list() averageValuesInstances= list() varSet = (0.1 * nVariables) ''' create directories ''' if not os.path.exists("ParallelConditioningID/"): os.makedirs("ParallelConditioningID/") if not os.path.exists("ParallelConditioningID/Charts/"): os.makedirs("ParallelConditioningID/Charts/") if not os.path.exists("ParallelConditioningID/FactorGraph/"): os.makedirs("ParallelConditioningID/FactorGraph/") string = "Max Sum\t Average Conflicts\n" for run in range(nIstances): averageValues = list() iterations = list() ''' fileName of log (Iteration Conflicts AverageDifferenceLink) save on file Iteration Conflicts AverageDifferenceLink ''' finalAssignaments = list() oldAssignaments = list() for i in range(nVariables): finalAssignaments.append(0) oldAssignaments.append(0) ''' values of MaxSum for each iteration ''' values = list() ''' create a new COP with a colored Graph and save the factorgraph on file ''' cop = create_DCop(infoGraphPathFile, nVariables, run) i = 0 going = False functions = cop.getNodeFunctions() variables = cop.getNodeVariables() ''' repeat the algorithm for 50 times every exec set the value of variable ''' while((len(variables) > 0) & (len(functions) > 0)): ''' if it isn't the first exec of MaxSum, change the factor graph and repeat the algorithm ''' if(going == True): ''' the agent of the cop ''' agent = (core.getCop().getAgents())[0] ''' the variables of the cop ''' variables = core.getCop().getNodeVariables() ''' the functions of the cop ''' functions = core.getCop().getNodeFunctions() variablesToRemove = list() w = 0 ''' choose variables in order by ID ''' while((w < varSet) & (w < len(variables))): variablesToRemove.append(variables[w]) w = w + 1 for j in range(len(variablesToRemove)): ''' state of variable i ''' state = variablesToRemove[j].getStateArgument() print('state of variable:', variablesToRemove[j].toString(), state.getValue()) index = getIndex(originalCop,variablesToRemove[j]) ''' save the value of the variable ''' finalAssignaments[index] = state.getValue() oldAssignaments[index] = finalAssignaments[index] for var in variablesToRemove: ''' remove the variable on the agent ''' agent.removeNodeVariable(var) for var in variablesToRemove: ''' neighbours of variable ''' neighbours = var.getNeighbour() for function in neighbours: functions.remove(function) agent.removeNodeFunction(function) ''' remove the variable from the list ''' variables.remove(var) for variable in variables: n1 = set(neighbours) n2 = set(variable.getNeighbour()) if((len(set(n1).intersection(n2))) > 0 ): ''' intersection of neighbours ''' inter = (set(n1).intersection(n2)) while(len(inter) > 0): func = inter.pop() ''' list of the arguments of the function ''' argumentsOfFunction = list() funId = 0 ''' there is at least a function ''' if((len(functions)) > 0): funId = ((functions[len(functions) - 1]).getId()) + 1 else: funId = 0 ''' creates an unary function linked to the variable ''' nodefunction = NodeFunction(funId) functionevaluator = TabularFunction() nodefunction.setFunction(functionevaluator) ''' add this nodefunction to the actual nodevariable's neighbour ''' variable.addNeighbour(nodefunction) ''' add this variable as nodefunction's neighbour ''' nodefunction.addNeighbour(variable) ''' add the variable as an argument of the function ''' argumentsOfFunction.append(variable) costTable = (func.getFunction()).getCostTable() cost = 0 ''' state of variableToRemove ''' state = var.getStateArgument() index = (func.getFunction()).getParameterPosition(var) ''' create the unary function ''' for j in range(0, variable.size()): t = (j) if(index == 0): cost = costTable[(state.getValue(),j)] else: cost = costTable[(j,(state.getValue()))] ''' add to the cost function: [parameters -> cost] ''' nodefunction.getFunction().addParametersCost(t, cost) ''' add the neighbors of the function node ''' nodefunction.getFunction().setParameters(argumentsOfFunction) ''' add the function node ''' functions.append(nodefunction) ''' add the function node to the agent ''' agent.addNodeFunction(nodefunction) variable.removeNeighbours(neighbours) if((len(variables) > 0) & (len(functions) > 0)): agents = list() agents.append(agent) cop = COP_Instance(variables, functions, agents) i = i + varSet if((len(variables) > 0) & (len(functions) > 0)): ''' create new MaxSum instance (max/min) ''' core = MaxSum(cop, op) ''' update only at end? ''' core.setUpdateOnlyAtEnd(False) core.setIterationsNumber(nIterations) start_time = time.time() ''' invoke the method that executes the MaxSum algorithm ''' core.solve_complete() values = core.getValues() elapse_time = time.time() - start_time print('MaxSum elapse_time:', elapse_time) going = True if(i == 0): ''' create a copy of cop ''' originalCop = deepcopy(core.getCop()) oldVariables = originalCop.getNodeVariables() for k in range(len(oldVariables)): oldAssignaments[k] = oldVariables[k].getStateIndex() actualValue = calculateActualValue(originalCop,oldAssignaments) averageValues.append(actualValue) iterations.append(len(values)) if((len(variables) > 0) & (len(functions) == 0)): ''' the variables of the cop ''' variables = core.getCop().getNodeVariables() ''' remaining variables to set ''' index = (nVariables - i) - 1 k = 0 j = 1 while(j < ((index / varSet) + 1)): while(k < (varSet * j)): ''' state of variable i ''' state = (variables[k]).getStateArgument() ''' save the value of the variable ''' finalAssignaments[i] = state.getValue() oldAssignaments[i] = finalAssignaments[i] i = i + k k = k + 1 actualValue = calculateActualValue(originalCop,oldAssignaments) averageValues.append(actualValue) iterations.append(len(values)) j = j + 1 for i in range(len(iterations)): if(i > 0): iterations[i] = iterations[i] + iterations[i-1] averageValuesInstances.append(averageValues) iterationsInstances.append(iterations) # draw the chart # x axis: number of MaxSum exec # y axis: conflicts x = iterations ''' x axis: number of MaxSum exec ''' y = averageValues pl.title('Cost / Iterations chart') pl.xlabel('Iterations') pl.ylabel('Cost') pl.plot(x, y) pl.savefig(chartsFile + "ParallelConditioningID/Charts/Chart_RUN_" + str(run) + ".png") pl.close() sumIterations = [sum(x) for x in zip(*iterationsInstances)] sumValues = [sum(x) for x in zip(*averageValuesInstances)] for i in range(len(sumIterations)): sumIterations[i] = sumIterations[i] / nIstances for i in range(len(sumValues)): sumValues[i] = sumValues[i] / nIstances # draw the chart # x axis: number of MaxSum exec # y axis: conflicts x = sumIterations ''' x axis: number of MaxSum exec ''' y = sumValues pl.title('Cost / Iterations chart') pl.xlabel('Iterations') pl.ylabel('Cost') pl.plot(x, y) #pl.show() pl.savefig(chartsFile + "ParallelConditioningID/Charts/AverageAllInstances.png") pl.close() string = 'Iteration\tConflict\n' for i in range(len(sumIterations)): string = string + str(sumIterations[i]) + '\t\t' + str(sumValues[i]) + '\n' output_file = open(infoGraphPathFile + "ParallelConditioningID/FactorGraph/reportIterations.txt", "w") output_file.write(string) output_file.write("\n") output_file.close()
def main(): ''' invoke the parser that takes parameters from the command line ''' args = getParser() ''' How many iterations? ''' nIterations = args.iterations ''' How many instances? ''' nIstances = args.instances ''' number of variables in Dcop ''' nVariables = args.variables ''' max/min ''' op = args.op ''' location of MaxSum report ''' reportMaxSum = args.reportMaxSum ''' location of FactorGraph report ''' infoGraphPathFile = args.reportFactorGraph ''' location of Charts ''' chartsFile = args.reportCharts ''' Constraint Optimization Problem ''' cop = None ''' average of 50 instances of problem ''' average = list() for i in range(nVariables + 1): average.append(0) state = 0 variables = list() functions = list() agents = list() agent = None finalAssignaments = list() oldAssignaments = list() core = None originalCop = None actualValue = 0 ''' list of all values of RUN for each iteration ''' averageValues = list() iterations = list() iterationsInstances = list() averageValuesInstances = list() maxGradeList = list() varSet = (0.1 * nVariables) ''' create directories ''' if not os.path.exists("MaxSumParallelHGMaxHop/"): os.makedirs("MaxSumParallelHGMaxHop/") if not os.path.exists("MaxSumParallelHGMaxHop/Charts/"): os.makedirs("MaxSumParallelHGMaxHop/Charts/") if not os.path.exists("MaxSumParallelHGMaxHop/FactorGraph/"): os.makedirs("MaxSumParallelHGMaxHop/FactorGraph/") string = "Max Sum\t Average Conflicts\n" for run in range(nIstances): averageValues = list() iterations = list() ''' fileName of log (Iteration Conflicts AverageDifferenceLink) save on file Iteration Conflicts AverageDifferenceLink ''' finalAssignaments = list() oldAssignaments = list() for i in range(nVariables): finalAssignaments.append(0) oldAssignaments.append(0) ''' values of MaxSum for each iteration ''' values = list() ''' create a new COP with a colored Graph and save the factorgraph on file ''' cop = create_DCop(infoGraphPathFile, nVariables, run) i = 0 going = False functions = cop.getNodeFunctions() variables = cop.getNodeVariables() ''' repeat the algorithm for 50 times every exec set the value of variable ''' while ((len(variables) > 0) & (len(functions) > 0)): ''' if it isn't the first exec of MaxSum, change the factor graph and repeat the algorithm ''' if (going == True): ''' the agent of the cop ''' agent = (core.getCop().getAgents())[0] ''' the variables of the cop ''' variables = core.getCop().getNodeVariables() ''' the functions of the cop ''' functions = core.getCop().getNodeFunctions() graph = dict() hop = dict() for variable in variables: graph[variable] = list() ''' for each node function ''' for f in variable.getNeighbour(): for v in f.getNeighbour(): if ((v.getId()) != (variable.getId())): graph[variable].append(v) hop[(variable, v)] = float('-inf') for v in range(len(variables)): for k in range(v + 1, len(variables)): ''' if there is at least a neighbour ''' if (len(graph[variable]) > 0): ''' bfs visit with shortest path ''' shortestPath = list( bfs_shortest_path(graph, variables[v], variables[k])) ''' if there is a path, update the number of hops ''' if (len(shortestPath) > 0): hop[(variables[v], variables[k])] = len(shortestPath) - 1 variablesToRemove = list() maxGrade = float('-inf') v = None ''' find the variable with max grade ''' for var in graph.keys(): if (len(graph[var]) > maxGrade): maxGrade = len(graph[var]) v = var variablesToRemove.append(v) maxGrade = float('-inf') maxHop = float('-inf') v = None ''' find the variable with max distance from random variable and with max grade ''' for tuple in hop.keys(): if ((tuple[0].getId()) == (variablesToRemove[0].getId())): if ((hop[tuple] > maxHop) & (len(graph[tuple[1]]) > maxGrade)): maxHop = hop[tuple] maxGrade = len(graph[tuple[1]]) ''' far variable ''' v = tuple[1] ''' there is a variable with max hop and max grade ''' if (v != None): ''' ad the variable more far ''' variablesToRemove.append(v) while ((len(variablesToRemove) < varSet) & ((len(variables) - len(variablesToRemove)) > 0)): inter = list() ''' find the intersection ''' for j in range(1, len(variablesToRemove)): if (j == 1): inter = list( set(graph[variablesToRemove[ j - 1]]).intersection( set(graph[variablesToRemove[j]]))) else: inter = list( set(inter).intersection( set(graph[variablesToRemove[j]]))) liste = list() maxGradeList = list() v = None if (len(inter) > 0): while ((len(maxGradeList) < varSet) & (len(inter) > 0)): v = None count = 0 maxGrade = float('-inf') while (count < len(inter)): var = inter[count] if (len(graph[var]) > maxGrade): maxGrade = len(graph[var]) v = var count = count + 1 maxGradeList.append(v) ''' remove the variable with highest grade ''' inter.remove(v) ''' intersection not empty ''' if (len(maxGradeList) > 0): k = 0 while (k < len(variablesToRemove)): list1 = list() for n in maxGradeList: list1.append(hop[(variablesToRemove[k], n)]) liste.append(list1) k = k + 1 ''' sum the distances between variables ''' content = [sum(x) for x in zip(*liste)] maxHopIndex = content.index(max(content)) ''' add the variable more far ''' variablesToRemove.append(maxGradeList[maxHopIndex]) else: ''' remaining variables to choose ''' diff = list( set(variables) - set(variablesToRemove)) while ((len(variablesToRemove) < varSet) & (len(diff) > 0)): ''' choose random variables ''' indexRemove = random.randint(0, len(diff) - 1) ''' add the random variable ''' variablesToRemove.append(diff[indexRemove]) diff.remove(diff[indexRemove]) else: ''' remaining variables to choose ''' diff = list(set(variables) - set(variablesToRemove)) while (((len(variablesToRemove) < varSet) & (len(diff) > 0))): ''' choose random variables ''' indexRemove = random.randint(0, len(diff) - 1) ''' add the random variable ''' variablesToRemove.append(diff[indexRemove]) diff.remove(diff[indexRemove]) for j in range(len(variablesToRemove)): ''' state of variable i ''' state = variablesToRemove[j].getStateArgument() print('state of variable:', variablesToRemove[j].toString(), state.getValue()) index = getIndex(originalCop, variablesToRemove[j]) ''' save the value of the variable ''' finalAssignaments[index] = state.getValue() oldAssignaments[index] = finalAssignaments[index] for var in variablesToRemove: ''' remove the variable on the agent ''' agent.removeNodeVariable(var) for var in variablesToRemove: ''' neighbours of variable ''' neighbours = var.getNeighbour() for function in neighbours: functions.remove(function) agent.removeNodeFunction(function) ''' remove the variable from the list ''' variables.remove(var) for variable in variables: n1 = set(neighbours) n2 = set(variable.getNeighbour()) if ((len(set(n1).intersection(n2))) > 0): ''' intersection of neighbours ''' inter = (set(n1).intersection(n2)) while (len(inter) > 0): func = inter.pop() ''' list of the arguments of the function ''' argumentsOfFunction = list() funId = 0 ''' there is at least a function ''' if ((len(functions)) > 0): funId = ((functions[len(functions) - 1]).getId()) + 1 else: funId = 0 ''' creates an unary function linked to the variable ''' nodefunction = NodeFunction(funId) functionevaluator = TabularFunction() nodefunction.setFunction(functionevaluator) ''' add this nodefunction to the actual nodevariable's neighbour ''' variable.addNeighbour(nodefunction) ''' add this variable as nodefunction's neighbour ''' nodefunction.addNeighbour(variable) ''' add the variable as an argument of the function ''' argumentsOfFunction.append(variable) costTable = (func.getFunction()).getCostTable() cost = 0 ''' state of variableToRemove ''' state = var.getStateArgument() index = (func.getFunction() ).getParameterPosition(var) ''' create the unary function ''' for j in range(0, variable.size()): t = (j) if (index == 0): cost = costTable[(state.getValue(), j)] else: cost = costTable[(j, (state.getValue()))] ''' add to the cost function: [parameters -> cost] ''' nodefunction.getFunction( ).addParametersCost(t, cost) ''' add the neighbors of the function node ''' nodefunction.getFunction().setParameters( argumentsOfFunction) ''' add the function node ''' functions.append(nodefunction) ''' add the function node to the agent ''' agent.addNodeFunction(nodefunction) variable.removeNeighbours(neighbours) if ((len(variables) > 0) & (len(functions) > 0)): agents = list() agents.append(agent) cop = COP_Instance(variables, functions, agents) i = i + varSet if ((len(variables) > 0) & (len(functions) > 0)): ''' create new MaxSum instance (max/min) ''' core = MaxSum(cop, op) ''' update only at end? ''' core.setUpdateOnlyAtEnd(False) core.setIterationsNumber(nIterations) start_time = time.time() ''' invoke the method that executes the MaxSum algorithm ''' core.solve_complete() values = core.getValues() elapse_time = time.time() - start_time print('MaxSum elapse_time:', elapse_time) going = True if (i == 0): ''' create a copy of cop ''' originalCop = deepcopy(core.getCop()) oldVariables = originalCop.getNodeVariables() for k in range(len(oldVariables)): oldAssignaments[k] = oldVariables[k].getStateIndex() actualValue = calculateActualValue(originalCop, oldAssignaments) averageValues.append(actualValue) iterations.append(len(values)) if ((len(variables) > 0) & (len(functions) == 0)): ''' the variables of the cop ''' variables = core.getCop().getNodeVariables() ''' remaining variables to set ''' index = (nVariables - i) - 1 k = 0 j = 1 while (j < ((index / varSet) + 1)): while (k < (varSet * j)): ''' state of variable i ''' state = (variables[k]).getStateArgument() ''' save the value of the variable ''' finalAssignaments[i] = state.getValue() oldAssignaments[i] = finalAssignaments[i] i = i + k k = k + 1 actualValue = calculateActualValue(originalCop, oldAssignaments) averageValues.append(actualValue) iterations.append(len(values)) j = j + 1 for i in range(len(iterations)): if (i > 0): iterations[i] = iterations[i] + iterations[i - 1] averageValuesInstances.append(averageValues) iterationsInstances.append(iterations) # draw the chart # x axis: number of MaxSum exec # y axis: conflicts x = iterations ''' x axis: number of MaxSum exec ''' y = averageValues pl.title('Cost / Iterations chart') pl.xlabel('Iterations') pl.ylabel('Cost') pl.plot(x, y) #pl.show() pl.savefig(chartsFile + "MaxSumParallelHGMaxHop/Charts/Chart_RUN_" + str(run) + ".png") pl.close() sumIterations = [sum(x) for x in zip(*iterationsInstances)] sumValues = [sum(x) for x in zip(*averageValuesInstances)] for i in range(len(sumIterations)): sumIterations[i] = sumIterations[i] / nIstances for i in range(len(sumValues)): sumValues[i] = sumValues[i] / nIstances # draw the chart # x axis: number of MaxSum exec # y axis: conflicts x = sumIterations ''' x axis: number of MaxSum exec ''' y = sumValues pl.title('Cost / Iterations chart') pl.xlabel('Iterations') pl.ylabel('Cost') pl.plot(x, y) pl.savefig(chartsFile + "MaxSumParallelHGMaxHop/Charts/AverageAllInstances.png") pl.close() string = 'Iteration\tConflict\n' for i in range(len(sumIterations)): string = string + str(sumIterations[i]) + '\t\t' + str( sumValues[i]) + '\n' output_file = open( infoGraphPathFile + "MaxSumParallelHGMaxHop/FactorGraph/reportIterations.txt", "w") output_file.write(string) output_file.write("\n") output_file.close()
def main(): ''' invoke the parser that takes parameters from the command line ''' args = getParser() ''' How many iterations? ''' nIterations = args.iterations ''' How many instances? ''' nIstances = args.instances ''' number of variables in Dcop ''' nVariables = args.variables ''' max/min ''' op = args.op ''' location of MaxSum report ''' reportMaxSum = args.reportMaxSum ''' create a directory for reports if it isn't exist ''' directory = os.path.dirname(reportMaxSum + "/TestingColoring/") if not os.path.exists(directory): os.makedirs(directory) ''' location of FactorGraph report ''' infoGraphPathFile = args.reportFactorGraph ''' create a directory for reports if it isn't exist ''' directory = os.path.dirname(reportMaxSum + "/FactorGraph/") if not os.path.exists(directory): os.makedirs(directory) ''' location of Charts ''' chartsFile = args.reportCharts ''' create a directory for charts if it isn't exist ''' directory = os.path.dirname(reportMaxSum + "/Charts/") if not os.path.exists(directory): os.makedirs(directory) ''' Constraint Optimization Problem ''' cop = None ''' average of rmessages difference for each link ''' rMessagesAverageDifference = defaultdict(dict) ''' average of rmessages difference for each link and for each iteration ''' rMessagesAverageDifferenceIteration = list() for i in range(nIterations): rMessagesAverageDifferenceIteration.append(0) for run in range(nIstances): ''' fileName of log (Iteration Conflicts AverageDifferenceLink) save on file Iteration Conflicts AverageDifferenceLink ''' fileOutputReport = reportMaxSum + "TestingColoring/TestingColoring_Report_RUN_" + str( run) + ".txt" ''' values of MaxSum for each iteration ''' values = list() for j in range(nIterations): rMessagesAverageDifferenceIteration[j] = 0 ''' create a new COP with a colored Graph and save the factorgraph on file ''' cop = create_DCop(infoGraphPathFile, nVariables, run) ''' create new MaxSum instance (max/min) ''' core = MaxSum(cop, op) ''' update only at end? ''' core.setUpdateOnlyAtEnd(False) core.setIterationsNumber(nIterations) ''' invoke the method that executes the MaxSum algorithm ''' core.solve_complete() ''' values of MaxSum for each iteration in this instance ''' values = core.getValues() ''' average of rmessages difference in the instance ''' rMessagesAverageDifference = core.getRmessagesAverageDifferenceIteration( ) ''' number of link in factor graph ''' links = 0 for key in rMessagesAverageDifference.keys(): for value in rMessagesAverageDifference[key]: ''' count how many links there are ''' links = links + 1 ''' add all the averages of the differences of each link for each iteration ''' for k in range(len(rMessagesAverageDifference[key][value])): rMessagesAverageDifferenceIteration[ k] = rMessagesAverageDifferenceIteration[k] + ( rMessagesAverageDifference[key][value])[k] ''' calculates the average of the differences ''' for k in range(len(values)): rMessagesAverageDifferenceIteration[ k] = rMessagesAverageDifferenceIteration[k] / links final = "\tITERATION\tCONFLICTS\tAVERAGE_DIFFERENCE_LINK\n" for i in range(len(values)): final = final + "\t" + str(i) + "\t\t" + str( values[i]) + "\t\t" + str( rMessagesAverageDifferenceIteration[i]) + "\n" ''' save on file the log file ''' core.stringToFile(final, fileOutputReport) # draw the chart # x axis: number of iterations # y axis: average of the differences in the R messages x = list() ''' x axis: iterations ''' for i in range(nIterations): x.append(i) pl.xticks([20 * k for k in range(0, 16)]) y = rMessagesAverageDifferenceIteration pl.title('Iteration / MediaDiffLink chart') pl.xlabel('Iterations') pl.ylabel('Average_Difference_Link') pl.plot(x, y) # pl.show() pl.savefig(chartsFile + "Charts/ChartMediaDiffLink_RUN_" + str(run) + ".png") pl.close() # draw the chart # x axis: number of iterations # y axis: values of MaxSum (value < 0 = conflict) x = list() ''' x axis: iterations ''' for i in range(nIterations): x.append(i) y = values pl.xticks([20 * k for k in range(0, 16)]) pl.title('Iteration / Conflict chart') pl.xlabel('Iterations') pl.ylabel('Values') pl.plot(x, y) # pl.show() pl.savefig(chartsFile + "Charts/ConflictsChart_RUN_" + str(run) + ".png") pl.close()
def main(): ''' invoke the parser that takes parameters from the command line ''' args = getParser() ''' How many iterations? ''' nIterations = args.iterations ''' How many instances? ''' nIstances = args.instances ''' file of the factor graph ''' file = args.file dire = file[:-2] ''' max/min ''' op = args.op ''' Constraint Optimization Problem ''' cop = None domain = 0 variables = list() functions = list() agents = list() agent = None finalAssignaments = list() oldAssignaments = list() core = None originalCop = None actualValue = 0 ''' list of all values of RUN for each iteration ''' averageValues = list() iterations = list() iterationsInstances = list() averageValuesInstances = list() relations = list() ''' create directories ''' if not os.path.exists("DFSParallelConditioning/"): os.makedirs("DFSParallelConditioning/") if not os.path.exists("DFSParallelConditioning/" + dire): os.makedirs("DFSParallelConditioning/" + dire) if not os.path.exists("DFSParallelConditioning/" + dire + "/Charts/"): os.makedirs("DFSParallelConditioning/" + dire + "/Charts/") if not os.path.exists("DFSParallelConditioning/" + dire + "/FactorGraph/"): os.makedirs("DFSParallelConditioning/" + dire + "/FactorGraph/") dir_charts = "DFSParallelConditioning/" + dire + "/Charts/" dir_factorgraph = "DFSParallelConditioning/" + dire + "/FactorGraph/" string = "Max Sum\t Average Conflicts\n" precop = loadDcopFromFile1(file) factorgraph = precop.getFactorGraph() nVariables = len(factorgraph.getNodeVariables()) varSet = (0.1 * nVariables) choice = True for run in range(nIstances): print('\n\n###########################\n#\n#\tRUN ' + str(run) + '\n#\n###########################') averageValues = list() iterations = list() finalAssignaments = list() oldAssignaments = list() for i in range(nVariables): finalAssignaments.append(0) oldAssignaments.append(0) ''' values of MaxSum for each iteration ''' values = list() ''' id for conditioning functions ''' fId = 200 ''' create a new DCOP by visiting the initial graph save the factorgraph on file ''' print('\ncreating dfs pseudo tree...') dfs_tree = dfs(factorgraph) dfs_tree.dfsVisit() #actual visit domain = factorgraph.getNodeVariables()[0].size() relation = dfs_tree.getRelations() cop = dfs_tree.getDCopPseudoGraph(dir_factorgraph, run, domain) print('...created:\n') i = 0 going = False functions = cop.getNodeFunctions() variables = cop.getNodeVariables() toRemove = list() toRemoveOld = list() iterations_bundling = int(nIterations) total_time = 0 ''' repeat the algorithm for 50 times every exec set the value of variable ''' while ((len(variables) > 0) & (len(functions) > 0)): ''' if it isn't the first exec of MaxSum, change the factor graph and repeat the algorithm ''' if (going == True): ''' the agent of the cop ''' agent = (core.getCop().getAgents())[0] ''' the variables of the cop ''' variables = core.getCop().getNodeVariables() ''' the functions of the cop ''' functions = core.getCop().getNodeFunctions() if not toRemoveOld: toRemove = dfs_tree.getInitToRemove( ) #prende le teste degli alberi graph = dict() for variable in variables: graph[variable] = list() for f in variable.getNeighbour(): for v in f.getNeighbour(): if ((v.getId()) != (variable.getId())): graph[variable].append(v) count = 0 while ((count < (50)) and ((len(variables) - count) > 0) and (len(toRemove) < 10)): v = None maxGrade = float('-inf') for var in graph.keys(): if (len(graph[var]) > maxGrade): maxGrade = len(graph[var]) v = var if (len(toRemove) == 0): toRemove.append(v) else: n2 = set(v.getNeighbour()) for var in toRemove: n1 = set(var.getNeighbour()) inter = set(n1).intersection(n2) if (len(inter) == 0): if v not in toRemove: toRemove.append(v) del graph[v] count = count + 1 toRemoveOld = toRemove else: toRemove = None toRemove = list() for node in toRemoveOld: nodeId = node.getId() for toAppend in relation[nodeId][1]: toRemove.append(toAppend) for rel in relation: for toRem in toRemove: if toRem in rel[1]: rel[1].remove(toRem) if len(toRemove) == 0: if len(variables) < nVariables / 10: for var in variables: toRemove.append(var) else: numberOfFunctions = list() for node_id in range(0, len(variables)): numberOfFunctions.append( len(variables[node_id]. getVariableNeighbour())) while (numberOfFunctions.index( max(numberOfFunctions)) != -1) and len(toRemove) < 5: node_id = numberOfFunctions.index( max(numberOfFunctions)) toRemove.append(variables[node_id]) numberOfFunctions[numberOfFunctions.index( max(numberOfFunctions))] = -1 toRemove = list(toRemove) toRemoveOld = list() toRemoveOld = toRemove iterations_bundling = iterations_bundling - len(toRemove) state = 0 for j in range(len(toRemove)): ''' state of variable i ''' state = toRemove[j].getStateArgument() print('conditioning on:', toRemove[j].toString(), state.getValue()) index = getIndex(originalCop, toRemove[j]) ''' save the value of the variable ''' finalAssignaments[index] = state.getValue() oldAssignaments[index] = finalAssignaments[index] if (len(toRemove) > 0): for variableToRemove in toRemove: for rel in relation: if (variableToRemove in rel[1]): rel[1].remove(variableToRemove) for variableToRemove in toRemove: functionsToRemove = variableToRemove.getNeighbour() if variableToRemove in agent.getVariables(): agent.removeNodeVariable(variableToRemove) if variableToRemove in variables: variables.remove(variableToRemove) for otherVar in variables: n1 = set(functionsToRemove) n2 = set(otherVar.getNeighbour()) inter = set(n1).intersection(n2) while (len(inter) > 0): argsOfFunction = list() func = inter.pop() argsOfFunction.append(otherVar) if func in functions: functions.remove(func) if func in agent.getFunctions(): agent.removeNodeFunction(func) otherVar.removeNeighbours(functionsToRemove) nodefunction = NodeFunction(fId) functionevaluator = TabularFunction() nodefunction.setFunction(functionevaluator) otherVar.addNeighbour(nodefunction) nodefunction.addNeighbour(otherVar) costTable = (func.getFunction()).getCostTable() index = (func.getFunction( )).getParameterPosition(variableToRemove) state = (variableToRemove.getStateArgument() ).getValue() for j in range(0, otherVar.size()): t = (j) if index == 0: cost = costTable[(state, j)] else: cost = costTable[(j, state)] nodefunction.getFunction( ).addParametersCost(t, cost) nodefunction.getFunction().setParameters( argsOfFunction) agent.addNodeFunction(nodefunction) functions.append(nodefunction) fId = fId + 1 otherVar.removeNeighbours(functionsToRemove) for fictfun in functionsToRemove: if fictfun.getId() >= 5000: if fictfun in functions: functions.remove(fictfun) if ((len(variables) > 0) & (len(functions) > 0)): agents = list() agents.append(agent) cop = COP_Instance(variables, functions, agents) i = i + varSet if ((len(variables) > 0) & (len(functions) > 0)): ''' create new MaxSum instance (max/min) ''' core = MaxSum(cop, "min", run) ''' update only at end? ''' core.setUpdateOnlyAtEnd(False) core.setIterationsNumber(iterations_bundling) start_time = time.time() ''' invoke the method that executes the MaxSum algorithm ''' print('\n###running solve_complete()\n') core.solve_complete() values = core.getValues() elapse_time = time.time() - start_time print('MaxSum elapse_time:', elapse_time) total_time = total_time + elapse_time going = True if (i == 0): ''' create a copy of cop ''' originalCop = deepcopy(core.getCop()) oldVariables = originalCop.getNodeVariables() for k in range(len(oldVariables)): oldAssignaments[k] = oldVariables[k].getStateIndex() actualValue = calculateActualValue(originalCop, oldAssignaments) averageValues.append(actualValue) iterations.append(len(values)) if ((len(variables) > 0) & (len(functions) == 0)): ''' the variables of the cop ''' variables = core.getCop().getNodeVariables() ''' remaining variables to set ''' index = (nVariables - i) - 1 k = 0 j = 1 while (j < ((index / varSet) + 1)): while (k < (varSet * j)): ''' state of variable i ''' state = (variables[k]).getStateArgument() ''' save the value of the variable ''' finalAssignaments[i] = state.getValue() oldAssignaments[i] = finalAssignaments[i] i = i + k k = k + 1 actualValue = calculateActualValue(originalCop, oldAssignaments) averageValues.append(actualValue) iterations.append(len(values)) j = j + 1 for i in range(len(iterations)): if (i > 0): iterations[i] = iterations[i] + iterations[i - 1] averageValuesInstances.append(averageValues) iterationsInstances.append(iterations) # draw the chart # x axis: number of MaxSum exec # y axis: conflicts x = iterations ''' x axis: number of MaxSum exec ''' y = averageValues pl.title('Cost / Iterations chart') pl.xlabel('Iterations') pl.ylabel('Cost') pl.plot(x, y) pl.savefig(dir_charts + "Chart_RUN_" + str(run) + ".png") pl.close() print('total time for MaxSum ', total_time) sumIterations = [sum(x) for x in zip(*iterationsInstances)] sumValues = [sum(x) for x in zip(*averageValuesInstances)] stdIterations = [numpy.std(x) for x in zip(*iterationsInstances)] stdValues = [numpy.std(x) for x in zip(*averageValuesInstances)] for i in range(len(sumIterations)): sumIterations[i] = sumIterations[i] / nIstances for i in range(len(sumValues)): sumValues[i] = sumValues[i] / nIstances totalIterations = list( itertools.izip_longest(*iterationsInstances, fillvalue=0)) totalConflicts = list( itertools.izip_longest(*averageValuesInstances, fillvalue=0)) sumIterations_correct = [sum(x) / len_not_0(x) for x in totalIterations] sumValues_correct = [sum(x) / len_not_0(x) for x in totalConflicts] stdIterations = list() stdValues = list() for i in range(0, len(totalConflicts)): appoggio = list() if (len_not_0(totalConflicts[i]) < 50): for elem in totalConflicts[i]: if (int(elem) != 0): appoggio.append(elem) stdValues.append(numpy.std(appoggio)) else: stdValues.append(numpy.std(totalConflicts[i])) for i in range(0, len(totalIterations)): appoggio = list() if (len_not_0(totalConflicts[i]) < 50): for elem in totalConflicts[i]: if (int(elem) != 0): appoggio.append(elem) stdIterations.append(numpy.std(appoggio)) else: stdIterations.append(numpy.std(totalIterations[i])) median_iterations = list() median_conflicts = list() number_of_iterations = list( ) #each elements indicate how many runs are still active at that iteration number for x in totalIterations: number_of_iterations.append(len_not_0(x)) finished_runs = list( ) #how many runs are terminated at that iteration number runs = nIstances for x in number_of_iterations: finished_runs.append(runs - int(x)) runs = runs - (runs - int(x)) runs = nIstances how_many_runs = 0 for x in finished_runs: runs = runs - x if runs <= math.floor(nVariables / 2): how_many_runs = x break max_number_of_finished_runs = max(finished_runs) for i in range(0, finished_runs.index(how_many_runs)): median_iterations.append(sumIterations_correct[i]) median_conflicts.append(sumValues_correct[i]) # draw the chart # x axis: number of MaxSum exec # y axis: conflicts x = median_iterations ''' x axis: number of MaxSum exec ''' y = median_conflicts pl.title('Cost / Iterations chart') pl.xlabel('Iterations') pl.ylabel('Cost') pl.plot(x, y) pl.savefig(dir_charts + "/AverageAllInstances.png") pl.close() string = 'Iteration\tStdDevIter\t\t\tConflict\tStdDevConflicts\t\tHowManyStopped\n' for i in range(len(sumIterations_correct)): string = string + str(sumIterations_correct[i]) + '\t\t' + str( stdIterations[i]) + '\t\t' + str( sumValues_correct[i]) + '\t\t' + str( stdValues[i]) + '\t\t' + str( number_of_iterations[i]) + '\n' output_file = open(dir_factorgraph + "/reportIterations.txt", "w") output_file.write(string) output_file.write("\n") output_file.close()
def main(): ''' invoke the parser that takes parameters from the command line ''' args = getParser() ''' How many iterations? ''' nIterations = args.iterations ''' How many instances? ''' nIstances = args.instances ''' number of variables in Dcop ''' nVariables = args.variables ''' max/min ''' op = args.op ''' location of MaxSum report ''' reportMaxSum = args.reportMaxSum ''' create a directory for reports if it isn't exist ''' '''directory = os.path.dirname(reportMaxSum + "Colored_Graph/TestingColoring/10Variabili/") if not os.path.exists(directory): os.mkdir(directory, 777)''' ''' location of FactorGraph report ''' infoGraphPathFile = args.reportFactorGraph ''' create a directory for reports if it isn't exist ''' '''directory = os.path.dirname(reportMaxSum + "Colored_Graph/FactorGraph/10Variabili/") if not os.path.exists(directory): os.mkdir(directory, 777)''' ''' location of Charts ''' chartsFile = args.reportCharts ''' create a directory for charts if it isn't exist ''' ''''directory = os.path.dirname(reportMaxSum + "Colored_Graph/Charts/10Variabili/") if not os.path.exists(directory): os.mkdir(directory,777)''' ''' Constraint Optimization Problem ''' cop = None ''' average of 50 instances of problem ''' average = list() for i in range(nIterations): average.append(0) for run in range(nIstances): ''' fileName of log (Iteration Conflicts AverageDifferenceLink) save on file Iteration Conflicts AverageDifferenceLink ''' fileOutputReport = reportMaxSum + "Experiment/min/Reports/Experiment_Report_RUN_" + str( run) + ".txt" ''' values of MaxSum for each iteration ''' values = list() ''' create a new COP with a colored Graph and save the factorgraph on file ''' cop = create_DCop(infoGraphPathFile, nVariables, run) ''' create new MaxSum instance (max/min) ''' core = MaxSum(cop, op) ''' update only at end? ''' core.setUpdateOnlyAtEnd(False) core.setIterationsNumber(nIterations) start_time = time.time() ''' invoke the method that executes the MaxSum algorithm ''' core.solve_complete() elapse_time = time.time() - start_time print('MaxSum elapse_time:', elapse_time) ''' values of MaxSum for each iteration in this instance ''' values = core.getValues() final = "\tITERATION\tCOST\n" for i in range(len(values)): final = final + "\t" + str(i) + "\t\t" + str(values[i]) + "\n" average[i] = average[i] + values[i] ''' save on file the log file ''' core.stringToFile(final, fileOutputReport) # draw the chart # x axis: number of iterations # y axis: max-cost found x = list() ''' x axis: iterations ''' for i in range(nIterations): x.append(i) #pl.xticks([20 * k for k in range(0, 16)]) y = values pl.title('Cost / Iteration chart') pl.xlabel('Iterations') pl.ylabel('Cost') pl.plot(x, y) #pl.show() pl.savefig(chartsFile + "Experiment/min/Charts/Chart_RUN_" + str(run) + ".png") pl.close() for i in range(nIterations): average[i] = average[i] / nIstances # draw the chart # x axis: number of iterations # y axis: average of costs x = list() ''' x axis: iterations ''' for i in range(nIterations): x.append(i) #pl.xticks([20 * k for k in range(0, 16)]) y = average pl.title('Average Cost / Iteration chart') pl.xlabel('Iterations') pl.ylabel('Cost') pl.plot(x, average) #pl.show() pl.savefig(chartsFile + "Experiment/min/Charts/AverageAllCharts.png") pl.close() string = 'Iteration\tConflict\n' for k in range(nIterations): string = string + str(k) + '\t\t' + str(average[k]) + '\n' output_file = open( infoGraphPathFile + "Experiment/min/Reports/reportAverageAllInstances.txt", "w") output_file.write(string) output_file.write("\n") output_file.close()