converge-optimization 3.9.2

Optimization algorithms for converge.zone - Rust reimplementation of OR-Tools subset
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
//! Pricing Guardrails Pack
//!
//! JTBD: "Set pricing within guardrails ensuring margin targets and competitive position"
//!
//! ## Problem
//!
//! Given:
//! - Products with cost data
//! - Competitor prices for market positioning
//! - Margin requirements (minimum and target)
//! - Price bounds (guardrails)
//!
//! Find:
//! - Recommended prices that respect margins and guardrails
//! - Margin analysis and compliance reporting
//!
//! ## Solver
//!
//! Uses rule-based pricing:
//! 1. Calculate minimum price to meet margin requirement
//! 2. Apply competitive strategy to adjust price
//! 3. Enforce price bounds (guardrails)
//! 4. Generate recommendations with compliance analysis

mod invariants;
mod solver;
mod types;

pub use invariants::*;
pub use solver::*;
pub use types::*;

use crate::packs::{InvariantDef, InvariantResult, Pack, PackSolveResult, default_gate_evaluation};
use converge_pack::gate::GateResult as Result;
use converge_pack::gate::{KernelTraceLink, ProblemSpec, PromotionGate, ProposedPlan};
use converge_pack::{CONFIDENCE_STEP_MAJOR, CONFIDENCE_STEP_MINOR};

/// Pricing Guardrails Pack
pub struct PricingGuardrailsPack;

impl Pack for PricingGuardrailsPack {
    fn name(&self) -> &'static str {
        "pricing-guardrails"
    }

    fn version(&self) -> &'static str {
        "1.0.0"
    }

    fn validate_inputs(&self, inputs: &serde_json::Value) -> Result<()> {
        let input: PricingGuardrailsInput =
            serde_json::from_value(inputs.clone()).map_err(|e| {
                converge_pack::GateError::invalid_input(format!("Invalid input: {}", e))
            })?;
        input.validate()
    }

    fn invariants(&self) -> &[InvariantDef] {
        INVARIANTS
    }

    fn solve(&self, spec: &ProblemSpec) -> Result<PackSolveResult> {
        let input: PricingGuardrailsInput = spec.inputs_as()?;
        input.validate()?;

        let solver = GuardrailPricingSolver;
        let (output, report) = solver.solve_pricing(&input, spec)?;

        let trace = KernelTraceLink::audit_only(format!("trace-{}", spec.problem_id));
        let confidence = calculate_confidence(&output);

        let plan = ProposedPlan::from_payload(
            format!("plan-{}", spec.problem_id),
            self.name(),
            output.summary(),
            &output,
            confidence,
            trace,
        )?;

        Ok(PackSolveResult::new(plan, report))
    }

    fn check_invariants(&self, plan: &ProposedPlan) -> Result<Vec<InvariantResult>> {
        let output: PricingGuardrailsOutput = plan.plan_as()?;
        Ok(check_all_invariants(&output))
    }

    fn evaluate_gate(
        &self,
        _plan: &ProposedPlan,
        invariant_results: &[InvariantResult],
    ) -> PromotionGate {
        default_gate_evaluation(invariant_results, self.invariants())
    }
}

fn calculate_confidence(output: &PricingGuardrailsOutput) -> f64 {
    if output.recommendations.is_empty() {
        return 0.0;
    }

    let mut confidence: f64 = 0.5;

    // Bonus for all margins met
    if output.guardrail_compliance.all_margins_met {
        confidence += CONFIDENCE_STEP_MAJOR;
    }

    // Bonus for all within bounds
    if output.guardrail_compliance.all_within_bounds {
        confidence += CONFIDENCE_STEP_MAJOR;
    }

    // Bonus for competitive position achieved
    if output.guardrail_compliance.competitive_position_achieved {
        confidence += CONFIDENCE_STEP_MINOR;
    }

    confidence.min(1.0)
}

#[cfg(test)]
mod tests {
    use super::*;
    use converge_pack::gate::ObjectiveSpec;

