Skip to content

Commit

Permalink
Merge pull request #4 from focusenergy/dot
Browse files Browse the repository at this point in the history
Make dot work
  • Loading branch information
ianfiske authored Nov 1, 2018
2 parents 750be91 + 86e1694 commit f103144
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/LinearFractional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ include("parseexpr.jl")
getvalue(x::LinearFractionalVariable) = getvalue(x.var)/getvalue(x.model.t)
getvalue(aff::LinearFractionalAffExpr) = getvalue(aff.afftrans)/getvalue(aff.t)

# Need to define Base.one to get dot() for free, but I don't think we can
# do this without knowing the model (to get t)
# Base.one(::Type{LinearFractionalVariable}) = LinearFractionalAffExpr(Variable[],Float64[],1.0)
Base.one(::Type{LinearFractionalVariable}) = 1.0
Base.one(::LinearFractionalVariable) = one(LinearFractionalVariable)
Base.sum(xs::Array{LinearFractionalAffExpr}) = LinearFractionalAffExpr(sum(x.afftrans for x in xs), xs[1].t)
function Base.sum(xs::Array{LinearFractionalAffExpr}, dim)
s = sum(collect(x.afftrans for x in xs), dim)
Expand Down
15 changes: 15 additions & 0 deletions test/arrayvars.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,18 @@ end
@test getvalue(xm) getvalue(x)
@test getobjectivevalue(lfp) getobjectivevalue(m)/5.0
end


@testset "Misc array features" begin
## dot should work
lfp = LinearFractionalModel(solver=ClpSolver())
a = [-2, 0]
x = @variable(lfp, [i=1:2], basename="x", lowerbound=0)
@constraint(lfp, x[2] <= 6)
@constraint(lfp, -x[1] + x[2] <= 4)
@constraint(lfp, 2x[1] + x[2] <= 14)
@numerator(lfp, :Min, dot(a, x) + 2)
@denominator(lfp, x[1] + 3x[2] + 4)
@test solve(lfp) == :Optimal
@test all(getvalue(x) .≈ [7.0, 0.0])
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Test
using LinearFractional
using LinearAlgebra
using Clp
using JuMP

Expand Down

0 comments on commit f103144

Please sign in to comment.