linopy.model.Model#
- class linopy.model.Model(solver_dir=None, chunk=None, force_dim_names=False, auto_mask=False, freeze_constraints=False, set_names_in_solver_io=True)#
Linear optimization model.
The Model contains all relevant data of a linear program, including
variables with lower and upper bounds
constraints with left hand side (lhs) being a linear expression of variables and a right hand side (rhs) being a constant. Lhs and rhs are set in relation by the sign
objective being a linear expression of variables
The model supports different solvers (see linopy.available_solvers) for the optimization process.
- __init__(solver_dir=None, chunk=None, force_dim_names=False, auto_mask=False, freeze_constraints=False, set_names_in_solver_io=True)#
Initialize the linopy model.
- Parameters:
solver_dir (
pathlike, optional) – Path where temporary files like the lp file or solution file should be stored. The default None results in taking the default temporary directory.chunk (
int, optional) – Chunksize used when assigning data, this can speed up large programs while keeping memory-usage low. The default is None.force_dim_names (
bool) – Whether assigned variables, constraints and data should always have custom dimension names, i.e. not matching dimension names “dim_0”, “dim_1” and so on. These helps to avoid unintended broadcasting over dimension. Especially the use of pandas DataFrames and Series may become safer.auto_mask (
bool) – Whether to automatically mask variables and constraints where bounds, coefficients, or RHS values contain NaN. The default is False.freeze_constraints (
bool) – Whether constraints added to the model should be frozen to the CSR-backed representation by default. The default is False.set_names_in_solver_io (
bool) – Whether direct solver exports should include variable and constraint names by default. The default is True.
- Returns:
linopy.Model
Methods
__init__([solver_dir, chunk, ...])Initialize the linopy model.
add_constraints(lhs[, sign, rhs, name, ...])Assign a new, possibly multi-dimensional array of constraints to the model.
add_objective(expr[, overwrite, sense])Add an objective function to the model.
add_piecewise_formulation(*pairs[, method, ...])Add piecewise linear constraints.
add_sos_constraints(variable, sos_type, sos_dim)Add an sos1 or sos2 constraint for one dimension of a variable
add_variables([lower, upper, coords, name, ...])Assign a new, possibly multi-dimensional array of variables to the model.
Reformulate SOS constraints into binary + linear form, in place.
assign_result(result[, solver])Write a solver Result back onto the model.
calculate_block_maps()Calculate the matrix block mappings based on dimensional blocks.
check_force_dim_names(ds)Ensure that the added data does not lead to unintended broadcasting.
Compute a set of infeasible constraints.
compute_set_of_infeasible_constraints()Compute a set of infeasible constraints.
copy([include_solution, deep])Return a copy of this model.
format_infeasibilities([display_max_terms])Return a string representation of infeasible constraints.
get_problem_file([io_api])Get a fresh created problem file if problem file is None.
get_solution_file()Get a fresh created solution file if solution file is None.
linexpr(*args)Create a linopy.LinearExpression from argument list.
print_infeasibilities([display_max_terms])Print a list of infeasible constraints.
reformulate_sos_constraints([prefix])Reformulate SOS constraints as binary + linear constraints.
remove_constraints(name)Remove all constraints stored under reference name 'name' from the model.
Remove the objective's linear expression from the model.
remove_sos_constraints(variable)Remove all sos constraints from a given variable.
remove_variables(name)Remove all variables stored under reference name name from the model.
reset_solution()Reset the solution and dual values if available of the model.
solve([solver_name, io_api, ...])Solve the model with possibly different solvers.
to_block_files(fn)Write out the linopy model to a block structured output.
to_cupdlpx()Build the cupdlpx.Model for m.
to_file(fn[, io_api, integer_label, ...])Write out a model to a lp or mps file.
to_gurobipy([env, ...])Build the gurobipy.Model for m.
to_highspy([explicit_coordinate_names, ...])Build the highspy.Highs instance for m.
to_mosek([task, explicit_coordinate_names, ...])Build the MOSEK task for m.
to_netcdf(*args, **kwargs)Write out the model to a netcdf file.
to_xpress([explicit_coordinate_names, set_names])Build the xpress.problem instance for m.
Revert a previously applied SOS reformulation.
Attributes
auto_maskIf True, automatically mask variables and constraints where bounds, coefficients, or RHS values contain NaN.
binariesGet all binary variables.
blocksBlocks used as a basis to split the variables and constraint matrix.
chunkChunk sizes of the model.
coefficientrangeCoefficient range of the constraints in the model.
Constraints assigned to the model.
continuousGet all continuous variables.
dataset_attrsDual values calculated by the optimization.
force_dim_namesWhether assigned variables, constraints and data should always have custom dimension names, i.e. not matching dimension names "dim_0", "dim_1" and so on.
freeze_constraintsWhether constraints are frozen to CSR by default when added.
integersGet all integer variables.
Whether the objective is linear.
Whether the objective is quadratic.
matricesMatrix representation of the model, computed fresh on each access.
nconsGet the total number of constraints.
nvarsGet the total number of variables.
Objective assigned to the model.
objectiverangeObjective range of the objective in the model.
parametersParameters assigned to the model.
scalar_attrssemi_continuousGet all semi-continuous variables.
Sense of the objective function.
set_names_in_solver_ioWhether direct solver exports include names by default.
shapeGet the shape of the non-filtered constraint matrix.
Solution calculated by the optimization.
solversolver_dirSolver directory of the model.
solver_modelsolver_nameStatus of the model.
Termination condition of the model.
Short string identifying the problem type.
Variables assigned to the model.