示例#1
0
    def get_optimal_package_hook(self, *args, **kwargs):
        # Settings
        cplex_feasopt = kwargs.get("cplex_feasopt", False)
        cplex_conflict_refine = kwargs.get("cplex_conflict_refine", False)
        # They can't both be set to True
        assert cplex_feasopt is False or cplex_conflict_refine is False

        self.ilp_problem.cplex_feasopt_active = cplex_feasopt
        self.ilp_problem.cplex_conflict_refiner_active = cplex_conflict_refine

        if self.N == 0:
            # If there's no tuple in the base table, the only candidate is the empty package
            empty_package = SequenceBasedPackage(self, tuple())
            if empty_package.is_valid():
                return empty_package
            else:
                raise InfeasiblePackageQuery

        # Solve ILP problem
        verbose_log("Solving ILP problem...")
        self.current_run_info.problem_solving_start()
        optimal_package, cplex_runinfo = self.ilp_problem.get_optimal_package(**kwargs)
        self.current_run_info.problem_solving_end()
        verbose_log("ILP problem solved.")

        # Add running info about ILP solver to the specific running info
        assert isinstance(cplex_runinfo, CPLEXRunInfo)
        self.current_run_info.CPLEX_run_info = cplex_runinfo

        return optimal_package
示例#2
0
    def setup_problem_parameters(self):
        verbose_log("Setting CPLEX problem params")

        # Set time limit for solver
        timelimit_sec = self.search.get_remaining_time()
        if timelimit_sec is not None:
            self.problem.parameters.timelimit.set(timelimit_sec)

        # Set CPLEX parameters
        set_cplex_parameters(self.problem)
示例#3
0
    def init(self, query, store_lp_problems_dir=None, **kwargs):
        """
        Ovverrides init_search of Search class to allow storing LP problems.
        """
        super(ILPDirect, self).init(query, **kwargs)

        # Settings
        ilp_solver_interface = kwargs.get("ilp_solver_interface", ILPPackageProblemCplexPy)
        assert inspect.isclass(ilp_solver_interface) and issubclass(ilp_solver_interface, ILPPackageProblem)
        problem_type = kwargs.get("problem_type", "lp")


        self.ilp_problem = ilp_solver_interface(
            search=self,
            store_all_solved_problems=self.store_all_solved_problems)
        assert isinstance(self.ilp_problem, ILPPackageProblem)

        verbose_log("Loading ILP problem...")
        self.current_init_info.problem_loading_start()
        self.ilp_problem.load_ilp_from_paql(problem_type)
        self.current_init_info.problem_loading_end()
        verbose_log("ILP problem loaded.")

        # Store ILP to a file (if needed)
        if store_lp_problems_dir is not None:
            verbose_log("Saving ILP problem into folder '{}'...".format(store_lp_problems_dir))
            self.current_init_info.storing_problem_start()
            self.ilp_problem.store_linear_problem_string(store_lp_problems_dir)
            self.current_init_info.storing_problem_end()
            verbose_log("ILP problem saved.")
