-
Notifications
You must be signed in to change notification settings - Fork 1
/
units.jl
277 lines (208 loc) · 7.51 KB
/
units.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
###
### UnitVariableRef
###
struct _UnitVariable{U<:Unitful.Units} <: JuMP.AbstractVariable
variable::JuMP.ScalarVariable
unit::U
end
"""
UnitVariableRef(::JuMP.VariableRef, ::Unitful.Units)
A type that wraps a `VariableRef` with a `Uniful.Units`.
"""
struct UnitVariableRef{U<:Unitful.Units} <: JuMP.AbstractVariableRef
variable::JuMP.VariableRef
unit::U
end
JuMP.owner_model(x::UnitVariableRef) = JuMP.owner_model(x.variable)
Unitful.unit(x::UnitVariableRef) = x.unit
function Base.zero(::Type{UnitVariableRef{U}}) where {U}
return zero(UnitAffExpr{U})
end
function Base.one(::Type{UnitVariableRef{U}}) where {U}
return one(UnitAffExpr{U})
end
Base.show(io::IO, x::UnitVariableRef) = print(io, "$(x.variable) [$(x.unit)]")
function JuMP.build_variable(
::Function,
info::JuMP.VariableInfo,
unit::U,
) where {U<:Unitful.Units}
return _UnitVariable{U}(JuMP.ScalarVariable(info), unit)
end
function JuMP.add_variable(
model::Model,
x::_UnitVariable{U},
name::String,
) where {U}
variable = JuMP.add_variable(model, x.variable, name)
return UnitVariableRef{U}(variable, x.unit)
end
function JuMP.value(x::UnitVariableRef; result::Int = 1)
return Unitful.Quantity(JuMP.value(x.variable; result), x.unit)
end
JuMP.name(x::UnitVariableRef) = JuMP.name(x.variable)
function JuMP.set_name(x::UnitVariableRef, s::String)
return JuMP.set_name(x.variable, s)
end
function JuMP.set_start_value(x::UnitVariableRef, value::Unitful.Quantity)
val = Unitful.uconvert(x.unit, value)
return JuMP.set_start_value(x.variable, Unitful.ustrip(val))
end
function JuMP.set_start_value(x::UnitVariableRef, value::Real)
return error("Start value for unit variable must be given with units")
end
function JuMP.start_value(x::UnitVariableRef)
return Unitful.Quantity(JuMP.start_value(x.variable), x.unit)
end
JuMP.has_lower_bound(x::UnitVariableRef) = JuMP.has_lower_bound(x.variable)
function JuMP.lower_bound(x::UnitVariableRef)
return Unitful.Quantity(JuMP.lower_bound(x.variable), x.unit)
end
function JuMP.set_lower_bound(x::UnitVariableRef, lower::Unitful.Quantity)
val = Unitful.uconvert(x.unit, lower)
return JuMP.set_lower_bound(x.variable, Unitful.ustrip(val))
end
function JuMP.set_lower_bound(x::UnitVariableRef, lower::Real)
return error("Lower bound of unit variable must be given with units")
end
function JuMP.delete_lower_bound(x::UnitVariableRef)
return JuMP.delete_lower_bound(x.variable)
end
JuMP.LowerBoundRef(x::UnitVariableRef) = JuMP.LowerBoundRef(x.variable)
JuMP.has_upper_bound(x::UnitVariableRef) = JuMP.has_upper_bound(x.variable)
function JuMP.upper_bound(x::UnitVariableRef)
return Unitful.Quantity(JuMP.upper_bound(x.variable), x.unit)
end
function JuMP.set_upper_bound(x::UnitVariableRef, upper::Unitful.Quantity)
val = Unitful.uconvert(x.unit, upper)
return JuMP.set_upper_bound(x.variable, Unitful.ustrip(val))
end
function JuMP.set_upper_bound(x::UnitVariableRef, upper::Real)
return error("Upper bound of unit variable must be given with units")
end
function JuMP.delete_upper_bound(x::UnitVariableRef)
return JuMP.delete_upper_bound(x.variable)
end
JuMP.UpperBoundRef(x::UnitVariableRef) = JuMP.UpperBoundRef(x.variable)
JuMP.is_fixed(x::UnitVariableRef) = JuMP.is_fixed(x.variable)
function JuMP.fix_value(x::UnitVariableRef)
return Unitful.Quantity(JuMP.fix_value(x.variable), x.unit)
end
function JuMP.fix(x::UnitVariableRef, value::Unitful.Quantity)
val = Unitful.uconvert(x.unit, value)
return fix(x.variable, Unitful.ustrip(val))
end
function JuMP.fix(x::UnitVariableRef, value::Real)
return error("Fix value of unit value must be given with units")
end
JuMP.unfix(x::UnitVariableRef) = JuMP.unfix(x.variable)
JuMP.FixRef(x::UnitVariableRef) = JuMP.FixRef(x.variable)
JuMP.is_integer(x::UnitVariableRef) = JuMP.is_integer(x.variable)
JuMP.set_integer(x::UnitVariableRef) = JuMP.set_integer(x.variable)
JuMP.unset_integer(x::UnitVariableRef) = JuMP.unset_integer(x.variable)
JuMP.IntegerRef(x::UnitVariableRef) = JuMP.IntegerRef(x.variable)
JuMP.is_binary(x::UnitVariableRef) = JuMP.is_binary(x.variable)
JuMP.set_binary(x::UnitVariableRef) = JuMP.set_binary(x.variable)
JuMP.unset_binary(x::UnitVariableRef) = JuMP.unset_binary(x.variable)
JuMP.BinaryRef(x::UnitVariableRef) = JuMP.BinaryRef(x.variable)
###
### UnitExpression
###
"""
UnitExpression(::JuMP.AbstractJuMPScalar, ::Unitful.Units)
A type that wraps an `AbstractJuMPScalar` with a `Uniful.Units`.
"""
struct UnitExpression{A<:JuMP.AbstractJuMPScalar,U<:Unitful.Units} <:
JuMP.AbstractJuMPScalar
expr::A
unit::U
end
Base.show(io::IO, x::UnitExpression) = print(io, "$(x.expr) [$(x.unit)]")
Unitful.unit(x::UnitExpression) = x.unit
function Base.:(==)(x::UnitExpression, y::UnitExpression)
return x.expr == y.expr && x.unit == y.unit
end
JuMP.moi_function(x::UnitExpression) = JuMP.moi_function(x.expr)
expr(x) = x
expr(x::UnitExpression) = x.expr
function JuMP.moi_function(func::AbstractVector{<:AbstractJuMPScalar})
func = [expr(f) for f in func]
return JuMP.moi_function(func)
end
function JuMP.check_belongs_to_model(x::UnitExpression, model::AbstractModel)
return JuMP.check_belongs_to_model(x.expr, model)
end
Unitful.uconvert(::U, x::UnitExpression{A,U}) where {A,U} = x
function Unitful.uconvert(unit::Unitful.Units, x::UnitExpression)
factor = Unitful.ustrip(Unitful.uconvert(unit, Unitful.Quantity(1, x.unit)))
return UnitExpression(factor * x.expr, unit)
end
function JuMP.value(x::UnitExpression; result::Int = 1)
return Unitful.Quantity(JuMP.value(x.expr; result = result), x.unit)
end
function JuMP.value(xvec::AbstractVector{<:AbstractJuMPScalar}; result::Int = 1)
return [JuMP.value(x; result = result) for x in xvec]
end
###
### UnitConstraintRef
###
struct _UnitConstraint{U<:Unitful.Units} <: AbstractConstraint
constraint::ScalarConstraint
unit::U
end
"""
UnitConstraintRef(::JuMP.ConstraintRef, ::Unitful.Units)
A type that wraps an `ConstraintRef` with a `Uniful.Units`.
"""
struct UnitConstraintRef{U<:Unitful.Units}
constraint::ConstraintRef
unit::U
end
Unitful.unit(c::UnitConstraintRef) = c.unit
function Base.show(io::IO, c::UnitConstraintRef)
return print(io, "$(c.constraint) [$(c.unit)]")
end
function JuMP.name(c::UnitConstraintRef)
return JuMP.name(c.constraint)
end
function JuMP.set_name(c::UnitConstraintRef, name)
return JuMP.set_name(c.constraint, name)
end
function JuMP.check_belongs_to_model(c::_UnitConstraint, model::AbstractModel)
return JuMP.check_belongs_to_model(c.constraint, model)
end
function JuMP.dual(c::UnitConstraintRef)
return Unitful.Quantity(JuMP.dual(c.constraint), Unitful.unit(1 / c.unit))
end
function JuMP.shadow_price(c::UnitConstraintRef)
return Unitful.Quantity(
JuMP.shadow_price(c.constraint),
Unitful.unit(1 / c.unit),
)
end
function JuMP.build_constraint(
_error::Function,
expr::UnitExpression{A,U},
set::MOI.AbstractScalarSet,
) where {A,U}
return _UnitConstraint{U}(
JuMP.build_constraint(_error, expr.expr, set),
expr.unit,
)
end
function JuMP.build_constraint(
_error::Function,
uexpr::UnitExpression,
set::MOI.AbstractScalarSet,
unit::Unitful.Units,
)
return JuMP.build_constraint(_error, Unitful.uconvert(unit, uexpr), set)
end
function JuMP.add_constraint(
model::Model,
c::_UnitConstraint{U},
name::String,
) where {U}
constraint = JuMP.add_constraint(model, c.constraint, name)
return UnitConstraintRef{U}(constraint, c.unit)
end