JWally / jsLPSolver

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

Implementing bounds for each variable #123

Closed m-abdullah-nabeel closed 1 year ago

m-abdullah-nabeel commented 1 year ago

I am working on a linear programming problem, which is about combining all available feedstuffs such that they cost very low (minimize the cost/price) and also maximize the availability of the nutrients of proteins and energy. All of this is doable, but I am wondering how I would be able to implement the bounds process as I am able to do it in Python scipy.optimize.linprog, where we can implement a minimum and maximum for each variable in the bounds variable as res = linprog(c, A_ub=A, b_ub=b, bounds=bounds=[x0_bounds, x1_bounds, x2_bounds, x3_bounds]) where each bound is like x0_bounds = (10, 50) and it implies the variable x0 can be minimum of 10 and maximum of 50, but if we go with the javascript solutions, I didn't find any implementation of this, as I would expect this to be

var javascriptLpSolver = require("javascript-lp-solver")

model = {
    "optimize": "price",
    "opType": "min",
    "constraints": {
        "cp": {"equal": 15},
        "me": {"equal": 2000},
        // is there a way we can say
        // the 'me' value of maize should not exceed x and should also be more than y, while also keeping all other constraints
        // like maize : {"min": 40}, maize: {"max": 70}
    },
    "variables": {
              "maize": {"cp": 8, "me": 3350, "price": 15},
              "riceTips": {"cp": 8, "me": 3450, "price": 16},
              "sorghum": {"cp": 12, "me": 3300, "price": 22},
              "csc": {"cp": 41, "me": 3500, "price": 90},
    }
}

console.log(javascriptLpSolver.Solve(model));
timreyn commented 1 year ago

For what it's worth, I've handled this by adding a constraint per variable:

var javascriptLpSolver = require("javascript-lp-solver")

model = {
    "optimize": "price",
    "opType": "min",
    "constraints": {
        "cp": {"equal": 15},
        "me": {"equal": 2000},
        "maize" : {"min": 40, "max": 70}
    },
    "variables": {
              "maize": {"cp": 8, "me": 3350, "price": 15, "maize": 1},
              "riceTips": {"cp": 8, "me": 3450, "price": 16, "riceTips": 1},
              "sorghum": {"cp": 12, "me": 3300, "price": 22, "sorghum": 1},
              "csc": {"cp": 41, "me": 3500, "price": 90, "csc": 1},
    }
}

console.log(javascriptLpSolver.Solve(model));
m-abdullah-nabeel commented 1 year ago

I tried a lot of things at this time, but using this same data, I am able to get results using the python library Scipy under linprog module and which has more flexibility, I found that this jsLPSolver doesn't currently provide a solution for constraints effectively. So I migrated to backend in python