Skip to content
New issue

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

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

Already on GitHub? # to your account

Uncaught ReferenceError: Solver is not defined #119

Open
Nick-Hope opened this issue Sep 8, 2021 · 2 comments
Open

Uncaught ReferenceError: Solver is not defined #119

Nick-Hope opened this issue Sep 8, 2021 · 2 comments

Comments

@Nick-Hope
Copy link

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

redblobgames commented Nov 16, 2021

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

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

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

No branches or pull requests

2 participants