Suppose a model helps divide a one-unit budget between two services. It predicts 0.51 units for each, close to a reference allocation of 0.5 each. The prediction error is small, but the total exceeds the budget. Learning useful allocations means respecting that limit as well as predicting accurately.

Figure 01 One budget, three steps

A candidate allocation, its budget constraint, and its projection K 011 y1y2 y
y^\widehat{y}

1. Propose an allocation. To show the same mismatch clearly, the figure uses y^=(0.8,0.8)\widehat{y}=(0.8, 0.8). It requests 1.6 units from a one-unit budget.

2. Check the rule. The triangle KK contains nonnegative allocations whose sum is at most one. The candidate lies outside it.

3. Project to a feasible output. In this example, projection returns y=(0.5,0.5)y=(0.5, 0.5), the nearest feasible allocation.

Projection repairs the proposal with the smallest change. Other methods build constraint handling into training or output construction.

Robot routes must clear obstacles; generated molecules must obey structural rules. The budget problem captures the same distinction between a plausible proposal and a usable output. It also extends naturally from predicting one allocation to generating several alternatives.

Accurate, yet impossible to use

Accuracy asks how close the allocation is to a reference. Feasibility asks whether it fits the budget. Utility asks how well it serves the two services. A near-reference allocation can exceed the budget; a zero allocation is feasible but may be of little use.

A neural network can learn from past solutions, taking demand and other features xx as inputs and producing a candidate y^=fθ(x)\widehat{y}=f_\theta(x). We use yy for the output finally returned. Ordinary network layers do not enforce the budget rule.

Follow the copper route from the input features to the predicted allocation.

Figure 02 The network proposes a candidate

Four input features connect to three hidden units with affine and ReLU operations, then to two affine output units. One route is highlighted in copper.
y^=fθ(x)\widehat{y}=f_\theta(x)
The route passes through an affine/ReLU hidden layer to an affine readout. Circles denote features or neural units, and lines show learned connections. θ\theta denotes the network’s weights and biases.

Draw the feasible set first

Write the two allocations as y1y_1 and y2y_2. They must be nonnegative, and their sum must not exceed one. The allowed outputs form a triangle:

K={y:y1≥0,y2≥0,y1+y2≤1}.K=\left\{y:\begin{aligned}y_1&\ge0,\quad y_2\ge0,\\y_1+y_2&\le1\end{aligned}\right\}.

The point (0.8,0.8)(0.8,0.8) sits outside this triangle. The points (0.4,0.4)(0.4,0.4), (1,0)(1,0), and (0,0)(0,0) lie inside or on its boundary. Feasibility alone does not say which allocation best serves the task.

A feasible set can combine equalities, such as conservation laws, with inequalities, such as capacity limits. It may also depend on today’s demand, the current robot state, or a requested output schema. Write this instance-dependent set as KxK_x, where xx describes the instance. The requirement applies to the returned yy, after any decoding, completion, correction, or solve.

A hard requirement determines which outputs are admissible. For the triangle, all three inequalities must hold. Numerical implementations check equality and inequality residuals against stated tolerances.

The triangle is a nonempty convex set defined by linear inequalities. Other constraints produce different geometries:

Compare the allowed outputs below: a surface, a region with a hole, isolated points, and a region that changes with the input.

Figure 03 Different constraints, different geometries

EqualitiesOutputs lie on a surface.
Nonconvex setsHoles or separated regions.
Discrete choicesOnly selected outputs exist.
Input-dependent setsThe instance changes the region.
These structures can coexist in one task. They determine which maps, updates, and recovery operations are available.

Can we just add a penalty?

A constraint penalty makes violations expensive during training. Added to the task loss, it can supply constraint information even when feasible labels are scarce. DL2, for example, translates logical specifications into losses optimized with gradient methods.[1]

The penalty weight sets the trade-off between task loss and violation. The result also depends on the model, data, optimization accuracy, and aggregation: a small average violation can conceal a few allocations that exceed the budget.

Consider a pointwise quadratic penalty: stay near the preferred allocation y^\widehat{y} = (0.8,0.8)(0.8,0.8), while penalizing an exceeded budget:

min⁡y12∥y−y^∥22+λ2[max⁡(0,y1+y2−1)]2.\begin{aligned}\min_y\quad&\frac{1}{2}\lVert y-\widehat{y}\rVert_2^2\\&{}+\frac{\lambda}{2}\bigl[\max(0,y_1+y_2-1)\bigr]^2.\end{aligned}

