jsLPSolver icon indicating copy to clipboard operation
jsLPSolver copied to clipboard

Boundaries for the variables

Open waeltut opened this issue 6 years ago • 7 comments

I could not find the way of adding upper and lower boundaries for variables. is it an attribute "max", "min" for each variable?

waeltut avatar Jan 21 '20 12:01 waeltut

As you can see in the figure below, this is a LP problem and to solve it you need to insert to the solver the upper and lower boundaries of the variables (last column in the table is the upper). problem

waeltut avatar Jan 21 '20 13:01 waeltut

That's what constraints are used for. For example:

       "constraints": {
            "oatmeal": {"max": 4},
        },
        "variables": {
            "oatmeal": {"oatmeal": 1 },
        },

Would ensure that the maximum value for the variable oatmeal would be 4.

ojame avatar Mar 20 '20 10:03 ojame

"oatmeal": {"oatmeal": 1 }, Can you tell me what does this line do?

ASIF-Mahmud1 avatar Apr 13 '20 14:04 ASIF-Mahmud1

This means for each oatmeal in the solution you want the oatmeal variable to increase by one. If you have "variables": { "x": {"y": 1 }, }, you can only use y in your constraints, not x. So have to use "oatmeal": {"oatmeal": 1 }, to limit the number of oatmeals.

diligence-dev avatar Apr 22 '20 08:04 diligence-dev

Maybe I'm missing something here, but I don't seem to be able to set a negative lower bound on a variable.

Is there always an implicit positivity constraint for every variable?

mbleichner avatar Jul 07 '20 12:07 mbleichner

@waeltut try this:

let model = {
    "name": "food",
    "optimize": "cost",
    "opType": "min",
    "constraints": {
        "oatmeal": {
            "max": 4
        },
        "chicken": {
            "max": 3
        },
        "egg": {
            "max": 2
        },
        "milk": {
            "max": 8
        },
        "muffin": {
            "max": 2
        },
        "soup": {
            "max": 2
        },
        "kcal": {
            "min": 2000
        },
        "prot": {
            "min": 55
        },
        "ca": {
            "min": 800
        }
    },
    "variables": {
        "oatmeal": {
            "kcal": 110,
            "prot": 4,
            "ca": 2,
            "cost": 0.1,
            "oatmeal": 1
        },
        "chicken": {
            "kcal": 205,
            "prot": 32,
            "ca": 12,
            "cost": 0.8,
            "chicken": 1
        },
        "egg": {
            "kcal": 160,
            "prot": 13,
            "ca": 54,
            "cost": 0.4,
            "egg": 1
        },
        "milk": {
            "kcal": 160,
            "prot": 8,
            "ca": 285,
            "cost": 0.3,
            "milk": 1
        },
        "muffin": {
            "kcal": 420,
            "prot": 4,
            "ca": 22,
            "cost": 0.7,
            "muffin": 1
        },
        "soup": {
            "kcal": 260,
            "prot": 14,
            "ca": 80,
            "cost": 0.6,
            "soup": 1
        }
    }
}

let results = solver.Solve(model);

// See what we have so far...
console.log(results);

// Roll it all up for proof:
let output = {};
Object.keys(results).forEach(function(rAttr){
    if(model.variables[rAttr]){
        
        let tmp = model.variables[rAttr];
        
        for(attr in tmp){
            output[attr] = output[attr] || 0;

            output[attr] += tmp[attr] * results[rAttr]
        }

    }
})

console.log(output);

JWally avatar Jul 26 '20 11:07 JWally

If I wanted to set the constraints this way, would it be correct?

"constraints": { "oatmeal": { "max": 4, "min": 2 },

jgbranco avatar Jan 11 '21 14:01 jgbranco