JWally / jsLPSolver

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

Uncaught ReferenceError: Solver is not defined #119

Open Nick-Hope opened 3 years ago

Nick-Hope commented 3 years ago

I would like to use jsLPSolver locally in a browser, not running Node.js. I'm using the line from the 'in browser through CDN' section of the Install section of the README.

I tried the exact example in the "Use" section, but get the error Uncaught ReferenceError: require is not defined. It looks like the require.js library might offer a workaround for that, for those not using Node.js. But then I found an example without using require() from JWally. However when I run that in the script shown below, I get the error Uncaught ReferenceError: Solver is not defined in my console.

How can I resolve this? Feels like I'm missing something fundamental.

<script src="https://unpkg.com/javascript-lp-solver/prod/solver.js"></script>
<script>
var solver = new Solver,
    results,
    model = {
    optimize: "profit",
    opType: "max",
    constraints: {
        "Costa Rican" : {max: 200},
        "Etheopian": {max: 330}
    },
    variables: {
        "Yusip": {"Costa Rican" : 0.5, "Etheopian": 0.5, profit: 3.5},
        "Exotic": {"Costa Rican" : 0.25, "Etheopian": 0.75, profit: 4}
    }
};

results = solver.solve(model);
console.log(results);
</script>
redblobgames commented 3 years ago

Yes, require is for node (or bundlers). You don't need it when using the CDN. When using the CDN, solver is a global so you don't have to require it. Try this:

<script src="https://unpkg.com/javascript-lp-solver/prod/solver.js"></script>
<script>
var results,
    model = {
    optimize: "profit",
    opType: "max",
    constraints: {
        "Costa Rican" : {max: 200},
        "Etheopian": {max: 330}
    },
    variables: {
        "Yusip": {"Costa Rican" : 0.5, "Etheopian": 0.5, profit: 3.5},
        "Exotic": {"Costa Rican" : 0.25, "Etheopian": 0.75, profit: 4}
    }
};

results = solver.Solve(model);
console.log(results);
</script>
Nick-Hope commented 2 years ago

Thank you @redblobgames and sorry for the very long delay in my response. That solved my issue and got it working.