JWally / jsLPSolver

Simple OOP javaScript library to solve linear programs, and mixed integer linear programs
The Unlicense
420 stars 69 forks source link

Boundaries for the variables #106

Open waeltut opened 4 years ago

waeltut commented 4 years ago

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

waeltut commented 4 years ago

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

ojame commented 4 years ago

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.

ASIF-Mahmud1 commented 4 years ago

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

diligence-dev commented 4 years ago

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.

mbleichner commented 4 years ago

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?

JWally commented 4 years ago

@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);
jgbranco commented 3 years ago

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

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