pulp_solver#

Initialize the optimization problem instance and solve it.

This module initializes the optimization problem instance and solves it using the PulpSolver class.

class wip.modules.pulp_solver.PulpSolver(path_to_constraints: str | Path, path_to_costs: str | Path, solver_option: str = 'cbc')[source]#

Bases: object

Class that defines and solves the optimization problem.

This class supports multiple solvers, that can be used to find the optimal solution to the problem. The attribute solver_option contains a dictionary, with the following allowed solvers:

  • 'cbc'

  • 'gurobi'

  • 'glpk'

  • 'cplex'

The default solver is 'cbc'

Methods

create_lp_file(tmp_path)

Write the optimization problem instances to .lp files.

export_results()

Export the optimization results to a CSV file.

get_probs()

Retrieve the optimization problem instances.

solve_range([ranges, tmp_path, scalers, ...])

Solve the optimization problems for each production range.

Parameters
  • path_to_constraints (str | Path) –

  • path_to_costs (str | Path) –

  • solver_option (str) –

__init__(path_to_constraints: str | Path, path_to_costs: str | Path, solver_option: str = 'cbc')[source]#

Set the initial parameters for the optimization problem.

Parameters
  • path_to_constraints (str | Path) – The path to the folder containing the constraints’ files.

  • path_to_costs (str | Path) – The path to the file containing the costs for each variable.

  • solver_option (str {'cbc', 'gurobi', 'glpk', 'cplex'}, default "cbc") – The solver to use to solve the optimization problem.

_check_variable_bounds(key: str, feature: str)[source]#

Check if a variable has min and max bounds, if not, set them to 0 and 1.

Parameters
  • key (str) – Production range. Possible values are: ‘750-800’, ‘800-850’, ‘850-900’, ‘900-950’, ‘950-1000’

  • feature (str) – Name of the variable name to check bounds.

_extract_key_name(file_path)[source]#

Extract key name from a file path

Parameters

file_path (str) – File path to extract key name from

Returns

Key name extracted from the file path

Return type

str

Examples

>>> _extract_key_name('C:/Users/username/Desktop/2020-01-01_2020-01-31_restricoes_01.csv')
'2020-01'
>>> probs._extract_key_name('C:/Users/username/Desktop_1-10')
'1-10'
_remove_non_ascii_normalized(string: str) str[source]#

Remove non-ascii characters from string

Parameters

string (str) –

Return type

str

_sub_specific_characters(sentence)[source]#

Replace specific characters from sentence

Method replaces “*”, “=”, “/” by “mult”, “equal”, “div” respectively

Parameters

sentence (str) – Sentence to be fixed

Returns

Fixed sentence, with math operations replaced by their literal abbreviations

Return type

str

create_lp_file(tmp_path: str)[source]#

Write the optimization problem instances to .lp files.

Parameters

tmp_path (str) – The path where the optimization problem instances should be saved to.

export_results()[source]#

Export the optimization results to a CSV file.

Method removes any previously generated results, prior to saving the optimization results. The results to each production range optimization problem are saved to the PulpSolver.path_to_constraints folder using the following name pattern: 'Variables - VarX_<PRODUCTION_RANGE>.csv', where '<PRODUCTION_RANGE>' is one of the following values:

  • '700-750'

  • '750-800'

  • '800-850'

  • '850-900'

  • '900-950'

  • '950-1000'

get_probs() Dict[str, LpProblem][source]#

Retrieve the optimization problem instances.

Returns

A dictionary containing the optimization problem instances.

Return type

Dict[str, pulp.LpProblem]

solve_range(ranges=None, tmp_path='/dbfs/tmp/us_not_defined', scalers=None, datasets=None, df_sql=None, save_relaxed_prob: bool = False, **kwargs)[source]#

Solve the optimization problems for each production range.

This method iterates over every production range, defines additional constraints that to be defined, requires the variables and optimization problems to be already defined, and solves the problem using the specified solver.

Parameters
  • ranges (list, optional) –

    A list of production ranges to solve. If not specified, all ranges are solved. Acceptable values are:

    • '700-750'

    • '750-800'

    • '800-850'

    • '850-900'

    • '900-950'

    • '950-1000'

  • tmp_path (str, default '/dbfs/tmp/us_not_defined') – File path where the results should be saved to.

  • scalers (Dict[str, sklearn.preprocessing.MinMaxScaler], default {}) – A dictionary containing the scalers for each variable.

  • datasets (Dict[str, pd.DataFrame], default {}) – A dictionary containing the datasets for each ridge regression model used to create the optimization model.

  • save_relaxed_prob (bool, default False) – If one of the optimization problem instances is found to be infeasible, find the constraints that are causing the infeasibility and save the relaxed problem instead of the original problem.

  • kwargs (Any) – Additional keyword arguments.

solvers = {'cbc': <pulp.apis.coin_api.PULP_CBC_CMD object>, 'cplex': <pulp.apis.cplex_api.CPLEX_CMD object>, 'glpk': <pulp.apis.glpk_api.GLPK_CMD object>, 'gurobi': <pulp.apis.gurobi_api.GUROBI object>}#