linopy.model.Model#
- class linopy.model.Model(solver_dir=None, chunk=None, force_dim_names=False, auto_mask=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, auto_mask=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.auto_mask (
bool) – Whether to automatically mask variables and constraints where bounds, coefficients, or RHS values contain NaN. The default is False.
- 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[, sign, ...])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.
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.
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 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_objective()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([explicit_coordinate_names])Export the model to cupdlpx.
to_file(fn[, io_api, integer_label, ...])Write out a model to a lp or mps file.
to_gurobipy([env, explicit_coordinate_names])Export the model to gurobipy.
to_highspy([explicit_coordinate_names])Export the model to highspy.
to_mosek([task, explicit_coordinate_names])Export model to MOSEK.
to_netcdf(*args, **kwargs)Write out the model to a netcdf file.
Attributes
solver_modelsolver_namematricesauto_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.
constraintsConstraints assigned to the model.
continuousGet all continuous variables.
dataset_attrsdualDual 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.
integersGet all integer variables.
is_linearis_quadraticnconsGet the total number of constraints.
nvarsGet the total number of variables.
objectiveObjective assigned to the model.
objectiverangeObjective range of the objective in the model.
parametersParameters assigned to the model.
scalar_attrssemi_continuousGet all semi-continuous variables.
senseSense of the objective function.
shapeGet the shape of the non-filtered constraint matrix.
solutionSolution calculated by the optimization.
solver_dirSolver directory of the model.
statusStatus of the model.
termination_conditionTermination condition of the model.
typevariablesVariables assigned to the model.