Here λ≥0\lambda\ge0 is the penalty weight. For this symmetric example, the optimum remains nonnegative and its budget violation is 0.61+2λ\frac{0.6}{1+2\lambda}. Increasing λ\lambda brings it closer to the triangle. For every finite λ\lambda the optimum still exceeds the budget in this quadratic formulation. The explorer below varies the weight and symmetric target. A target that already fits the budget stays unchanged. The optimum is computed analytically; no network is trained in this demonstration.

This persistent violation comes from the quadratic penalty in this example; exact-penalty methods can recover feasible optima under suitable conditions. A training penalty also needs a separate argument to establish feasibility of a learned predictor’s outputs. Output construction and correction offer other ways to enforce the rule.

Four families of constraint handling

Constraint handling can act during training, within a network layer, through the output representation, or after prediction. Our survey organizes these roles into four families: training-based approaches, structured NN layers, constraint parameterization, and post-processing.

Figure 04 Where does the constraint mechanism act?

Training-based approaches
xx → predictor → y^\widehat{y} = yyConstraints shape the predictor during training; its prediction is returned directly.
Structured NN layers
xx → predictor → y^\widehat{y} → layer → yyTraining uses the task loss evaluated after the layer.
Post-processing
xx → predictor → y^\widehat{y} → repair → yyA repair can apply the same projection after prediction, without training through it.
Constraint parameterization
xx → predictor → u → decoder → yyThe predictor supplies admissible coordinates; decoding constructs a feasible decision.

For the triangle, choose two coordinates: bb is the fraction of the budget used, and qq is the share assigned to the first service. With both in [0,1][0,1], decode the allocation as (bq,b(1−q))\bigl(bq,b(1-q)\bigr). Its entries are nonnegative and sum to b≤1b\le1, so the budget rule is built into the output.

For example, b=0.6b=0.6 and q=0.7q=0.7 give (0.42,0.18)(0.42,0.18). The coordinate controls below construct an allocation directly.

Compare the three operations: vary the penalty weight, then switch to projection for the same candidate. Parameterization constructs an allocation from coordinates.

On a triangular feasible set, the quadratic penalty leaves a violation while projection lands on the boundary; feasible coordinates construct an interior point.
For the outside candidate shown, increasing the penalty approaches the triangle and projection reaches its boundary. Feasible coordinates construct an allocation directly.

The explorer shows three operations for four families: a projection layer and a post-processing projection have the same forward result here. They differ during training. The layer affects how the predictor learns; post-processing is applied afterwards.

Training-based approaches: change the predictor

Constraints can shape the training loss, the learning problem, or a verification-guided modification of the model.[2][3] The resulting predictor runs directly at inference. Improving average behavior and establishing a property for every input in a specified region are different outcomes.

Structured NN layers: train through an output operation

A layer can transform a prediction, complete missing variables, or solve a constrained optimization problem. The task loss is evaluated on the layer’s output; training uses its derivative or a method-specific backward approximation. Examples include OptNet and differentiable convex optimization layers.[4][5] The forward operation supplies feasibility up to its numerical accuracy. Training through it lets the predictor anticipate its effect.

Constraint parameterization: use a feasible representation

The budget decoder builds feasibility into the representation. Other representations use coefficients that combine feasible generators, parameters of a distribution supported on feasible outputs, or coordinates for a feasible-set map.

ConstraintNet illustrates feasible combinations: admissible weights combine the vertices of an input-dependent polytope;[6] gauge-based approaches map a bounded reference region into the feasible set, with equality completion where required.[7] The final operation still matters: a mean of feasible samples can lie outside a nonconvex set. Coverage matters too; a valid representation may exclude useful solutions.

Post-processing: recover the returned output

A correction, search, or solver recovers a decision from the candidate without training through that operation. Options include projection, geometry-specific recovery, and using the candidate to warm-start a solver.[8] Recovery can make an infeasible prediction usable, at a computational cost and with a possible change in decision quality.

These families can work together. DC3, for example, combines equality completion with differentiable inequality correction.[9] The final output must satisfy the combined requirements; later operations can undo constraints enforced earlier. Method-level details are in the prediction guide on GitHub.

From constrained prediction to constrained generation

The same demand may admit several useful allocations: one favors the first service, another the second, and a third leaves some budget unused. Rather than commit to one prediction, a generative model uses random inputs to propose alternatives, optionally conditioned on that demand and task context.

