Skip to content

Sign Error on Dual values for >= Constraints #82

@PaulDL-RS

Description

@PaulDL-RS

Description: When using the method dual on a SolutionWithDual with a constraint expressed with >=, the returned dual value has the opposite sign of the one expected

I noticed this error using both Highs and Clarabel Solvers, the only ones implementing SolutionWithDual

Code Example:

The error is on constraint 3

use good_lp::{
    clarabel, constraint, highs, variable, DualValues, ProblemVariables, Solution,
    SolutionWithDual, SolverModel,
};

fn main() {
    let mut problem = ProblemVariables::new();
    let vars = problem.add_vector(variable().min(0), 4);
    let z = 4 * vars[0] + 6 * vars[1] + 7 * vars[2] + 8 * vars[3];
    let cst1 = constraint!(2 * vars[0] + 3 * vars[1] + 4 * vars[2] + 7 * vars[3] <= 4600);
    let cst2 = constraint!(3 * vars[0] + 4 * vars[1] + 5 * vars[2] + 6 * vars[3] <= 5000);
    let cst3 = constraint!(vars[3] >= 400);
    let cst4 = constraint!(vars[0] + vars[1] + vars[2] + vars[3] == 950);
    let mut problem = problem.maximise(z.clone()).using(highs);
    let c1 = problem.add_constraint(cst1);
    let c2 = problem.add_constraint(cst2);
    let c3 = problem.add_constraint(cst3);
    let c4 = problem.add_constraint(cst4);
    let mut solution = problem.solve().unwrap();

    for (idx, var) in vars.iter().enumerate() {
        println!("x{} = {}", idx, solution.value(*var));
    }
    let dual = solution.compute_dual();
    println!("shadow price cst1: {}", dual.dual(c1));
    println!("shadow price cst2: {}", dual.dual(c2));
    // The issue seems to only be for >= constraints
    println!("shadow price cst3: {}", dual.dual(c3)); // It should be -2
    println!("shadow price cst4: {}", dual.dual(c4));

    println!("objective value: {}", solution.eval(&z));
}

Output

x0 = 0
x1 = 400
x2 = 150
x3 = 400
shadow price cst1: 1
shadow price cst2: 0
shadow price cst3: 2 // Here should be -2
shadow price cst4: 3
objective value: 6650

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions