Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Boundaries for the variables #106

Open
waeltut opened this issue Jan 21, 2020 · 7 comments
Open

Boundaries for the variables #106

waeltut opened this issue Jan 21, 2020 · 7 comments

Comments

@waeltut
Copy link

waeltut commented Jan 21, 2020

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

@waeltut
Copy link
Author

waeltut commented Jan 21, 2020

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
Copy link

ojame commented Mar 20, 2020

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
Copy link

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

@diligence-dev
Copy link

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
Copy link

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
Copy link
Owner

JWally commented Jul 26, 2020

@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
Copy link

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants