Sin(x), cos(x) w/ Gurobi?

Can I use sinusoidal functions with JuMP/Gurobi? something like:

using JuMP, Gurobi
m=Model(Gurobi.Optimizer)
@variable(m, -2π <= x <= 2π)
@objective(m, Min, cos(x))
optimize!(m)

It seems like there’s a section on sine/cosine functions supported by Gurobi:

https://docs.gurobi.com/projects/optimizer/en/current/reference/c/model.html#c.GRBaddgenconstrSin

See GitHub - jump-dev/Gurobi.jl: A Julia interface to the Gurobi Optimizer

Otherwise, if you have Gurobi v12, there’s an experimental interface in ] add Gurobi#master.

It’s not officially released because there are still a few known bugs: Track nonlinear resultant vars outside of MOI by simonbowly · Pull Request #590 · jump-dev/Gurobi.jl · GitHub (But these are mostly a problem if you’re going to delete variables or constraints, or use some of the more advanced features of MathOptInterface.)

julia> using JuMP, Gurobi

julia> m=Model(Gurobi.Optimizer)
Set parameter LicenseID to value 890341
A JuMP Model
├ solver: Gurobi
├ objective_sense: FEASIBILITY_SENSE
├ num_variables: 0
├ num_constraints: 0
└ Names registered in the model: none

julia> @variable(m, -2π <= x <= 2π)
x

julia> @objective(m, Min, cos(x))
cos(x)

julia> optimize!(m)
Gurobi Optimizer version 12.0.0 build v12.0.0rc1 (mac64[x86] - Darwin 24.1.0 24B83)

CPU model: Intel(R) Core(TM) i5-8259U CPU @ 2.30GHz
Thread count: 4 physical cores, 8 logical processors, using up to 8 threads

Optimize a model with 0 rows, 3 columns and 0 nonzeros
Model fingerprint: 0x63accd21
Model has 1 general nonlinear constraint (1 nonlinear terms)
Variable types: 3 continuous, 0 integer (0 binary)
Coefficient statistics:
  Matrix range     [0e+00, 0e+00]
  Objective range  [1e+00, 1e+00]
  Bounds range     [6e+00, 6e+00]
  RHS range        [0e+00, 0e+00]
Presolve model has 1 nlconstr
Added 1 variables to disaggregate expressions.
Presolve time: 0.00s
Presolved: 9 rows, 5 columns, 21 nonzeros
Presolved model has 1 nonlinear constraint(s)

Solving non-convex MINLP

Variable types: 5 continuous, 0 integer (0 binary)
Found heuristic solution: objective 1.0000000

Root relaxation: objective -1.000000e+00, 4 iterations, 0.00 seconds (0.00 work units)

    Nodes    |    Current Node    |     Objective Bounds      |     Work
 Expl Unexpl |  Obj  Depth IntInf | Incumbent    BestBd   Gap | It/Node Time

     0     0   -1.00000    0    1    1.00000   -1.00000   200%     -    0s
     0     0   -1.00000    0    1    1.00000   -1.00000   200%     -    0s
     0     0   -1.00000    0    1    1.00000   -1.00000   200%     -    0s
H    0     0                      -0.9966721   -1.00000  0.33%     -    0s
     0     2   -1.00000    0    1   -0.99667   -1.00000  0.33%     -    0s
*    4     4               2      -0.9999769   -1.00000  0.00%   3.2    0s
H    6     3                      -1.0000000   -1.00000  0.00%   2.2    0s

Explored 7 nodes (18 simplex iterations) in 0.01 seconds (0.00 work units)
Thread count was 8 (of 8 available processors)

Solution count 4: -1 -0.999977 -0.996672 1 

Optimal solution found (tolerance 1.00e-04)
Best objective -1.000000000000e+00, best bound -1.000000000000e+00, gap 0.0000%

User-callback calls 145, time in user-callback 0.00 sec

julia> value(x)
3.1418746727522655
2 Likes

Great! Thank you so much!

1 Like