rust-or / good_lp

Linear Programming for Rust, with a user-friendly API. This crate allows modeling LP problems, and lets you solve them with various solvers.
https://crates.io/crates/good_lp
MIT License
238 stars 34 forks source link

`From<Expression> not implemented for f64` #15

Closed keegit closed 2 years ago

keegit commented 2 years ago

Given this code, where batt_var.b_energy: Vec<Vec<Variable>>:

        for (bat, b_energy_row) in batt_var.b_energy.iter().enumerate() {
            for (t, b_energy) in b_energy_row.iter().enumerate() {
                let mut energy_init: Expression = match bat {
                    0 => b_energy.clone(),
                    1 => Expression::from_other_affine(st_soc * e_max / 100.0),
                    _ => Expression::from_other_affine(b_energy_row[t-1]),
                };
                let bat_pc = Expression::from_other_affine(batt_var.bat_pc[bat][t]);
                energy_init.mul(bat_pc);
            }
        }

I get the following error output:

error[E0277]: the trait bound `f64: From<Expression>` is not satisfied
   --> src/model/constr.rs:449:51
    |
449 |                 energy_init.mul(bat_pc);
    |                                         --- ^^^^^^ the trait `From<Expression>` is not implemented for `f64`
    |                                         |
    |                                         required by a bound introduced by this call
    |

Update:

I have fixed my issue, but I am keeping this because it seems strange.

lovasoa commented 2 years ago

You are trying to call this function, which expects an f64, with a parameter that is an Expression : https://docs.rs/good_lp/1.3.2/good_lp/struct.Expression.html#impl-Mul%3CN%3E

lovasoa commented 2 years ago

Just as a reminder: the product of two linear expressions is not a linear expression.

lovasoa commented 2 years ago

You probably meant energy_init *= batt_var.bat_pc[bat][t]

keegit commented 2 years ago

:facepalm: Thank you.