尝试使用Variable()和Problem()解决R中的优化问题时,我在为变量施加整数条件时遇到了一些困难。
我知道使用Variable()时不是很直观,例如,如果要进行“ x”除法,则需要使用inv_pos(x)而不是1 / x(这会产生错误)。因此,我猜想有一种解决方法,但我找不到它。
p <- Variable(100)
objective <- Minimize(sum(p))
constraints <- list(p%%1==0) #this is what I expected to work
constraints <- list((p-1*floor(p))==0) #this is what I expected to work too
problem <- Problem(objective, constraints)
sol <- solve(problem, solver="ECOS") #the problem might not be optimzable
with ECOS but I have not reached to
that point
有没有办法强加这种“整数”条件?
答案 0 :(得分:1)
Int
可用于定义整数变量:
library(CVXR)
p <- Int(100)