-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathchange_form.jl
More file actions
170 lines (161 loc) · 4.26 KB
/
change_form.jl
File metadata and controls
170 lines (161 loc) · 4.26 KB
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
# Copyright (c) 2019: Joaquim Dias Garcia, and contributors
#
# Use of this source code is governed by an MIT-style license that can be found
# in the LICENSE.md file or at https://opensource.org/licenses/MIT.
function change_form(::Type{LPForm{T,AT,VT}}, lp::LPForm) where {T,AT,VT}
return LPForm{T,AT,VT}(
lp.sense,
lp.c,
lp.A,
lp.c_lb,
lp.c_ub,
lp.v_lb,
lp.v_ub,
)
end
function change_form(
::Type{LPForm{T,AT,VT}},
lp::LPGeometricForm{T},
) where {T,AT,VT}
return LPForm{T,AT,VT}(
lp.sense,
lp.c,
lp.A,
fill(typemin(T), length(lp.b)),
lp.b,
fill(typemin(T), length(lp.c)),
fill(typemax(T), length(lp.c)),
)
end
function change_form(
::Type{LPForm{T,AT,VT}},
lp::LPSolverForm{T},
) where {T,AT,VT}
c_lb = fill(typemin(T), length(lp.b))
c_ub = fill(typemax(T), length(lp.b))
for i in eachindex(lp.b)
if lp.senses[i] == LESS_THAN
c_ub[i] = lp.b[i]
elseif lp.senses[i] == GREATER_THAN
c_lb[i] = lp.b[i]
elseif lp.senses[i] == EQUAL_TO
c_lb[i] = lp.b[i]
c_ub[i] = lp.b[i]
else
error("invalid sign $(lp.senses[i])")
end
end
return LPForm{T,AT,VT}(lp.sense, lp.c, lp.A, c_lb, c_ub, lp.v_lb, lp.v_ub)
end
function change_form(
::Type{LPGeometricForm{T,AT,VT}},
lp::LPGeometricForm,
) where {T,AT,VT}
return LPGeometricForm{T,AT,VT}(lp.sense, lp.c, lp.A, lp.b)
end
function change_form(
::Type{LPGeometricForm{T,AT,VT}},
lp::LPForm{T},
) where {T,AT,VT}
has_c_upper = Int[]
has_c_lower = Int[]
sizehint!(has_c_upper, length(lp.c_ub))
sizehint!(has_c_lower, length(lp.c_ub))
for i in eachindex(lp.c_ub)
if _no_upper(lp.c_ub[i])
push!(has_c_upper, i)
end
if _no_lower(lp.c_lb[i])
push!(has_c_lower, i)
end
end
has_v_upper = Int[]
has_v_lower = Int[]
sizehint!(has_v_upper, length(lp.v_ub))
sizehint!(has_v_lower, length(lp.v_ub))
for i in eachindex(lp.v_ub)
if _no_upper(lp.v_ub[i])
push!(has_v_upper, i)
end
if _no_lower(lp.v_lb[i])
push!(has_v_lower, i)
end
end
Id = Matrix{T}(I, length(lp.c), length(lp.c))
new_A = vcat(
lp.A[has_c_upper, :],
-lp.A[has_c_lower, :],
Id[has_v_upper, :],
-Id[has_v_lower, :],
)
new_b = vcat(
lp.c_ub[has_c_upper],
-lp.c_lb[has_c_lower],
lp.v_ub[has_v_upper],
-lp.v_lb[has_v_lower],
)
return LPGeometricForm{T,AT,VT}(lp.sense, lp.c, new_A, new_b)
end
function change_form(
::Type{LPGeometricForm{T,AT,VT}},
lp::F,
) where {T,AT,VT,F<:AbstractLPForm{T}}
temp_lp = change_form(LPForm{T,AT,VT}, lp)
return change_form(LPGeometricForm{T,AT,VT}, temp_lp)
end
function change_form(
::Type{LPSolverForm{T,AT,VT}},
lp::LPSolverForm,
) where {T,AT,VT}
return LPSolverForm{T,AT,VT}(
lp.sense,
lp.c,
lp.A,
lp.b,
lp.senses,
lp.v_lb,
lp.v_ub,
)
end
function change_form(
::Type{LPSolverForm{T,AT,VT}},
lp::LPForm{T},
) where {T,AT,VT}
new_A = copy(lp.A)
senses = fill(LESS_THAN, length(lp.c_lb))
new_b = fill(NaN, length(lp.c_lb))
for i in eachindex(lp.c_lb)
if lp.c_lb[i] == lp.c_ub[i]
senses[i] = EQUAL_TO
new_b[i] = lp.c_lb[i]
elseif lp.c_lb[i] > -Inf && lp.c_ub[i] < Inf
senses[i] = GREATER_THAN
new_b[i] = lp.c_lb[i]
push!(new_b, lp.c_ub[i])
push!(sense, LESS_THAN)
new_A = vcat(new_A, lp.A[i, :])
elseif lp.c_lb[i] > -Inf
senses[i] = GREATER_THAN
new_b[i] = lp.c_lb[i]
elseif lp.c_ub[i] < Inf
senses[i] = LESS_THAN
new_b[i] = lp.c_ub[i]
end
end
return LPSolverForm{T,AT,VT}(
lp.sense,
lp.c,
new_A,
new_b,
senses,
lp.v_lb,
lp.v_ub,
)
end
function change_form(
::Type{LPSolverForm{T,AT,VT}},
lp::F,
) where {T,AT,VT,F<:AbstractLPForm{T}}
temp_lp = change_form(LPForm{T,AT,VT}, lp)
return change_form(LPSolverForm{T,AT,VT}, temp_lp)
end