3
$\begingroup$

I've written the following code and it works, but my code syntax is inefficient. This is a linear optimization with binary (0,1) integer constraints. Instead of writing each Ri as between 0 and 1 and each Ri as an integer, is the a better way to code this as a group of Ri's that must all be either 0 or 1 and hence an integer? Below is my code. It works but not tidy!!

Code Using LinearOptimization

Budget = 30 R1 + 150 R2 + 300 R3 + 25 R4 + 85 R5 + 95 R6 + 435 R7;
LinearOptimization[-(0.80 R1 + 0.75 R2 + 0.56 R3 + 0.32 R4 + 
    0.25 R5 + 0.86 R6 + 0.93 R7), {0 <= Budget <= 500, 0 <= R1 <= 1, 
  0 <= R2 <= 1, 0 <= R3 <= 1, 0 <= R4 <= 1, 0 <= R5 <= 1, 
  0 <= R6 <= 1, 0 <= R7 <= 1}, {R1 \[Element] Integers, 
  R2 \[Element] Integers, R3 \[Element] Integers, 
  R4 \[Element] Integers, R5 \[Element] Integers, 
  R6 \[Element] Integers, R7 \[Element] Integers}]

Code Using Maximize

Budget = 30 R1 + 150 R2 + 300 R3 + 25 R4 + 85 R5 + 95 R6 + 435 R7;
m = Maximize[{0.80 R1 + 0.75 R2 + 0.56 R3 + 0.32 R4 + 0.25 R5 + 
    0.86 R6 + 0.93 R7,
   0 <= Budget <= 500, 0 <= R1 <= 1, 0 <= R2 <= 1, 0 <= R3 <= 1, 
   0 <= R4 <= 1, 0 <= R5 <= 1, 0 <= R6 <= 1, 0 <= R7 <= 1},
  {R1, R2, R3, R4, R5, R6, R7} \[Element] Integers]
Budget /. Flatten[Last[Last[{m}]]]
$\endgroup$

1 Answer 1

4
$\begingroup$

VectorLessEqual is your friend, Element on lists works too:

With[{Rs = {R1, R2, R3, R4, R5, R6, R7},
  Budget = 30 R1 + 150 R2 + 300 R3 + 25 R4 + 85 R5 + 95 R6 + 435 R7},
 LinearOptimization[-(0.80 R1 + 0.75 R2 + 0.56 R3 + 0.32 R4 + 
     0.25 R5 + 0.86 R6 + 0.93 R7),
  {0 <= Budget <= 500,
   VectorLessEqual[{0, Rs, 1}],
   Element[Rs, Integers]},
  Rs]]

(* {R1 -> 1, R2 -> 1, R3 -> 0, R4 -> 1, R5 -> 1, R6 -> 1, R7 -> 0} *)

I'd probably write the constants with dot products, though:

With[{Rs = {R1, R2, R3, R4, R5, R6, R7}},
 LinearOptimization[-{0.80, 0.75, 0.56, 0.32, 0.25, 0.86, 0.93} . Rs,
  {0 <= {30, 150, 300, 25, 85, 95, 435} . Rs <= 500,
   VectorLessEqual[{0, Rs, 1}],
   Element[Rs, Integers]},
  Rs]]
$\endgroup$
10
  • $\begingroup$ kirma: Many thanks! I did not know about VectorLessEqual ... thank you for sharing your insights! $\endgroup$
    – user42700
    Dec 28, 2022 at 16:16
  • $\begingroup$ @PRG It's actually a slightly new operation, introduced in v12 (2019). It is definitely nicely concise for forcing a set of variables to a range. $\endgroup$
    – kirma
    Dec 28, 2022 at 16:18
  • $\begingroup$ Getting this error. I'm running v13.1 $\endgroup$
    – user42700
    Dec 28, 2022 at 16:27
  • $\begingroup$ During evaluation of In[1]:= LinearOptimization::nvcnstr: The constraint 0<=30 R1+150 R2+300 R3+25 R4+85 R5+95 R6+435 R7<=500 did not explicitly have any of the variables {R1|R2|R3|R4|R5|R6|R7} and will be disregarded. Out[1]= LinearOptimization[-0.8 R1 - 0.75 R2 - 0.56 R3 - 0.32 R4 - 0.25 R5 - 0.86 R6 - 0.93 R7, {0 <= 30 R1 + 150 R2 + 300 R3 + 25 R4 + 85 R5 + 95 R6 + 435 R7 <= 500, 0 [VectorLessEqual] {R1, R2, R3, R4, R5, R6, R7} [VectorLessEqual] 1}, (R1 | R2 | R3 | R4 | R5 | R6 | R7) [Element] Integers] $\endgroup$
    – user42700
    Dec 28, 2022 at 16:28
  • $\begingroup$ @PRG Interesting that it even worked before! I'm looking at it on v13.1 and I'll fix it in a moment. $\endgroup$
    – kirma
    Dec 28, 2022 at 16:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.