linopy.model.Model#
- class linopy.model.Model(solver_dir=None, chunk=None, force_dim_names=False)#
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)#
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.
- Returns:
linopy.Model
Methods
__init__
([solver_dir, chunk, force_dim_names])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])Add a linear objective function to the model.
add_variables
([lower, upper, coords, name, ...])Assign a new, possibly multi-dimensional array of variables to 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_infeasibilities
()Compute a set of infeasible constraints.
compute_set_of_infeasible_constraints
()Compute a set of infeasible constraints.
get_problem_file
([problem_fn, io_api])Get a fresh created problem file if problem file is None.
get_solution_file
([solution_fn])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.
remove_constraints
(name)Remove all constraints stored under reference name name from the model.
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, problem_fn, ...])Solve the model with possibly different solvers.
to_block_files
(fn)Write out the linopy model to a block structured output.
to_file
(fn)Write out a model to a lp or mps file.
to_gurobipy
([env])Export the model to gurobipy.
to_highspy
()Export the model to highspy.
to_netcdf
(*args, **kwargs)Write out the model to a netcdf file.
Attributes
solver_model
matrices
binaries
Get all binary variables.
blocks
Blocks used as a basis to split the variables and constraint matrix.
chunk
Chunk sizes of the model.
coefficientrange
Coefficient range of the constraints in the model.
constraints
Constraints assigned to the model.
continuous
Get all continuous variables.
dataset_attrs
dual
Dual values calculated by the optimization.
force_dim_names
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.
integers
Get all integer variables.
is_linear
is_quadratic
ncons
Get the total number of constraints.
nvars
Get the total number of variables.
objective
Objective assigned to the model.
objective_value
Objective value of the model.
objectiverange
Objective range of the objective in the model.
parameters
Parameters assigned to the model.
scalar_attrs
shape
Get the shape of the non-filtered constraint matrix.
solution
Solution calculated by the optimization.
solver_dir
Solver directory of the model.
status
Status of the model.
termination_condition
Termination condition of the model.
type
variables
Variables assigned to the model.