Message Boards Message Boards

0
|
5129 Views
|
6 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Use MaximalBy for the following optimization problem?

Posted 5 years ago

Hi, guys

Could you please help me to have a look at my code? It seems that the result is wrong. Thank you!

Clear["`*"];(*????????,??table*)
b = 2;
y01 = Table[
   N[Maximize[{-x^2*a - 3*x*b + 1, -(6/8) <= x <= -(4/8)}, x]], {a, 1,
     3}];
Print["q1=", y01]
y11 = Table[
   N[Maximize[{-x^3*a + 2*x*b - 1, -2 <= x <= 2}, x]], {a, 1, 3}];
Print["q2=", y11]
q = N[{y01, y11}]
Print["Ymax=", MaximalBy[q, First]]
POSTED BY: Shaoyan Robert
6 Replies
Posted 5 years ago

Simplified and closer to your original question example

The First value of each sublist is compared and the sublist with the largest first value is returned

l1 = {{1, a}, {2, b}};
MaximalBy[l1, First]
(* {{2, b}} *}

In your question q is not a list of sublists, it has 3 levels of { }. 3 is > 1 so the second sublist is returned.

l2 = {{{1, a}, {2, b}}, {{3, c}, {4, d}}};
MaximalBy[l2, First]
(* {{{3, c}, {4, d}}} *}

There is no pairwise comparison between the elements in the sublists and only the First element of the sublist is considered. This does not change the result

l2 = {{{1, a}, {20, b}}, {{3, c}, {4, d}}}
MaximalBy[l2, First]
(* {{{3, c}, {4, d}}} *)

Regarding #, &, /@, read this and this.

POSTED BY: Rohit Namjoshi
Posted 5 years ago

Thank you very much!

POSTED BY: Shaoyan Robert
Posted 5 years ago

It was not clear that you wanted to do a pairwise comparison between the two lists and pick the one with the largest first value. For that you need to combine them into pairs first and map.

q = Transpose[{y01, y11}];
MaximalBy[#, First] & /@ q

(* {{{4.9375, {x -> -0.75}}}, {{7., {x -> -2.}}}, {{15., {x -> -2.}}}} *)
POSTED BY: Rohit Namjoshi
Posted 5 years ago

Thank you, Namjoshi. Could you please tell me what do they mean, #, &, /@? In addtion, why my code is wrong? For example, it works with following calculation:

MaximalBy[{{a, 1}, {b, 1}, {a, 2}, {d, 1}, {b, 3}}, Last]
POSTED BY: Shaoyan Robert
Posted 5 years ago

You need to Join the two lists, not embed them in another list.

q = Join[y01, y11];
MaximalBy[q, First]

(* {{15., {x -> -2.}}} *)
POSTED BY: Rohit Namjoshi
Posted 5 years ago

Thank you! But the result that I want to derive is a list, i.e.,

{{{4.9375, {x -> -0.75}}, {7.,{x->-2.}},{15.,{x->-2.}}}}

because 4.937>2.0792, so we choose {4.9375,{x->-0.75}}, 4.375<7, so we choose {7., {x -> -2.}}, and we choose {15.,{x->-2.}} because 15>3.8125. That is to say, we choose them by comparing the first number of the list.

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

Group Abstract Group Abstract