Example #1
0
    def build_sparse_linear_model_and_solve(cls, nb_vars, var_lbs, var_ubs,
                                            var_types, var_names,
                                            nb_rows, cts_sparse_coefs,
                                            objsense, costs,
                                            solution_maker=make_solution,
                                            **transform_params):
        cpx = cls.create_cplex()
        # varlist = mdl.continuous_var_list(var_count, lb=var_lbs, ub=var_ubs, name=var_names)
        cls.create_column_vars(cpx, nb_vars, var_lbs, var_ubs, var_types, var_names)
        var_indices = list(range(nb_vars))
        cpx_linexprs = [([], []) for _ in range(nb_rows)]
        cpx_rhss = [0.] * nb_rows
        for coef, row, col in cts_sparse_coefs:
            if col >= nb_vars:
                cpx_rhss[row] = float(coef)
            elif coef:
                cpx_row = cpx_linexprs[row]
                #  int() conversio nis mandatory here
                # as sparse matrices contain numpy int types -> cause cplex to crash
                cpx_row[0].append(int(col))
                cpx_row[1].append(float(coef))
        ctsense = ComparisonType.parse(transform_params.get('sense', 'le'))
        cpx_senses = ctsense.cplex_code * nb_rows
        #fast_add_linear(cpx, cpx_linexprs, cpx_senses, cpx_rhss, names=[])
        cpx.linear_constraints.add(cpx_linexprs, cpx_senses, cpx_rhss, names=[])

        if costs is not None:
            # set linear objective for all variables.
            fcosts = [float(k) for k in costs]
            static_fast_set_linear_obj(cpx, var_indices, fcosts)
            cpx.objective.set_sense(objsense.cplex_coef)
        # here we go to solve...
        return cls._solve(cpx, var_names, solution_maker=solution_maker, **transform_params)
Example #2
0
 def build_matrix_range_model_and_solve(cls, var_count, var_lbs, var_ubs,
                                        var_types, var_names,
                                        cts_mat, range_mins, range_maxs,
                                        objsense, costs,
                                        cast_to_float,
                                        solution_maker=make_solution,
                                        **transform_params):
     cpx = cls.create_cplex()
     # varlist = mdl.continuous_var_list(var_count, lb=var_lbs, ub=var_ubs, name=var_names)
     cls.create_column_vars(cpx, var_count, var_lbs, var_ubs, var_types, var_names)
     var_indices = list(range(var_count))
     gen_rows = ModelAggregator.generate_rows(cts_mat)
     cpx_rows = []
     for row in gen_rows:
         # need this step as cplex may crash with np types.
         frow = [float(k) for k in row] if cast_to_float else row
         cpx_rows.append([var_indices, frow])
     nb_rows = len(cpx_rows)
     if nb_rows:
         #ctsense = ComparisonType.parse(transform_params.get('sense', 'le'))
         cpx_senses = 'R' * nb_rows
         cpx_ranges = [float(rmin - rmax) for rmin, rmax in izip(range_mins, range_maxs)]
         # rhs is UB
         cpx_rhss = [float(rmax) for rmax in range_maxs]
         add_linear(cpx, cpx_rows, cpx_senses, rhs=cpx_rhss, names=[], ranges=cpx_ranges)
     if costs is not None:
         # set linear objective for all variables.
         fcosts = [float(k) for k in costs]
         static_fast_set_linear_obj(cpx, var_indices, fcosts)
         cpx.objective.set_sense(objsense.cplex_coef)
     # here we go to solve...
     return cls._solve(cpx, var_names, solution_maker=solution_maker, **transform_params)
Example #3
0
    def build_matrix_linear_model_and_solve(cls,
                                            var_count,
                                            var_lbs,
                                            var_ubs,
                                            var_types,
                                            var_names,
                                            cts_mat,
                                            rhs,
                                            objsense,
                                            costs,
                                            cast_to_float,
                                            solution_maker=make_solution,
                                            **transform_params):
        cpx = cls.create_cplex()
        if cast_to_float:
            print("-- all numbers will be cast to float")
        else:
            print("-- no cast to float is performed")
        cls.create_column_vars(cpx, var_count, var_lbs, var_ubs, var_types,
                               var_names)
        var_indices = list(range(var_count))
        gen_rows = ModelAggregator.generate_rows(cts_mat)
        cpx_rows = []
        if cast_to_float:
            for row in gen_rows:
                # need this step as cplex may crash with np types.
                frow = [float(k) for k in row]
                cpx_rows.append([var_indices, frow])
        else:
            cpx_rows = [[var_indices, row] for row in gen_rows]

        nb_rows = len(cpx_rows)
        if nb_rows:
            ctsense = ComparisonType.parse(transform_params.get('sense', 'le'))
            cpx_senses = ctsense.cplex_code * nb_rows
            cpx_rhss = [float(r) for r in rhs] if cast_to_float else rhs
            fast_add_linear(cpx, cpx_rows, cpx_senses, cpx_rhss, names=[])
        if costs is not None:
            # set linear objective for all variables.
            fcosts = [float(k) for k in costs]
            static_fast_set_linear_obj(cpx, var_indices, fcosts)
            cpx.objective.set_sense(objsense.cplex_coef)
        # here we go to solve...
        return cls._solve(cpx,
                          var_names,
                          solution_maker=solution_maker,
                          **transform_params)