Each sample must still fit the budget. We also care about which allocations appear, and how often. A correction can make every sample feasible while concentrating them on a few allocations. Generation therefore requires attention to diversity and coverage as well as sample quality, and to fidelity when the task specifies a target distribution.

Direct generation produces a complete candidate, so prediction mechanisms can be reused. This includes iterative models distilled into a faster noise-to-output map. Autoregressive generation builds candidates through successive choices. A grammar can restrict those choices,[10] but a locally valid prefix may still have no valid completion.

Diffusion and flow-based generation instead build samples through repeated updates. A diffusion sampler uses a learned denoiser or score to move through noise levels, with stochastic or deterministic updates. A flow-matching sampler integrates a learned velocity field from a prior toward a data distribution. These iterative models are the focus here. Write ztz_t for a sampling state at generation time tt, reserving yy for the returned sample.

Follow the copper dots along one path, then compare the endpoint density profiles.

Figure 05 One particle moves; the distribution changes

Fifteen smooth sample paths connect a normal prior to a bimodal target. Three copper dots follow one sample through time; the endpoint density profiles share the same vertical scale.
dztdt=vθ(zt,t)\displaystyle\frac{\mathrm{d}z_t}{\mathrm{d}t}=v_\theta(z_t,t)
The copper dots track one sample at three times; all paths together transform the prior distribution into the target. The density profiles share a sample-value scale. The equation describes a flow model, while the illustrated paths use an analytic toy transport with no trained velocity field or output constraint. Background: Meta's Flow Matching guide.

Repeated updates create several places to impose constraints: the current state, an estimate of the completed sample, or the final output. These objects need not satisfy the same requirements.

Constraint handling in diffusion and flow models

For an iterative allocation generator, the budget rule applies to the completed allocation; noisy intermediate states need not satisfy it. In trajectory generation, each update similarly revises a candidate route. Generation time describes the sampler’s progress, distinct from physical time along the route.

Figure 06 Follow one sampling run

The central block repeats until the sampler returns an output. A constraint method may modify a sampling state, an estimate of the completed sample, or the returned result; its guarantee depends on which object it constrains.

Valid samples can still follow different distributions

Take the uniform distribution over the allocation triangle as the target. Draw proposals uniformly from [−1,1]2[-1,1]^2 and compare two ways to obtain feasible samples.

Compare the clouds: look for samples accumulating on the edges and at the origin. Resample, then compare both methods with the uniform target.

256 rejection samples fill the feasible triangle, after 1,917 uniform-square proposals.256 projected samples include 218 boundary outputs; the large origin circle represents 75 coincident samples.
Projection moves outside proposals to the boundary; rejection discards them. Larger circles indicate coincident outputs.

Rejection keeps feasible draws from the uniform square; independent draws and an exact membership test give the conditional target, with population acceptance 18\tfrac18. Projection returns feasible outputs from every proposal, but places population mass 78\tfrac78 on the boundary, including 14\tfrac14 at the origin. Thus full feasibility does not imply the desired distribution. This experiment isolates an output operation, with no trained diffusion or flow model. The same issue arises in those samplers: constraint handling can change the distribution of returned samples.

For diffusion and flow models, our survey groups constraint handling into four overlapping viewpoints: training-based methods, geometry-aware sampling, constraint parameterization, and guidance, correction, and search.

Figure 07 Four viewpoints on iterative generation

Training-based methods
Constraint feedback updates the generator’s parameters before sampling.
Geometry-aware sampling
The update follows the geometry, for example by moving along a manifold or reflecting at a boundary.
Constraint parameterization
The sampler generates coordinates; a feasible decoder maps them into the decision set.
Guidance, correction, and search
A score or validity check can steer a proposal, repair it, or select among alternatives.
A sampler can combine these operations.

Geometry-aware examples include Reflected Diffusion and Riemannian Flow Matching.[11][15] Universal Guidance steers sampling with an objective;[12] Projected Diffusion and Physics-Constrained Flow Matching apply corrections at different points.[13][14] Search instead evaluates and selects continuations, including when the objective cannot be differentiated.

Combining mechanisms

Training can anticipate a sampling correction, while different stages handle complementary requirements. Their order matters: output-space guidance can move a decoded sample outside the feasible set. Guiding within admissible coordinates or correcting for the combined requirements can avoid this conflict. The generation guide on GitHub covers the complete classification, including discrete interventions, bridge and control formulations.

Choose from the operations you have

In the triangle example, projection ensures feasibility but changes the target distribution; rejection preserves that distribution but discards proposals. The choice depends on the required outcome—feasibility of each sample, a population statistic, or a sampling-process property—and on the operations available for the task.

Available operations → candidate mechanisms
What you haveWhat to consider and check
A feasible representation or domain geometryConsider parameterization or geometry-aware sampling. Check representation validity, coverage, numerical preservation, and construction cost.
A reliable projection, correction, or solverConsider post-processing for an existing predictor or generator. Train through the operation when the model should anticipate its effect. Measure the entire procedure at the required tolerance.
A differentiable violation measureConsider training-based methods when constraints recur, or guidance during sampling. Establish final feasibility separately when the task requires it.
A cheap membership testConsider rejection. Check the acceptance rate and whether the resulting conditional distribution is the one you want.
An evaluator for candidate quality or validityConsider candidate or path search without objective gradients. Count competing proposals and failed attempts; a finite search budget need not find a feasible sample.
A test for valid sequence completionsConsider constrained decoding. Check that permitted choices still admit a valid completion.

Include preparation costs: building a feasible representation may require vertex enumeration or a reference-point solve. A model used as a solver warm start should save time over a conventional initialization at the same stopping tolerance.

When the same constraints recur, training or preprocessing costs can be spread across many queries. When constraints change, online construction and enforcement matter more.

Evaluate the result you will actually use

Evaluate the complete procedure, including any decoding or repair, along four dimensions:

  • Constraint satisfaction: Distinguish requirements on each output from population statistics: an expectation bound does not ensure every sample is feasible. Report the fraction of outputs meeting the chosen tolerances and the equality and inequality residuals, including their largest values. Identify the evidence: a construction, solver, certificate, probability statement, or evaluated samples.
  • Output quality: For prediction, report accuracy against reference outputs and the utility of the resulting decisions. For generation, examine quality, diversity, and coverage; assess fidelity to a target distribution when the task specifies one.
  • Computational cost: Count preprocessing and training where relevant, then measure the entire inference procedure. Include checks, correction, decoding, failed attempts, and solver work. For generation, report cost per valid sample.
  • Applicable scope: Which constraint geometry, input region, oracle, solver condition, or discretization does the result require? If feasibility holds in continuous time, explain how the numerical updates preserve it.

If a solver repairs a prediction, measure the repaired decision’s quality and the complete latency. If a relaxation is rounded, check the rounded output against the original constraints. Later stages can invalidate an earlier feasibility guarantee.

Prediction asks for a useful allocation that fits the budget. Generation also asks for the right collection of alternatives. Feasibility, quality, distribution, and cost belong in the same comparison.

The Hard-Constrained Machine Learning repository collects the full taxonomy, methods, and application literature from our companion survey. A public preprint link will be added when available.

Selected references

  1. Fischer et al. DL2: Training and Querying Neural Networks with Logic. ICML, 2019.
  2. Chamon et al. Constrained Learning with Non-Convex Losses. IEEE Transactions on Information Theory, 2023.
  3. Zhao et al. Ensuring DNN Solution Feasibility for Optimization Problems with Linear Constraints. ICLR, 2023.
  4. Amos and Kolter. OptNet: Differentiable Optimization as a Layer in Neural Networks. ICML, 2017.
  5. Agrawal et al. Differentiable Convex Optimization Layers. NeurIPS, 2019.
  6. Brosowsky et al. Sample-Specific Output Constraints for Neural Networks. AAAI, 2021.
  7. Li, Kolouri, and Mohammadi. Learning to Solve Optimization Problems with Hard Linear Constraints. IEEE Access, 2023.
  8. Liang, Chen, and Low. Low Complexity Homeomorphic Projection to Ensure Neural-Network Solution Feasibility for Optimization over (Non-)Convex Set. ICML, 2023.
  9. Donti, Rolnick, and Kolter. DC3: A Learning Method for Optimization with Hard Constraints. ICLR, 2021.
  10. Geng et al. Grammar-Constrained Decoding for Structured NLP Tasks without Finetuning. EMNLP, 2023.
  11. Lou and Ermon. Reflected Diffusion Models. ICML, 2023.
  12. Bansal et al. Universal Guidance for Diffusion Models. CVPR Workshops, 2023.
  13. Christopher, Baek, and Fioretto. Constrained Synthesis with Projected Diffusion Models. NeurIPS, 2024.
  14. Utkarsh et al. Physics-Constrained Flow Matching: Sampling Generative Models with Hard Constraints. NeurIPS, 2025.
  15. Chen and Lipman. Flow Matching on General Geometries. ICLR, 2024.