How to do basic optimization with loco

I am trying to make a basic optimization example using loco .

I have a cost vector whose index corresponds to the integer value of the number of slots and would like to minimize the amount of costs for a separate subset of slots.

Please see my attempt below, which does not work, because there is no “link” between the selected slots and costs.

(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]))))
+6
source share
2 answers

-, , , , , 5 . loco , , knapsack - . , , . 1 , 5, 5 10 . [:include i], , (1 true 0 false). : include .

(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}
+2

, , , . ?

max :

(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.

+2

All Articles