Pyconstruct: Constraint Programming Meets Structured Prediction

Paolo Dragone, Stefano Teso, Andrea Passerini

Introduction

Structured prediction is a cornerstone of machine learning, essential for tasks ranging from sequence labeling to image segmentation. However, traditional frameworks often rely on the assumption that the "inference" step—identifying the optimal output for a given input—can be efficiently encoded using standard methods like linear programming or dynamic programming.

In practice, many "constructive" tasks involve complex, non-standard output spaces that do not fit these templates. For example, in layout synthesis or architectural design, outputs involve discrete variables (object types), continuous variables (coordinates), and a web of arbitrary constraints (e.g., non-overlapping components or specific design guidelines). Pyconstruct addresses this challenge by bridging the gap between structured prediction and constraint programming (CP), providing a framework where the inference oracle is both programmable and declarative.

Methodology

Pyconstruct decouples the learning logic from domain-specific constraints by combining two distinct paradigms:

1. Max-Margin Learning

The library utilizes state-of-the-art learning algorithms, including Stochastic Subgradient (SSG) and Block-Coordinate Frank-Wolfe. These methods are "solver-agnostic," meaning they require only the result of an inference query (the MAP assignment) to update model weights. This allows the learning process to remain consistent regardless of the underlying complexity of the output space.

2. Declarative Inference via MiniZinc

Instead of implementing a custom, ad-hoc solver for every new problem, users define their output domain, feasibility constraints, and feature maps using MiniZinc—a high-level, declarative language for constraint satisfaction. When a prediction is required, Pyconstruct compiles the MiniZinc model and executes it through a backend solver (such as Gecode, Gurobi, or OptiMathSAT). This "inference as code" approach allows researchers to modify the structure of the output space by simply editing a text file.

Experimental Setup: Handwritten Equation Recognition

To demonstrate the utility of the framework, the authors applied Pyconstruct to a handwritten equation recognition task. Using a subset of the ICFHR’14 CROHME (Competition on Recognition of Online Handwritten Mathematical Expressions) dataset, the model was tasked with recognizing equations in the format $a + b = c$ from sequences of images. This task is particularly suited for CP because the output must adhere to strict mathematical syntax, which can be easily expressed as constraints.

Key Insights

  • Constraints as Regularizers: Hard constraints (e.g., "an equation must contain exactly one '+' sign") act as powerful forms of prior knowledge. This is especially beneficial in low-data regimes, as it prevents the model from exploring or predicting "impossible" configurations.
  • Handling Hybrid Spaces: By leveraging CP solvers, Pyconstruct naturally manages problems that mix discrete categories with continuous numerical constraints—a combination that is traditionally difficult for pure machine learning approaches.
  • Programmable Oracles: The ability to define the inference step declaratively significantly reduces the engineering overhead. In the authors' experiments, the entire domain logic for the equation solver was implemented in approximately 30 lines of code.

Results

The study compared a "constrained" model (incorporating MiniZinc rules about equation structure) against an "unconstrained" baseline on the OCR task:

  • Accuracy: The constrained model achieved an average Hamming loss of 0.102, significantly outperforming the unconstrained model, which stalled at 0.373.
  • Data Efficiency: The constrained model's training loss was 22% lower on average, confirming that domain knowledge allows the model to learn more effectively from fewer samples.

Limitations and Future Work

While Pyconstruct offers significant flexibility, it introduces a trade-off in terms of inference speed. General-purpose constraint solvers are typically slower than specialized algorithms like Viterbi. For real-time or extremely large-scale applications, the overhead of the CP solver may become a bottleneck. Additionally, while CP solvers are robust, extremely high-dimensional combinatorial spaces may still present challenges for convergence during the learning phase.

Future research directions include extending this framework toward "learning modulo theories" and constructive preference elicitation, where systems learn user preferences within highly constrained design environments.

Summary updated

BibTeX
@inproceedings{dragone2018pyconstruct,
  author = {Paolo Dragone and Stefano Teso and Andrea Passerini},
  title = {{Pyconstruct: Constraint Programming Meets Structured Prediction}},
  booktitle = {International Joint Conference on Artificial Intelligence},
  year = {2018},
  doi = {10.24963/ijcai.2018/850},
}
Figure