def create_child_candidate_from_relaxations(self, candidate, relaxations=None, allocations=None): new_candidate = Candidate() new_candidate.resolved_conflicts = candidate.resolved_conflicts.copy() new_candidate.continuously_resolved_cycles = candidate.continuously_resolved_cycles.copy( ) new_candidate.assignments_to_avoid = candidate.assignments_to_avoid.copy( ) new_candidate.assigned_variables = candidate.assigned_variables.copy() new_candidate.add_assignments(candidate.assignments) # We do not need the following line as it adds duplicated relaxations to the # new candidate. # new_candidate.add_temporal_relaxations(candidate.temporal_relaxations) # Add temporal relaxations if relaxations is not None: for relaxation in relaxations: if not new_candidate.add_temporal_relaxation(relaxation): return None # Add temporal allocations if allocations is not None: for allocation in allocations: if not new_candidate.add_temporal_allocation(allocation): return None new_candidate.add_semantic_relaxations(candidate.semantic_relaxations) # find all available variables for variable in self.tpnu.decision_variables.values(): # it must not have been assigned if not variable in new_candidate.assigned_variables: # and the guard must have been satisfied if len(variable.guards ) == 0 or variable.guards <= new_candidate.assignments: new_candidate.unassigned_variables.put(variable) new_candidate.h += variable.optimal_utility return new_candidate
def create_child_candidate_from_relaxations(self,candidate,relaxations=None,allocations=None): new_candidate = Candidate() new_candidate.resolved_conflicts = candidate.resolved_conflicts.copy() new_candidate.continuously_resolved_cycles = candidate.continuously_resolved_cycles.copy() new_candidate.assignments_to_avoid = candidate.assignments_to_avoid.copy() new_candidate.assigned_variables = candidate.assigned_variables.copy() new_candidate.add_assignments(candidate.assignments) # We do not need the following line as it adds duplicated relaxations to the # new candidate. # new_candidate.add_temporal_relaxations(candidate.temporal_relaxations) # Add temporal relaxations if relaxations is not None: for relaxation in relaxations: if not new_candidate.add_temporal_relaxation(relaxation): return None # Add temporal allocations if allocations is not None: for allocation in allocations: if not new_candidate.add_temporal_allocation(allocation): return None new_candidate.add_semantic_relaxations(candidate.semantic_relaxations) # find all available variables for variable in self.tpnu.decision_variables.values(): # it must not have been assigned if not variable in new_candidate.assigned_variables: # and the guard must have been satisfied if len(variable.guards) == 0 or variable.guards <= new_candidate.assignments: new_candidate.unassigned_variables.put(variable) new_candidate.h += variable.optimal_utility return new_candidate
def get_solution(self, m): solution = Candidate() # f = open('sol.txt', 'w') for var_a in m.getVars(): vname = var_a.getAttr(GRB.Attr.VarName) if self.objective_type == ObjectiveType.MAX_FLEX_UNCERTAINTY: if vname.find("cl") == -1 and vname.find("cu") == -1: continue #f.write('%s %s\n' % (var_a.getAttr(GRB.Attr.VarName), var_a.getAttr(GRB.Attr.X))) # if vname.find("l") != -1: # print(var_a.getAttr(GRB.Attr.VarName), var_a.getAttr(GRB.Attr.X)) e = self.relax_e[vname] new_relaxation = TemporalRelaxation(e) if vname.find("cl") != -1: new_relaxation.relaxed_lb = var_a.getAttr("X") if new_relaxation.relaxed_lb != e.get_lower_bound(): solution.add_temporal_relaxation(new_relaxation) else : new_relaxation.relaxed_ub = var_a.getAttr("X") if new_relaxation.relaxed_ub != e.get_upper_bound(): solution.add_temporal_relaxation(new_relaxation) elif self.objective_type == ObjectiveType.MIN_COST: if vname.find("vlr") == -1 and vname.find("vur") == -1: continue; e = self.relax_e[vname] #print(vname,e.fro, e.to) new_relaxation = TemporalRelaxation(e) if vname.find("vlr") != -1: new_relaxation.relaxed_lb = self.l[(e.fro,e.to)].getValue() else: new_relaxation.relaxed_ub = self.u[(e.fro, e.to)].getValue() if var_a.getAttr("X") > 0: solution.add_temporal_relaxation(new_relaxation) # for e in self.network.temporal_constraints.values(): # if e.activated: # print(e.fro, e.to, self.l[(e.fro, e.to)].getValue(), self.u[(e.fro, e.to)].getValue()) solution.utility = round(m.getObjective().getValue(), 6) if solution.utility > 1000 and self.objective_type == ObjectiveType.MAX_FLEX_UNCERTAINTY: solution.utility = 1000 # for node_a in range(1, self.num_nodes+1): # for node_b in range(1, self.num_nodes+1): # if node_a != node_b: # f.write("%s->%s [%s, %s]\n"%(node_a,node_b,self.l[(node_a,node_b)].getValue(), self.u[(node_a,node_b)].getValue() )) # # if solution.utility > 100: # solution.utility = 100 # f.close() #print a dot file # f = open('sol.dot', 'w') # f.write('digraph G {\n nodesep = .45; \n size = 30;\n label="CCTP";\n') # # for e in self.network.temporal_constraints.values(): # if e.activated: # f.write('"%s"->"%s"'%(e.fro, e.to)) # tlb = self.l[(e.fro, e.to)].getValue() # tub = self.u[(e.fro, e.to)].getValue() # f.write('[ label = "[%s,%s]'%(tlb, tub)) # if not e.controllable: # f.write(' type=dashed') # f.write('"];\n') # #%d"];'%(e.fro, e.to,e.get_lower_bound(),e.get_upper_bound(),e.controllable)) # f.write("}") # f.close() return solution