    fn create_test_input() -> PricingGuardrailsInput {
        PricingGuardrailsInput {
            products: vec![
                Product {
                    product_id: "SKU-001".to_string(),
                    name: "Widget A".to_string(),
                    unit_cost: 80.0,
                    current_price: Some(100.0),
                    price_bounds: Some(PriceBounds {
                        min_price: 90.0,
                        max_price: 150.0,
                    }),
                    competitor_prices: vec![CompetitorPrice {
                        competitor_id: "comp1".to_string(),
                        price: 110.0,
                        as_of_date: None,
                    }],
                    category: Some("widgets".to_string()),
                },
                Product {
                    product_id: "SKU-002".to_string(),
                    name: "Widget B".to_string(),
                    unit_cost: 50.0,
                    current_price: None,
                    price_bounds: None,
                    competitor_prices: vec![],
                    category: Some("widgets".to_string()),
                },
            ],
            margin_requirements: MarginRequirements {
                min_margin_pct: 20.0,
                target_margin_pct: 30.0,
                competitive_strategy: CompetitiveStrategy::MatchMarket,
            },
            price_bounds: Some(PriceBounds {
                min_price: 10.0,
                max_price: 1000.0,
            }),
        }
    }

    #[test]
    fn test_pack_name() {
        let pack = PricingGuardrailsPack;
        assert_eq!(pack.name(), "pricing-guardrails");
        assert_eq!(pack.version(), "1.0.0");
    }

    #[test]
    fn test_validate_inputs() {
        let pack = PricingGuardrailsPack;
        let input = create_test_input();
        let json = serde_json::to_value(&input).unwrap();
        assert!(pack.validate_inputs(&json).is_ok());
    }

    #[test]
    fn test_validate_inputs_rejects_invalid() {
        let pack = PricingGuardrailsPack;
        let mut input = create_test_input();
        input.products[0].unit_cost = -10.0;
        let json = serde_json::to_value(&input).unwrap();
        assert!(pack.validate_inputs(&json).is_err());
    }

    #[test]
    fn test_solve_basic() {
        let pack = PricingGuardrailsPack;
        let input = create_test_input();

        let spec = ProblemSpec::builder("test-001", "test-tenant")
            .objective(ObjectiveSpec::maximize("margin"))
            .inputs(&input)
            .unwrap()
            .seed(42)
            .build()
            .unwrap();

        let result = pack.solve(&spec).unwrap();
        assert!(result.is_feasible());

        let output: PricingGuardrailsOutput = result.plan.plan_as().unwrap();
        assert_eq!(output.recommendations.len(), 2);
        assert!(output.margin_analysis.average_margin_pct > 0.0);
    }

    #[test]
    fn test_check_invariants() {
        let pack = PricingGuardrailsPack;
        let input = create_test_input();

        let spec = ProblemSpec::builder("test-002", "test-tenant")
            .objective(ObjectiveSpec::maximize("margin"))
            .inputs(&input)
            .unwrap()
            .seed(42)
            .build()
            .unwrap();

        let result = pack.solve(&spec).unwrap();
        let invariants = pack.check_invariants(&result.plan).unwrap();

        // Should have all 4 invariants checked
        assert_eq!(invariants.len(), 4);
    }

    #[test]
    fn test_gate_promotes_valid() {
        let pack = PricingGuardrailsPack;
        let input = create_test_input();

        let spec = ProblemSpec::builder("test-003", "test-tenant")
            .objective(ObjectiveSpec::maximize("margin"))
            .inputs(&input)
            .unwrap()
            .seed(42)
            .build()
            .unwrap();

        let result = pack.solve(&spec).unwrap();
        let invariants = pack.check_invariants(&result.plan).unwrap();
        let gate = pack.evaluate_gate(&result.plan, &invariants);

        // With valid input, should either promote or require review
        assert!(!gate.is_rejected());
    }

    #[test]
    fn test_determinism() {
        let pack = PricingGuardrailsPack;
        let input = create_test_input();

        let spec1 = ProblemSpec::builder("test-a", "tenant")
            .objective(ObjectiveSpec::maximize("margin"))
            .inputs(&input)
            .unwrap()
            .seed(99999)
            .build()
            .unwrap();

        let spec2 = ProblemSpec::builder("test-b", "tenant")
            .objective(ObjectiveSpec::maximize("margin"))
            .inputs(&input)
            .unwrap()
            .seed(99999)
            .build()
            .unwrap();

        let result1 = pack.solve(&spec1).unwrap();
        let result2 = pack.solve(&spec2).unwrap();

        let output1: PricingGuardrailsOutput = result1.plan.plan_as().unwrap();
        let output2: PricingGuardrailsOutput = result2.plan.plan_as().unwrap();

        assert_eq!(output1.recommendations.len(), output2.recommendations.len());
        for (r1, r2) in output1
            .recommendations
            .iter()
            .zip(output2.recommendations.iter())
        {
            assert_eq!(r1.product_id, r2.product_id);
            assert!((r1.recommended_price - r2.recommended_price).abs() < 0.01);
        }
    }

