Skip to content

Instantly share code, notes, and snippets.

@death
Last active December 6, 2020 12:22
Show Gist options
  • Save death/9b1be6ce8b34eb771c1d2ba6ef28210f to your computer and use it in GitHub Desktop.
Save death/9b1be6ce8b34eb771c1d2ba6ef28210f to your computer and use it in GitHub Desktop.
a blending problem
;;;; A Blending Problem
;;; See https://coin-or.github.io/pulp/CaseStudies/a_blending_problem.html
(defvar *blending-ingredients*
'((chicken :cost 0.013 :protein 0.100 :fat 0.080 :fibre 0.001 :salt 0.002)
(beef :cost 0.008 :protein 0.200 :fat 0.100 :fibre 0.005 :salt 0.005)
(mutton :cost 0.010 :protein 0.150 :fat 0.110 :fibre 0.003 :salt 0.007)
(rice :cost 0.002 :protein 0.000 :fat 0.010 :fibre 0.100 :salt 0.002)
(wheat :cost 0.005 :protein 0.040 :fat 0.010 :fibre 0.150 :salt 0.008)
(gel :cost 0.001 :protein 0.000 :fat 0.000 :fibre 0.000 :salt 0.000)))
(defun a-blending-problem (&optional (ing-list *blending-ingredients*))
(with-lp (problem "A Blending Problem" :minimize)
(flet ((col (indicator) (mapcar (lambda (ing) (getf (cdr ing) indicator)) ing-list)))
(let ((vars (mapcar (lambda (ing) (var (string-capitalize (car ing)) :continuous 0)) ing-list)))
(set-objective (dot (col :cost) vars))
(add-constraint (= vars 100) "Pcts Sum")
(add-constraint (>= (dot (col :protein) vars) 8) "Protein Req")
(add-constraint (>= (dot (col :fat) vars) 6) "Fat Req")
(add-constraint (<= (dot (col :fibre) vars) 2) "Fibre Req")
(add-constraint (<= (dot (col :salt) vars) 0.4) "Salt Req")
(solve)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment