Exemple #1
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)
    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):
        adapter = cls.create_cplex_adapter()
        cpx = adapter.cpx
        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
            adapter.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]
            adapter.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)