Pytorch Sequential Network Constraint

pyscipopt_ml.torch.add_sequential_constr(scip_model, sequential_model, input_vars, output_vars=None, unique_naming_prefix='', output_type='regression', **kwargs)

Formulate sequential_model into scip_model.

Parameters:
  • scip_model (PySCIPOpt Model) – The SCIP model where the sequential model should be inserted.

  • sequential_model (torch.nn.Sequential) – The sequential model to insert as predictor.

  • input_vars (np.ndarray) – Decision variables used as input for the sequential neural network in PySCIPOpt Model.

  • output_vars (np.ndarray) – Decision variables used as output for the sequential neural network in the PySCIPOpt Model

  • unique_naming_prefix (str, optional) – A unique naming prefix that is used before all variable and constraint names. This parameter is important if the SCIP model is later printed to file and many predictors are added to the same SCIP model.

  • output_type ({"classification", "regression"}, default="regression") – If the option chosen is “classification” the output is 1 for exactly one class and 0 for all others. If the option chosen is “regression” then the output for each node of the final layer is the value from the sequential torch model.

Returns:

Object containing information about what was added to model to insert the predictor into it

Return type:

SequentialConstr

Raises:

NoModel – If a section of the Pytorch model structure (layer or activation) is not implemented.

Note

See add_predictor_constr for acceptable values for input_vars and output_vars

class pyscipopt_ml.torch.sequential.SequentialConstr(scip_model, predictor, input_vars, output_vars=None, unique_naming_prefix='', output_type='regression', **kwargs)

Transform a pytorch Sequential Neural Network to SCIP constraints with input and output as matrices of variables.

Stores the changes to the SCIP Model for representing an instance into it. Inherits from AbstractPredictorConstr..

get_error(eps=None)

Returns error in SCIP’s solution with respect to the actual output of the sequential neural network

Parameters:

eps (float or int or None, optional) – The maximum allowed tolerance for a mismatch between the actual predictive model and SCIP. If the error is larger than eps an appropriate warning is printed

Returns:

error – The absolute values of the difference between SCIP’s solution and the trained ML model’s output given the input as defined by SCIP. The matrix is the same dimension as the output of the trained predictor. Using torch / pyscipopt, the absolute difference between model.forward(input) and scip.getVal(output).

Return type:

np.ndarray

Raises:

NoSolution – If SCIP has no solution (either was not optimized or is infeasible).