示例#4
0
    def solve(self, feas_opt=False, conflict_refine=False):
        if self.store_all_solved_problems:
            self.store_linear_problem_string("/tmp")

        # Initialize running time counters
        cplex_waltime = -self.problem.get_time()
        cplex_dettime = -self.problem.get_dettime()
        wallclock_time = -time.time()
        cputicks_time = -time.clock()

        # Solve the problem with CPLEX
        if not feas_opt and not conflict_refine:
            # Just use the standard CPLEX solve() procedure
            verbose_log("Running CPLEX solve()...")
            self.problem.solve()

        # Solve with CPLEX Feasopt
        elif feas_opt:
            # Mode 0 (default): Minimize the sum of all required relaxations in first phase only
            # Mode 1: Minimize the sum of all required relaxations in first phase and execute second
            #         phase to find optimum among minimal relaxations
            # Mode 2: Minimize the number of constraints and bounds requiring relaxation in first phase only
            # Mode 3: Minimize the number of constraints and bounds requiring relaxation in first phase and
            #         execute second phase to find optimum among minimal relaxations
            # Mode 4: Minimize the sum of squares of required relaxations in first phase only
            # Mode 5: Minimize the sum of squares of required relaxations in first phase and execute second
            #         phase to find optimum among minimal relaxations
            self.problem.parameters.feasopt.mode.set(0)
            verbose_log("Running CPLEX feasopt(...)...")
            self.problem.feasopt(self.problem.feasopt.linear_constraints())

        # Solve after removing constraints with Conflict Refine
        elif conflict_refine:
            self.remove_conflicting_global_constraints()
            verbose_log("Running CPLEX solve()...")
            self.problem.solve()

        self.n_solver_solve_calls += 1

        # Update running time counters
        cplex_waltime += self.problem.get_time()
        cplex_dettime += self.problem.get_dettime()
        wallclock_time += time.time()
        cputicks_time += time.clock()

        cplex_run_info = CPLEXRunInfo(
            cplex_problem_size=self.problem.variables.get_num(),
            cplex_n_problem_linear_constraints=self.problem.linear_constraints.
            get_num(),
            cplex_wallclock_time=cplex_waltime,
            cplex_detticks_time=cplex_dettime,
            sys_wallclock_time=wallclock_time,
            sys_cputicks_time=cputicks_time,
            wallclock_time_to_store=None,
            cputicks_time_to_store=None,
            cplex_feas_opt_used=feas_opt,
            cplex_conflict_refiner_used=self.conflicting_linear_constraints
            is not None,
            cplex_status=self.problem.solution.get_status())

        return cplex_run_info
示例#5
0
    def compute_conflicting_linear_constraints(self):
        verbose_log("refining...")
        self.problem.conflict.refine(
            self.problem.conflict.linear_constraints())
        verbose_log("done.")

        print "UGC's:"
        for i, ugc in enumerate(self.search.query.uncoalesced_gcs):
            print i, "GC:", str_u_gc(ugc)

        if self.problem.solution.get_status(
        ) == cplex.Cplex.solution.status.conflict_minimal:

            conflict_statuses = self.problem.conflict.get()
            verbose_log("CONFLICT STATUSES:", conflict_statuses)

            selected_group_ids = [
                i for i in xrange(len(conflict_statuses))
                if conflict_statuses[i] == self.problem.conflict.group_status.
                member or conflict_statuses[i] ==
                self.problem.conflict.group_status.possible_member
            ]
            verbose_log("SELECTED GROUP IDS:", selected_group_ids)

            conflict_groups = self.problem.conflict.get_groups(
                selected_group_ids)
            verbose_log("CONFLICT GROUPS:", conflict_groups)

            self.conflicting_linear_constraints = list()
            print "INCONSISTENT GCs:"
            for preference, constrs in conflict_groups:
                # The preference for now is unused (it is possible to assign a weight to each constraint)
                assert preference == 1.0
                for constr_type, constr_id in constrs:
                    verbose_log("CONSTR ID:", constr_id)
                    if constr_type == cplex.Cplex.conflict.constraint_type.linear:
                        verbose_log(
                            "LINEAR RHS:",
                            self.problem.linear_constraints.get_rhs(constr_id))

                        ugc = self.search.query.uncoalesced_gcs[constr_id]
                        print constr_id, str_u_gc(ugc)

                        linear_constraint = filter(
                            lambda x: x.cid == constr_id,
                            self.linear_constraints)[0]

                        self.conflicting_linear_constraints.append(
                            linear_constraint)
                    elif constr_type == cplex.Cplex.conflict.constraint_type.upper_bound:
                        raise Exception
                    else:
                        raise Exception

            if len(self.conflicting_linear_constraints) == 0:
                raise Exception(
                    "Problem was infeasible but didn't find any problematic linear constraint."
                )

            verbose_log("minimial_infeasible_constrs:",
                        self.conflicting_linear_constraints)

        else:
            raise Exception("Cplex status unsupported: {}".format(
                self.problem.solution.get_status()))