Beispiel #1
0
    def mip_solver(self):
        
        try:
            
            # create a new model
            m = Model("mip_encode")
            m.params.outputflag = 0
            #m.params.feasibilitytol = 1e-9
            #m.params.intfeastol = 1e-9
            # add variables
            self.add_vars(m)
            self.add_additional_vars(m)
            
            # integrate new variables
            m.update()
            # print("add constr")

            # add constraints
            self.add_constrs(m)
            self.add_spc(m)
            if self.objective_type == ObjectiveType.MAX_FLEX_UNCERTAINTY:
                m.setObjective(self.Z + 0.0, GRB.MAXIMIZE)
            else:
                m.setObjective(self.objexp, GRB.MAXIMIZE)
            m.update()
            m.write("1.lp")
            m.optimize()
            
            if m.status == GRB.Status.INF_OR_UNBD:
                m.setParam(GRB.Param.Presolve, 0)
                m.optimize()
            
            if m.status == GRB.Status.OPTIMAL:
                m.write("1.sol")
                m.fixed()
                return self.get_solution(m)
            if m.status != GRB.Status.INFEASIBLE:
                print(m.status)
            m.computeIIS()
            m.write("1.ilp")
            
        except GurobiError as e:
            print('Error reported')
            print (e.message)
            print (e.errno)
        solution = Candidate()
        solution.utility = -1;
        return solution
Beispiel #2
0
    def initialize(self):
        # clear the search state
        self.queue = PriorityQueue()
        self.known_conflicts = set()

        # make any unconditional temporal constraints
        # active
        for constraint in self.tpnu.temporal_constraints.values():
            if len(constraint.guards) == 0:
                constraint.activated = True
            else:
                constraint.activated = False

        # add an empty candidate to the queue
        first_candidate = Candidate()

        # make any unconditional decision variables
        # available for assignment
        for variable in self.tpnu.decision_variables.values():
            if len(variable.guards) == 0:
                first_candidate.unassigned_variables.put(variable)

        self.queue.put(first_candidate)

        # initialize the heuristic for each decision variable
        for dv in self.tpnu.decision_variables.values():
            self.update_variable_heuristics(dv)
Beispiel #3
0
    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     
Beispiel #4
0
    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
Beispiel #5
0
    def create_child_candidate_from_assignment(self,candidate,assignment):

        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)
        if not new_candidate.add_assignment(assignment):
            return None
        new_candidate.add_temporal_relaxations(candidate.temporal_relaxations)
        new_candidate.add_semantic_relaxations(candidate.semantic_relaxations)

        # with this new assignment, find all available/unassigned 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
Beispiel #6
0
    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
Beispiel #7
0
    def create_child_candidate_from_assignment(self, candidate, assignment):

        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)
        if not new_candidate.add_assignment(assignment):
            return None
        new_candidate.add_temporal_relaxations(candidate.temporal_relaxations)
        new_candidate.add_semantic_relaxations(candidate.semantic_relaxations)

        # with this new assignment, find all available/unassigned 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
Beispiel #8
0
            a, b = (0 - mean) / sigma, (1e6 - mean) / sigma

            # For the lower bound, the derivative is the Gaussian pdf
            G[2 * idx] = truncnorm.pdf(x[lb_var], a, b, loc=mean, scale=sigma)

            # For the upper bound, it is the negation of the Gaussian pdf
            G[2 * idx +
              1] = -1 * truncnorm.pdf(x[ub_var], a, b, loc=mean, scale=sigma)

            # print('Updating G['+str(2 * idx)+']: ' + str(G[2 * idx]))
            # print('Updating G['+str(2 * idx+1)+']: ' + str(G[2 * idx + 1]))


if __name__ == "__main__":

    candidate = Candidate()
    ev1 = 1
    ev2 = 2
    ev3 = 3

    ep1 = TemporalConstraint('ep-1', 'ep-1', ev1, ev2, 15, 30)
    ep2 = TemporalConstraint('ep-2', 'ep-2', ev2, ev3, 15, 30)
    ep3 = TemporalConstraint('ep-3', 'ep-3', ev1, ev3, 40, 40)
    ep1.controllable = False
    ep1.probabilistic = True
    ep1.mean = 150
    ep1.std = 5

    ep2.controllable = False
    ep2.probabilistic = True
    ep2.mean = 150