@@ -102,6 +102,36 @@ mutable struct _NLConstraintInfo
102
102
end
103
103
end
104
104
105
+ """
106
+ Env(
107
+ params::Dict{String,Any} = Dict{String,Any}();
108
+ started::Bool = true,
109
+ )
110
+
111
+ Create a new Gurobi environment object.
112
+
113
+ Optionally pass a `params` dictionary which sets parameters for the created
114
+ environment before starting.
115
+
116
+ ## kwargs
117
+
118
+ The extra keyword argument `started` delays starting the environment if set to
119
+ `false`.
120
+
121
+ ## Example
122
+
123
+ ```julia
124
+ using JuMP, Gurobi
125
+ const GRB_ENV = Gurobi.Env(
126
+ Dict(
127
+ "ComputeServer" => "localhost:61000",
128
+ "OutputFlag" => 0,
129
+ );
130
+ started = true,
131
+ )
132
+ model = Model(() -> Gurobi.Optimizer(GRB_ENV))
133
+ ```
134
+ """
105
135
mutable struct Env
106
136
ptr_env:: Ptr{Cvoid}
107
137
# These fields keep track of how many models the `Env` is used for to help
@@ -110,34 +140,41 @@ mutable struct Env
110
140
finalize_called:: Bool
111
141
attached_models:: Int
112
142
113
- function Env (;
143
+ function Env (
144
+ params:: Union{Nothing,Dict{String,Any}} = nothing ;
145
+ started:: Bool = true ,
146
+ # These kwargs are provided for legacy backwards compatibility
114
147
output_flag:: Int = 1 ,
115
148
memory_limit:: Union{Nothing,Real} = nothing ,
116
- started:: Bool = true ,
117
149
)
118
150
a = Ref {Ptr{Cvoid}} ()
119
151
ret = GRBemptyenv (a)
120
152
env = new (a[], false , 0 )
121
153
_check_ret (env, ret)
122
- ret = GRBsetintparam (env. ptr_env, GRB_INT_PAR_OUTPUTFLAG, output_flag)
123
- _check_ret (env, ret)
124
- if _GUROBI_VERSION >= v " 9.5.0" && memory_limit != = nothing
125
- ret = GRBsetdblparam (env, GRB_DBL_PAR_MEMLIMIT, memory_limit)
126
- _check_ret (env, ret)
127
- end
128
- if started
129
- ret = GRBstartenv (env. ptr_env)
130
- end
131
154
finalizer (env) do e
132
155
e. finalize_called = true
133
156
if e. attached_models == 0
134
157
# Only finalize the model if there are no models using it.
135
158
GRBfreeenv (e. ptr_env)
136
159
e. ptr_env = C_NULL
137
160
end
161
+ return
162
+ end
163
+ if params === nothing
164
+ # These two parameters are provided for backwards compability
165
+ _set_param (env. ptr_env, GRB_INT_PAR_OUTPUTFLAG, 1 )
166
+ if _GUROBI_VERSION >= v " 9.5.0" && memory_limit != = nothing
167
+ _set_param (env. ptr_env, GRB_DBL_PAR_MEMLIMIT, memory_limit)
168
+ end
169
+ else
170
+ for (param_name, value) in params
171
+ _set_param (env. ptr_env, param_name, value)
172
+ end
173
+ end
174
+ if started
175
+ ret = GRBstartenv (env. ptr_env)
176
+ _check_ret (env, ret)
138
177
end
139
- # Even if the loadenv fails, the pointer is still valid.
140
- _check_ret (env, ret)
141
178
return env
142
179
end
143
180
end
@@ -170,22 +207,11 @@ function Env(
170
207
server_password:: Union{String,Nothing} = nothing ;
171
208
started:: Bool = true ,
172
209
)
173
- env = Env (; started = false )
174
- ret = GRBsetstrparam (env. ptr_env, GRB_STR_PAR_COMPUTESERVER, server_address)
175
- _check_ret (env, ret)
210
+ params = Dict {String,Any} (GRB_STR_PAR_COMPUTESERVER => server_address)
176
211
if server_password != = nothing
177
- ret = GRBsetstrparam (
178
- env. ptr_env,
179
- GRB_STR_PAR_SERVERPASSWORD,
180
- server_password,
181
- )
182
- _check_ret (env, ret)
212
+ params[GRB_STR_PAR_SERVERPASSWORD] = server_password
183
213
end
184
- if started
185
- ret = GRBstartenv (env. ptr_env)
186
- _check_ret (env, ret)
187
- end
188
- return env
214
+ return Env (params; started)
189
215
end
190
216
191
217
Base. cconvert (:: Type{Ptr{Cvoid}} , x:: Env ) = x
@@ -685,6 +711,11 @@ function MOI.set(model::Optimizer, raw::MOI.RawOptimizerAttribute, value)
685
711
env = GRBgetenv (model)
686
712
param = raw. name
687
713
model. params[param] = value
714
+ _set_param (env, param, value)
715
+ return
716
+ end
717
+
718
+ function _set_param (env, param:: String , value)
688
719
param_type = GRBgetparamtype (env, param)
689
720
ret = if param_type == - 1
690
721
throw (MOI. UnsupportedAttribute (MOI. RawOptimizerAttribute (param)))
0 commit comments