|
| 1 | +# Copyright 2017, Iain Dunning, Joey Huchette, Miles Lubin, and contributors |
| 2 | +# This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | +# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | +# file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 5 | +############################################################################# |
| 6 | +# JuMP |
| 7 | +# An algebraic modeling language for Julia |
| 8 | +# See https://github.com/jump-dev/JuMP.jl |
| 9 | +############################################################################# |
| 10 | + |
| 11 | +module MyApp |
| 12 | + |
| 13 | +using JuMP |
| 14 | +import HiGHS |
| 15 | +import Ipopt |
| 16 | + |
| 17 | +function run_highs_example() |
| 18 | + capacity = 10.0 |
| 19 | + profit = [5.0, 3.0, 2.0, 7.0, 4.0] |
| 20 | + weight = [2.0, 8.0, 4.0, 2.0, 5.0] |
| 21 | + model = Model(HiGHS.Optimizer) |
| 22 | + @variable(model, x[1:length(weight)], Bin) |
| 23 | + @constraint(model, weight' * x <= capacity) |
| 24 | + @objective(model, Max, profit' * x) |
| 25 | + optimize!(model) |
| 26 | + assert_is_solved_and_feasible(model) |
| 27 | + print(solution_summary(model)) |
| 28 | + return |
| 29 | +end |
| 30 | + |
| 31 | +function run_ipopt_example() |
| 32 | + model = Model(Ipopt.Optimizer) |
| 33 | + @variable(model, x) |
| 34 | + @variable(model, y) |
| 35 | + @objective(model, Min, (1 - x)^2 + 100 * (y - x^2)^2) |
| 36 | + optimize!(model) |
| 37 | + assert_is_solved_and_feasible(model) |
| 38 | + print(solution_summary(model)) |
| 39 | + return |
| 40 | +end |
| 41 | + |
| 42 | +function julia_main()::Cint |
| 43 | + solver = only(match(r"--solver=(.+)", only(ARGS))) |
| 44 | + if solver == "ipopt" |
| 45 | + run_ipopt_example() |
| 46 | + else |
| 47 | + @assert solver == "highs" |
| 48 | + run_highs_example() |
| 49 | + end |
| 50 | + return 0 |
| 51 | +end |
| 52 | + |
| 53 | +end |
0 commit comments