Metadata-Version: 2.4
Name: onnxgurobi
Version: 0.1.3
Summary: The onnx_to_gurobi is a Python library that creates Gurobi models for neural networks in ONNX format.
Author-email: Muhammad Atallah <muhammad.sabri@outlook.com>
License-Expression: MIT
Project-URL: Homepage, https://yourhomepage.example.com/
Project-URL: Repository, https://github.com/yourusername/onnx_gurobi
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Operating System :: OS Independent
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE.txt
Requires-Dist: numpy>=1.26.4
Requires-Dist: onnx>=1.16.2
Requires-Dist: onnxruntime>=1.20.1
Requires-Dist: torch>=2.5.1
Requires-Dist: pytest>=8.3.5
Requires-Dist: tensorflow>=2.19.0
Requires-Dist: matplotlib>=3.10.1
Dynamic: license-file

# Overview

The ONNX-To-Gurobi is a Python library that creates Gurobi models for neural networks in ONNX format.

The library has been designed to allow easy extensions, and it currently supports the following ONNX nodes:

- Add
- Sub
- MatMul
- Gemm
- ReLu
- Conv
- Unsqueeze
- MaxPool
- AveragePool
- BatchNormalization
- Flatten
- Identity
- Reshape
- Shape
- Concat
- Dropout


# Installation

We highly recommend creating a virtual conda environment and installing the library within the environment by following the following steps:

1- Gurobi is not installed automatically. Please install it manually using:
```
    conda install -c gurobi gurobi
```
2- Make sure to switch to Python 11 inside your environment using:
```
    conda install python=11
``` 

3- Install the library using:
```
    pip install onnxgurobi
```

# Getting Started

The ```ONNXToGurobi``` class provides the central interface for converting an ONNX model into a Gurobi optimization model.

To get access to the class's methods and attributes, you need to import it using:

```
from onnx_to_gurobi.onnxToGurobi import ONNXToGurobi

```


The ```ONNXToGurobi``` class:

- Parses the ONNX graph and constructs an internal representation of each operator and its corresponding tensor shapes.

- Creates a Gurobi model along with the necessary variables and constraints.

- Exposes all model components (decision variables, Gurobi Model object, node definitions, tensor shapes), allowing you to:

* Set or fix input variables to specific values.

* Introduce objectives.

* Add your own constraints.

* Solve the resulting MILP and then inspect or extract the outputs from the solution.


An overview of the class’s methods and attributes:

```
class ONNXToGurobi:
    def build_model(self):
        """
        Constructs the Gurobi model by creating variables and applying operator constraints.

        """

    def get_gurobi_model(self):
        """
        Retrieves the Gurobi model after all constraints have been added.

        Returns:
            gurobipy.Model: The constructed Gurobi model reflecting the ONNX graph.
        """

    # Attributes:
    self.model               # The Gurobi Model object
    self.variables           # A dict mapping tensor names to Gurobi variables (or constants)
    self.in_out_tensors_shapes # Shapes of all input and output tensors
    self.nodes               # Node definitions parsed from ONNX
    self.initializers        # Constant tensors extracted from the ONNX graph

```

# How to Use

See [example1.py](./examples/example1.py) for a simple example.
See [example2.py](./examples/example2.py) for a detailed adversarial example.
