Group Abstract Group Abstract

Message Boards Message Boards

0
|
5.3K Views
|
9 Replies
|
5 Total Likes
View groups...
Share
Share this post:

Help me maximize this, please.

Posted 10 years ago

Consider the following dot products. Thanks to tips from you guys i've managed to create the lists I need. Now all I need is a way to maximize the dot product shown below ( p:=a.as + b.bs ) under constrain that s:=a.an + b.bn <= 400 .

a = RandomSample[{0, 1, 0, 0, 0, 0}];
as = { 3, 1, 4, 5, 5, 8};
an = {100, 50, 100, 150, 50, 100};
ans = {a1, a2, a3, a4, a5, a6};

b = RandomSample[{0, 1, 1, 0, 0, 0, 0, 0}];
bs = {3, 5, 1, 6, 8, 5, 5, 10};
bn = {100, 200, 150, 50, 150, 200, 50, 150 };
bns = {b1, b2, b3, b4, b5, b6, b7, b8};

p := a.as + b.bs
s := a.an + b.bn

a.an + b.bn <= 400

I've tried the following

Maximize[{p , s <= 400}, p]

It returns

Maximize[{16, True}, 16]

saying 16 isn't a valid variable.

i think my function definition if off?

Any help is greatly appreciated

POSTED BY: bored dude
9 Replies
Posted 10 years ago

Ok Thank you I got it working after it caused errors. The source of the error was the string nature of the imported names. ToExpression seems to have fixed it.

Even then it wasn't working on the real problem because the names had special characters like "-" which treated it as a negative sign, and ' sign which just threw some error.

I eventually found out by using your code and copying and pasting the names, which pasted as string, (bingo), then looking over the list and finding things being out of order due to the hyphens and apostrophes (double bingo)

When I fixed these issues code ran no problems.

if I can I ask you guys a couple of more questions,

How would I select the cases where the mathematica has put 1 next to other than to scroll through a long list.

I've tried

