如何使用loco

时间:2017-07-06 16:37:01

标签: optimization clojure constraint-programming

我正在尝试使用loco进行优化的基本示例。

我有一个成本向量,其索引对应于多个时隙的整数值,并且希望最小化不同时隙子集的成本总和。

请参阅下面我的尝试,但由于所选插槽与费用之间没有“链接”,因此无效。

(def costs [10 10 20 20 30 30 40 40 10 10])

(let [slot-vars (for [i (range 5)] ($in [:slot i] 1 10))
      cost-vars (for [i (range 10)] ($in [:cost i] 10 40))]
  (solution
   (concat
    slot-vars
    cost-vars
    [($distinct (for [i (range 5)] [:slot i]))]
    (for [i (range 5)]
      ($= [:cost i] (get costs i))))
   :minimize (apply $+ (for [i (range 5)] [:slot i]))))

2 个答案:

答案 0 :(得分:2)

这不是答案,但我希望它可能有助于指出可能有所帮助的方向。这听起来像背包问题?

您可以找到最大值:

(def slots (for [i (range 10)] (keyword (str "slot-" i))))

(solution
   (concat
    (for [s slots] ($in s 0 1))
    [($in :total-weight 10 60)
     ($in :total-value 5 5)
     ($knapsack [10 10 20 20 30 30 40 40 10 10]
                (repeat 10 1)
                slots :total-weight :total-value)]))

假设你只能有5个插槽。

可以通过查看源代码并直接使用Choco库来编写最小化版本吗?

检查loco knapsack函数的来源。

答案 1 :(得分:2)

首先,我不完全确定我理解这个问题的关键点,因为显然解决方案是只取列表中的5个最小值。但是如果你想让loco这样做,我同意背包约束是一个方便的工具。与Mike在评论中所说的相反,我认为使用背包进行最小化并没有任何障碍。让权重都为1,并强制权重加起来为5,以便从10个槽中选择5个。我已经使用变量[:include i]来指示插槽i是否应该包含在子集中(1表示true,0表示false)。我们希望最小化向量的点积:包括变量和成本向量。

(def costs [10 10 20 20 30 30 40 40 10 10])
(def weights (repeat 10 1))

(def include-vars (for [i (range 10)] [:include i]))
(def include-constraints (for [i (range 10)] ($in [:include i] 0 1)))

(def model
  (concat
   include-constraints
   [($knapsack weights costs include-vars 5 :total)
    ($in :total 0 (apply + costs))]))

(solution model :minimize :total)

结果是:

{[:include 4] 0, [:include 6] 0, [:include 9] 1, [:include 1] 1, [:include 3] 0, [:include 8] 1, :total 60, [:include 0] 1, [:include 7] 0, [:include 2] 1, [:include 5] 0}