    #[test]
    fn test_margin_enforcement() {
        let pack = PricingGuardrailsPack;
        let mut input = create_test_input();
        input.margin_requirements.min_margin_pct = 30.0;
        input.margin_requirements.target_margin_pct = 35.0;
        input.margin_requirements.competitive_strategy = CompetitiveStrategy::IgnoreCompetitors;

        let spec = ProblemSpec::builder("test-margin", "test-tenant")
            .objective(ObjectiveSpec::maximize("margin"))
            .inputs(&input)
            .unwrap()
            .seed(42)
            .build()
            .unwrap();

        let result = pack.solve(&spec).unwrap();
        let output: PricingGuardrailsOutput = result.plan.plan_as().unwrap();

        // All products should meet minimum margin (or be constrained by bounds)
        for rec in &output.recommendations {
            // Either meets target or is constrained by bounds
            assert!(rec.margin_pct >= 30.0 || !rec.within_bounds);
        }
    }

    #[test]
    fn test_price_bounds_enforcement() {
        let pack = PricingGuardrailsPack;
        let mut input = create_test_input();
        input.products = vec![Product {
            product_id: "bounded".to_string(),
            name: "Bounded Product".to_string(),
            unit_cost: 80.0,
            current_price: None,
            price_bounds: Some(PriceBounds {
                min_price: 95.0,
                max_price: 105.0,
            }),
            competitor_prices: vec![],
            category: None,
        }];

        let spec = ProblemSpec::builder("test-bounds", "test-tenant")
            .objective(ObjectiveSpec::maximize("margin"))
            .inputs(&input)
            .unwrap()
            .seed(42)
            .build()
            .unwrap();

        let result = pack.solve(&spec).unwrap();
        let output: PricingGuardrailsOutput = result.plan.plan_as().unwrap();

        let rec = &output.recommendations[0];
        assert!(rec.recommended_price >= 95.0);
        assert!(rec.recommended_price <= 105.0);
    }

    #[test]
    fn test_competitive_pricing() {
        let pack = PricingGuardrailsPack;
        let mut input = create_test_input();
        input.margin_requirements.competitive_strategy = CompetitiveStrategy::PriceToBeat;
        input.margin_requirements.min_margin_pct = 10.0; // Lower margin to allow competitive pricing

        let spec = ProblemSpec::builder("test-competitive", "test-tenant")
            .objective(ObjectiveSpec::maximize("margin"))
            .inputs(&input)
            .unwrap()
            .seed(42)
            .build()
            .unwrap();

        let result = pack.solve(&spec).unwrap();
        let output: PricingGuardrailsOutput = result.plan.plan_as().unwrap();

        // First product has competitor data
        let rec = &output.recommendations[0];
        assert!(rec.competitive_position.competitor_count > 0);
        assert!(rec.competitive_position.avg_competitor_price.is_some());
    }

    #[test]
    fn test_calculate_confidence() {
        // Full compliance should give high confidence
        let output = PricingGuardrailsOutput {
            recommendations: vec![PricingRecommendation {
                product_id: "test".to_string(),
                recommended_price: 100.0,
                previous_price: None,
                price_change: None,
                price_change_pct: None,
                margin_pct: 25.0,
                markup_pct: 33.0,
                competitive_position: CompetitivePosition::default(),
                within_bounds: true,
                margin_target_met: true,
                rationale: "Test".to_string(),
            }],
            margin_analysis: MarginAnalysis::default(),
            guardrail_compliance: GuardrailCompliance {
                all_within_bounds: true,
                all_margins_met: true,
                competitive_position_achieved: true,
                violations: vec![],
            },
        };

        let confidence = calculate_confidence(&output);
        assert!(confidence >= 0.9);

        // Empty recommendations should give 0 confidence
        let empty_output = PricingGuardrailsOutput::no_valid_pricing("No products");
        let empty_confidence = calculate_confidence(&empty_output);
        assert_eq!(empty_confidence, 0.0);
    }
}