max= NMaximize[{ans.as + bns.bs, constraints}, vars]
Select[max[[2]], #-> 1&]

Apparently that's not the correct syntax.

The goal is to make a list of the money spent (condition in the constraint ans.as+bns.bs <= 400), players chosen, points added up which is given by NMaximize in a long list.

Also from what I understand, you can seed NMaximize randomly as to change the outcome? Or does NMaximize always converge to the global optimum?

If you would like to see your code in action using the fixed excel sheet and my nb I've attached them both.

Attachments:
POSTED BY: bored dude
Posted 10 years ago

Thanks Henrik for the help. The problem I posted is a toy example of what I want to do. The actual problem deals with 4 lists of varying lengths which changes every week. Currently, 4 lists have the lengths of 23, 42, 12, and 52.

From the list 1 of 23 elements, I need to choose 1, out of list 2 of 42 elements, I need to choose 2, and out of list 3 of 12 elements, i need 1, and then out of list 4 of 52 elements, i need 3.

So to perform the permutations, I get 23 possibles from list 1, 861 from list 2, 12 from list 3, and 22100 from list 4. And to do the Outer function on these permutation froze up my mathematica.

I guess that wasn't too much of a surprise. We're dealing with 2386112*22100= 5251755600 combinations. Mathematica just didn't have enough resources on my computer. (vista, 64 bit, 4 gigs ram, core2duo 2 gighz ) Is there a way to get around these limitations?

This was the reason why I was trying to do it iteratively by randomly choosing the permutations, and see if I can define a function that include the random permutations and maximize the scores(a.as + b.bs )

And thanks Daniel for that code, but the score I need to maximize is the a.as+b.bs under the constraint. And the clear answer is a6 with 8 points for 100, and b5 with 8 points for 150, and b8 with 10 points for 100 for the total points of 26 points for 400.

Not sure where that 48 points come from.

The actual problem, as I said, consists of significantly longer lists, and at the end of the maximization, I need the identity of the elements that yielded the maximum dot product value.

If you like to see the real problem I'm working on, it's attached along with the excel sheet that I'm getting the data from.

That function "score" I wrote does work and if I run enough times, I could find various local maxima, I just can't do it using the Maximization function in Mathematica.

Attachments:
POSTED BY: bored dude

So it becomes an integer linear program. I suspect the linear relaxation is always optimal since it looks like the type of problem for which that will hold, but I am not certain after a brief look. Anyway, can be done as below. Note that all I changed from my last version is the constraints.

as = {3, 1, 4, 5, 5, 8};
an = {100, 50, 100, 150, 50, 100};
ans = {a1, a2, a3, a4, a5, a6};
bs = {3, 5, 1, 6, 8, 5, 5, 10};
bn = {100, 200, 150, 50, 150, 200, 50, 150};
bns = {b1, b2, b3, b4, b5, b6, b7, b8};
vars = Join[ans, bns];
constraints = 
  Flatten[{Total[ans] == 1, Total[bns] == 2, ans.an + bns.bn <= 400, 
    Map[0 <= # <= 1 &, vars]}];
NMaximize[{ans.as + bns.bs, constraints}, vars]

(* Out[69]= {26., {a1 -> 0., a2 -> 0., a3 -> 0., a4 -> 0., a5 -> 0., 
  a6 -> 1., b1 -> 0., b2 -> 0., b3 -> 0., b4 -> 0., b5 -> 1., 
  b6 -> 0., b7 -> 0., b8 -> 1.}} *)
POSTED BY: Daniel Lichtblau

Dear Bored Dude,

Please do no pollute groups unrelated to the subject of your post. This is what you have chosen:

  • Aerospace Engineering
  • Control Systems
  • Data Science
  • Electrical Engineering
  • Mathematics
  • Computer-Based Maths
  • Machine Learning

And this is what it is really about as we corrected:

  • Mathematics
  • Algebra
  • Wolfram Language

Please be mindful of other members of this community whose experience becomes much worth because of misclassified content.

POSTED BY: EDITORIAL BOARD
Posted 10 years ago

Ok Will watch where I post the questions to. I figured those groups would be relevant since it deals with converting data from excel into mathematica and using the data to maximize which a lot of those disciplines have experiences in. But I'll keep it to those groups you mentioned.

POSTED BY: bored dude

Wrong "variables", probably it is ans and bns that were intended. If so, can be done as below. Note that nonnegativity is needed, else it becomes unbounded.

as = {3, 1, 4, 5, 5, 8};
an = {100, 50, 100, 150, 50, 100};
ans = {a1, a2, a3, a4, a5, a6};
bs = {3, 5, 1, 6, 8, 5, 5, 10};
bn = {100, 200, 150, 50, 150, 200, 50, 150};
bns = {b1, b2, b3, b4, b5, b6, b7, b8};
vars = Join[ans, bns];
NMaximize[{ans.as + bns.bs, ans.an + bns.bn <= 400, 
  Thread[0 <= vars]}, vars]

(* {48., {a1 -> 0., a2 -> 0., a3 -> 0., a4 -> 0., a5 -> 0., a6 -> 0., 
  b1 -> 0., b2 -> 0., b3 -> 0., b4 -> 8., b5 -> 0., b6 -> 0., 
  b7 -> 0., b8 -> 0.}} *)
POSTED BY: Daniel Lichtblau

Oh yes, that is the better idea! I was wondering what bored dude might have had in mind with these ans and bns.

POSTED BY: Henrik Schachner
Posted 10 years ago

ans and bns are the index for the values as and an and bs and bn respectively, which needs to be pulled after the maximization has been done.

POSTED BY: bored dude

Hi bored dude,

in your example there is no variable according to which anything could be maximized. Instead you have a small set of items from which you can simply select the right one. I would do it like this:

a = Permutations[{0, 1, 0, 0, 0, 0}];
as = {3, 1, 4, 5, 5, 8};
an = {100, 50, 100, 150, 50, 100};

b = Permutations[{0, 1, 1, 0, 0, 0, 0, 0}];
bs = {3, 5, 1, 6, 8, 5, 5, 10};
bn = {100, 200, 150, 50, 150, 200, 50, 150};

(* all combinations from permutations a and b: *)
ab = Flatten[Outer[List, a, b, 1], 1];

(* members of 'ab' which fullfill the constrain: *)
constrOK = Select[ab, First[#].an + Last[#].bn <= 400 &];

(* finding the best combination simply by sorting: *)
{optA, optB} = Last@SortBy[constrOK, First[#].as + Last[#].bs &]
(* Out:  {{0,0,0,0,0,1},{0,0,0,0,1,0,0,1}} *)

Regards -- Henrik

POSTED BY: Henrik Schachner
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard