Skip to content

Commit d9c1591

Browse files
authored
Merge pull request #1 from radevgit/all_selen_types
All selen types
2 parents 3d83aa3 + 8728206 commit d9c1591

72 files changed

Lines changed: 7846 additions & 209 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

AGPRICE_MISSING_CONSTRAINTS.md

Lines changed: 251 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,251 @@
1+
# Analysis: Missing Constraints in agprice_full.rs
2+
3+
## Executive Summary
4+
5+
The generated `agprice_full.rs` file is **missing critical upper bound constraints** from the original MiniZinc model. This causes the optimization problem to be **mathematically unbounded**, leading to astronomical solution values (e.g., 8.2e51).
6+
7+
**The issue is NOT with Selen's optimizer - it's with the MiniZinc-to-Rust conversion process.**
8+
9+
## Problem Manifestation
10+
11+
When running the generated file:
12+
```
13+
OBJECTIVE = -8245408037230409000000000000000000000000000000000000 (-8.2e51)
14+
xm = 128512896896560200000000000000000000 (1.3e35)
15+
xb = 257154306690016960000000000000000000000 (2.6e38)
16+
qsq = 8245408037230409000000000000000000000000000000000000 (8.2e51)
17+
```
18+
19+
All main decision variables (milk, butt, cha, etc.) = 0, while auxiliary variables explode to astronomical values.
20+
21+
## Root Cause: Missing Constraints
22+
23+
### Original MiniZinc Model
24+
Source: https://www.hakank.org/minizinc/agprice.mzn
25+
26+
The original model contains **THREE CRITICAL GROUPS** of constraints that bound the problem:
27+
28+
### 1. Resource Limit Constraints (MISSING)
29+
30+
These constraints limit the production resources:
31+
32+
```minizinc
33+
0.04*xm+0.8*xb+0.35*xca+0.25*xcb<=0.600 /\
34+
0.09*xm+0.02*xb+0.3*xca+0.4*xcb<=0.750 /\
35+
4.82*milk+0.32*butt+0.21*cha+0.07*chb <= 1.939 /\
36+
```
37+
38+
**Status**: ❌ **COMPLETELY MISSING** from generated file
39+
**Impact**: Without these, xm, xb, xca, xcb can grow unbounded
40+
41+
### 2. Piecewise Linear Variable Sum Constraints (MISSING)
42+
43+
These constraints ensure the piecewise linear approximation variables sum to at most 1.0:
44+
45+
```minizinc
46+
sum (i in point) (lmilk[i])<=1.0 /\
47+
sum (i in point) (lbutt[i])<=1.0 /\
48+
sum (i in point) (lcha[i])<=1.0 /\
49+
sum (i in point) (lchb[i])<=1.0 /\
50+
sum (i in point) (mq[i]+lq[i])<=1.0
51+
```
52+
53+
**Status**: ❌ **COMPLETELY MISSING** from generated file
54+
**Impact**: Without these, the lmilk, lbutt, lcha, lchb arrays can grow unbounded
55+
56+
### 3. Basic Non-negativity Constraints (PRESENT)
57+
58+
```minizinc
59+
milk >= 0.0 /\
60+
milksq >= 0.0 /\
61+
butt >= 0.0 /\
62+
buttsq >= 0.0 /\
63+
cha >= 0.0 /\
64+
chasq >= 0.0 /\
65+
chb >= 0.0 /\
66+
chbsq >= 0.0 /\
67+
xm >= 0.0 /\
68+
xb >= 0.0 /\
69+
xca >= 0.0 /\
70+
xcb >= 0.0 /\
71+
qsq >= 0.0 /\
72+
forall(i in point) (lmilk[i] >= 0.0) /\
73+
forall(i in point) (lbutt[i] >= 0.0) /\
74+
forall(i in point) (lcha[i] >= 0.0) /\
75+
forall(i in point) (lchb[i] >= 0.0) /\
76+
forall(i in point) (lq[i] >= 0.0) /\
77+
forall(i in point) (mq[i] >= 0.0)
78+
```
79+
80+
**Status**: ✅ **PRESENT** in generated file (lines 296-313)
81+
**Note**: These are correctly translated
82+
83+
## Why This Causes Unbounded Solutions
84+
85+
The revenue equation is:
86+
```
87+
revenue = 420*cha + 1185*butt + 6748*milk - qsq - 8*chbsq - 194*chasq - 1200*buttsq - 6492*milksq + 70*chb
88+
```
89+
90+
Without the resource limit constraints:
91+
1. All variables have only **lower bounds** (>= 0) but **no upper bounds**
92+
2. Variables with **positive coefficients** in revenue (milk, butt, cha, chb) should increase to maximize revenue
93+
3. But without upper bound constraints, they can increase to **infinity**
94+
4. The solver is correctly maximizing within the given constraints - the problem is that the constraints are incomplete!
95+
96+
## Mathematical Proof of Unboundedness
97+
98+
Given only the constraints in the generated file:
99+
- Let milk = M, butt = B, cha = C, chb = K, all >= 0
100+
- Let all squared terms = 0 (for simplicity)
101+
- revenue = 6748*M + 1185*B + 420*C + 70*K
102+
103+
Without upper bounds on M, B, C, K:
104+
- For any revenue value R, we can set M = R/6748 to achieve it
105+
- As M → ∞, revenue → ∞
106+
- **Therefore: The problem is mathematically unbounded**
107+
108+
The resource constraints like `4.82*milk + 0.32*butt + 0.21*cha + 0.07*chb <= 1.939` are essential to bound the feasible region.
109+
110+
## Verification
111+
112+
### Check 1: Search for resource limits
113+
```bash
114+
grep -E "0.600|0.750|1.939" /home/ross/devpublic/selen/debug/agprice_full.rs
115+
```
116+
**Result**: No matches found ❌
117+
118+
### Check 2: Count constraint statements
119+
```bash
120+
grep -E "(float_lin_le|float_lin_eq)" /home/ross/devpublic/selen/debug/agprice_full.rs | wc -l
121+
```
122+
**Result**: Only 16 constraint statements found
123+
124+
**Expected**: Should have:
125+
- 13+ basic >= 0 constraints ✅
126+
- 3 resource limit constraints ❌
127+
- 5 piecewise sum <= 1.0 constraints ❌
128+
- Multiple equality constraints for piecewise definitions ❌
129+
- Total should be 100+ constraint statements
130+
131+
## What Selen's Optimizer Is Doing (Correctly)
132+
133+
Selen's optimizer is working correctly:
134+
1. It detects that revenue can be maximized
135+
2. It respects all the constraints that ARE present (>= 0 bounds)
136+
3. Since there are no upper bounds, it tries to maximize revenue as much as possible
137+
4. The astronomical values are the solver's attempt to maximize within an unbounded feasible region
138+
139+
**This is mathematically correct behavior for an unbounded optimization problem.**
140+
141+
## Expected Behavior
142+
143+
With the complete constraint set from the MiniZinc model, the optimal solution should be:
144+
- **cha ≈ 10.0** (cheese 1 price around $10,000)
145+
- All other variables at reasonable finite values
146+
- Revenue at a finite optimal value
147+
148+
The original MiniZinc model with all constraints is **bounded** and has a **finite optimal solution**.
149+
150+
## Recommendations for Zelen (MiniZinc Converter)
151+
152+
The MiniZinc-to-Rust converter needs to properly translate:
153+
154+
1. **Linear inequality constraints** with floating-point coefficients:
155+
- `0.04*xm+0.8*xb+0.35*xca+0.25*xcb<=0.600`
156+
- Should generate: `model.float_lin_le(&vec![0.04, 0.8, 0.35, 0.25], &vec![xm, xb, xca, xcb], 0.600)`
157+
158+
2. **Sum constraints over arrays**:
159+
- `sum(i in point) (lmilk[i])<=1.0`
160+
- Should generate: `model.float_lin_le(&vec![1.0; 35], &lmilk, 1.0)` (all coefficients are 1.0)
161+
162+
3. **Combined array sums**:
163+
- `sum(i in point) (mq[i]+lq[i])<=1.0`
164+
- Should generate: `model.float_lin_le(&vec![1.0; 70], &[mq, lq].concat(), 1.0)`
165+
166+
## Test Case for Zelen
167+
168+
To verify the fix, the generated file should:
169+
1. Include all resource limit constraints
170+
2. Include all sum <= 1.0 constraints for piecewise variables
171+
3. When run, produce output with cha ≈ 10.0 (not 0)
172+
4. All variables should be finite (not astronomical)
173+
5. Revenue should be finite and positive
174+
175+
## Files for Reference
176+
177+
- **Generated file**: `/tmp/agprice_full.rs` (incomplete)
178+
- **Original MiniZinc**: https://www.hakank.org/minizinc/agprice.mzn (complete)
179+
- **Expected output**: cha ≈ 10.0, finite revenue
180+
181+
## Conclusion
182+
183+
This is **NOT a bug in Selen's constraint solver or optimizer**. Selen is correctly solving the optimization problem as specified in the generated file. The problem is that the generated file is **missing critical constraints** that bound the feasible region.
184+
185+
The fix needs to be in the MiniZinc-to-Rust converter (Zelen) to ensure all constraints are properly translated.
186+
187+
---
188+
189+
## UPDATE: New Export Issue Found (agprice_test.rs)
190+
191+
After the constraint export was fixed, a new issue was discovered in `/tmp/agprice_test.rs`:
192+
193+
### Problem: Missing Parameter Array Definitions
194+
195+
The file contains comments indicating parameter arrays that should be defined:
196+
```rust
197+
// Array parameter: X_INTRODUCED_212_ (initialization skipped in export)
198+
// Array parameter: X_INTRODUCED_214_ (initialization skipped in export)
199+
// ... etc
200+
```
201+
202+
But these arrays are **never actually defined** in the code. Later, the constraints try to use them:
203+
```rust
204+
model.float_lin_eq(&x_introduced_212_, &vec![xm, milk], 1.4); // ❌ ERROR: x_introduced_212_ not defined
205+
model.float_lin_eq(&x_introduced_214_, &vec![xb, butt], 3.7); // ❌ ERROR: x_introduced_214_ not defined
206+
```
207+
208+
### What Should Be Generated
209+
210+
Based on the MiniZinc constraints like:
211+
```minizinc
212+
(1.0/4.82)*xm+(0.4/0.297)*milk = 1.4
213+
```
214+
215+
The exporter should generate:
216+
```rust
217+
let x_introduced_212_ = vec![1.0/4.82, 0.4/0.297];
218+
model.float_lin_eq(&x_introduced_212_, &vec![xm, milk], 1.4);
219+
```
220+
221+
### Missing Arrays
222+
223+
From the comments, these parameter arrays need to be defined:
224+
- `x_introduced_212_` - coefficients for equation with xm, milk
225+
- `x_introduced_214_` - coefficients for equation with xb, butt
226+
- `x_introduced_216_` - coefficients for equation with cha, xca, chb
227+
- `x_introduced_218_` - coefficients for equation with chb, xcb, cha
228+
- `x_introduced_220_` - coefficients for constraint with xca, xb, xm, xcb (≤ 0.6)
229+
- `x_introduced_222_` - coefficients for constraint with xca, xb, xm, xcb (≤ 0.75)
230+
- `x_introduced_224_` - coefficients for constraint with cha, butt, milk, chb (≤ 1.939)
231+
- `x_introduced_226_` - coefficients for equation with chb, cha, q
232+
- And several more for sum constraints
233+
234+
### Current Status
235+
236+
✅ Constraint structure is correct (the calls to float_lin_eq/float_lin_le are proper)
237+
✅ Variables are declared correctly
238+
**Coefficient arrays are missing** - marked as "initialization skipped in export"
239+
**File does not compile** - 284 compilation errors due to undefined variables
240+
241+
### Next Steps for Zelen
242+
243+
1. Generate the coefficient array definitions for all `X_INTRODUCED_NNN_` parameter arrays
244+
2. Each should be a `vec![...]` of floating-point coefficients
245+
3. Extract the coefficients from the original FlatZinc constraints
246+
247+
---
248+
249+
**Date**: October 7, 2025
250+
**Selen Version**: v0.9.4
251+
**Analyzer**: GitHub Copilot

Cargo.lock

Lines changed: 23 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "zelen"
3-
version = "0.1.1"
3+
version = "0.2.0"
44
edition = "2024"
55
description = "Selen CSP solver parser for FlatZinc"
66
rust-version = "1.88"
@@ -26,7 +26,7 @@ name = "zelen"
2626
path = "src/bin/zelen.rs"
2727

2828
[dependencies]
29-
selen = "0.9"
29+
selen = "0.12"
3030
clap = { version = "4.5", features = ["derive"] }
3131

3232
[dev-dependencies]

0 commit comments

Comments
 (0)