我必须计算这个多目标问题的理想向量。我不能如何访问@objective中functs_BK1()的第一个和第二个函数。 任何想法如何使所有动态和支持n函数?
using JuMP
using Ipopt
function functs_BK1(x::Vector)
f = zeros(2)
f[1] = x[1]^2 + x[2]^2
f[2] = (x[1]-5.0)^2 + (x[2]-5.0)^2
return f
end
function bounds_BK1()
return ([-5.0;-5.0],[10.0;10.0])
end
m = Model(solver=IpoptSolver(print_level=0))
@variable(m, x[i=1:2])
@NLexpression(m, F1, functs_BK1()[1]) #<--- Problem is here
@NLexpression(m, F2, functs_BK1()[2]) #<--- here
@constraint(m, x .>= bounds_BK1()[1])
@constraint(m, x .<= bounds_BK1()[2])
s=solve(m)
答案 0 :(得分:1)
在您的函数zoo.to.data.frame
中,您已将# define environment
data <- new.env()
# Load Stock data in environment
getSymbols(symbols,from="2016-01-01", env=data)
SignalCalculator2 <- function(x) {
# Assumes first colname format is still [tickername].Open
tickername <- strsplit(colnames(x)[1], "\\.")[[1]][1]
# Adjusted Price:
price <- Ad(x)
colnames(price) <- "Price"
adx <- ADX(HLC(x), n = 14)
# You dont want this column in your table:
adx$DX <- NULL
res <- merge(price, adx)
# res is an xts object. Now convert to data.frame
df_res <- data.frame("Date" = index(res), "Ticker" = tickername, coredata(res))
df_res
}
# data is an environment, not a list, so can use eapply (similar to lapply):
out <- eapply(env = data, FUN = SignalCalculator2)
out2 <- do.call(rbind, out)
# If you want to order rows by date:
out2 <- out2[order(out2$Date),]
# Optional tidy up:
rownames(out2) <- NULL
> tail(out2)
# Date Ticker Price DIp DIn ADX
# 164 2016-08-25 AAMC 12.72 17.67766 21.28021 11.31483
# 330 2016-08-25 AAU 1.42 22.36896 23.64023 30.50243
# 165 2016-08-26 AAMC 12.80 16.48608 21.07136 11.37868
# 331 2016-08-26 AAU 1.36 23.02102 21.80518 28.51742
# 166 2016-08-29 AAMC 13.15 15.75814 20.14096 11.43797
# 332 2016-08-29 AAU 1.43 21.63012 22.30030 26.58943
定义为functs_BK1
值的数组,而f
宏必须从JuMP变量中构建所需的表达式它的第三个论点。如JuMP documentation for Nonlinear Expressions中所述,所有非线性表达式都必须在Float64
宏内定义,@NLexpression
和@NLexpression
对象当前不能在AffExpr
内使用}宏。
以下命令集导致通过QuadExpr
操作找到解决方案:
@NLexpression
我认为您目前无法在solve
中构建一系列表达式,以后会传递给julia> using JuMP
julia> using Ipopt
julia> m = Model(solver=IpoptSolver(print_level=0))
Feasibility problem with:
* 0 linear constraints
* 0 variables
Solver is Ipopt
julia> @variable(m, x[i=1:2])
2-element Array{JuMP.Variable,1}:
x[1]
x[2]
julia> function bounds_BK1()
return ([-5.0;-5.0],[10.0;10.0])
end
bounds_BK1 (generic function with 1 method)
julia> @NLexpression(m, F1, x[1]^2 + x[2]^2)
JuMP.NonlinearExpression(Feasibility problem with:
* 0 linear constraints
* 2 variables
Solver is Ipopt,1)
julia> @NLexpression(m, F2, (x[1]-5.0)^2 + (x[2]-5.0)^2)
JuMP.NonlinearExpression(Feasibility problem with:
* 0 linear constraints
* 2 variables
Solver is Ipopt,2)
julia> @constraint(m, x .>= bounds_BK1()[1])
2-element Array{JuMP.ConstraintRef{JuMP.Model,JuMP.GenericRangeConstraint{JuMP.GenericAffExpr{Float64,JuMP.Variable}}},1}:
x[1] ≥ -5
x[2] ≥ -5
julia> @constraint(m, x .<= bounds_BK1()[2])
2-element Array{JuMP.ConstraintRef{JuMP.Model,JuMP.GenericRangeConstraint{JuMP.GenericAffExpr{Float64,JuMP.Variable}}},1}:
x[1] ≤ 10
x[2] ≤ 10
julia> s = solve(m)
******************************************************************************
This program contains Ipopt, a library for large-scale nonlinear optimization.
Ipopt is released as open source code under the Eclipse Public License (EPL).
For more information visit http://projects.coin-or.org/Ipopt
******************************************************************************
:Optimal
julia> getvalue(x)
2-element Array{Float64,1}:
0.261454
0.261454
。目前,需要将JuMP变量的标量表达式直接作为functs_BK1
的参数传递。