Message Boards Message Boards

0
|
5311 Views
|
8 Replies
|
4 Total Likes
View groups...
Share
Share this post:

[?] Write a constraint on list elements when using Maximize ?

Posted 7 years ago

I would like to maximize a function which has a list as one of its arguments, and I want to put a constraint : a - p[[i]] +p [[i-1]] >0 which has to be true for all the list elements (i goes from 1 to 5). How do I write it ? Here is my code :

Module[
 {
  \[Alpha] = 100,
  \[Beta] = 10,
  c = 10,
  b = 0.05,
  e = 100,
  k = 500,
  a = 10000,
  t = 0.01 *Range[5],
  m,
  f
  },
 (*m=(a+ \[Alpha] p[[n-1]]- \[Alpha] c - (Length[t]-n) \
a)/(\[Alpha](b+(Length[t]-n+1)t[n]- Sum[t[[i]],{i,n+1, Length[t]}]));*)


 f[x0_, pList_] := (pList[[1]] - 
      c - (b + t[[1]]) x0) (a - \[Alpha]   pList[[1]]) +
   Sum[ (pList[[i]] - 
       c - (b + t[[i]]) x0) (a - \[Beta]  pList[[
         i]] + \[Beta]  pList[[i - 1]] - \[Alpha]   pList[[1]]), {i, 
     2, Length[t]}] - k (e - x0)^2;

 Maximize[
  {
   f[\[FormalX], Array[\[FormalP], Length[t]]],
     And @@ Thread[Array[\[FormalP], Length[t]] > 0] 
   },
  Flatten[{\[FormalX], Array[\[FormalP], Length[t]]}] 
  ]

 ]
POSTED BY: Narjes Kandil
8 Replies

Glad to help.

I find the [FormalP] variables hard to read especially when posting to the web-- I suggest using regular variable names unless you need to use them for your notation. Another approach is to use regular variable names like "p" or "pvars" and then replace them with the notation you want for display purposes.

for example:

expression = p[1] + p[2]
expression /. p-> \[FormalP]

In your case the entire problem is in a Module so you can't see the equations anyway so I would recommend using simple variable names.

POSTED BY: Neil Singer
Posted 7 years ago

I only saw the difference now, excuse me. Thank you very much for your help :)

POSTED BY: Narjes Kandil

you need to change the "p"'s to formalP's so they are the same variable.

See here --:

    0 && \[FormalP][2] > 0 && \[FormalP][3] > 0 && \[FormalP][4] > 
    0 && \[FormalP][5] > 0 && 
   10000 - 100 p[1] - 10 (p[1] + p[2]) > 0 && 

When I posted I indicated that I changed the formalp's to p's to make it easier to read. You need to change them back.

My last post had them changed back if you copy that...

POSTED BY: Neil Singer
Posted 7 years ago

Here's my code , I did like you told me

Module[
 {
  \[Alpha] = 100,
  \[Beta] = 10,
  c = 10,
  b = 0.05,
  e = 100,
  k = 500,
  a = 10000,
  t = 0.01 *Range[5],
  m,
  f
  },
 (*m=(a+ \[Alpha] p[[n-1]]- \[Alpha] c - (Length[t]-n) \
a)/(\[Alpha](b+(Length[t]-n+1)t[n]- Sum[t[[i]],{i,n+1, Length[t]}]));*)


 f[x0_, pList_] := (pList[[1]] - 
      c - (b + t[[1]]) x0) (a - \[Alpha] pList[[1]]) +
   Sum[ (pList[[i]] - 
       c - (b + t[[i]]) x0) (a - \[Beta]  pList[[
         i]] + \[Beta]  pList[[i - 1]] - \[Alpha] pList[[1]]), {i, 2, 
     Length[t]}] - k (e - x0)^2;

 Maximize[
  {
   f[\[FormalX], Array[\[FormalP], Length[t]]],
     And @@ Thread[Array[\[FormalP], Length[t]] > 0] && 
    And @@ Table[
      a - \[Beta] (p[i] + p[i - 1]) - \[Alpha] p[1] > 0, {i, 2, 
       Length[t]}] 
   },
  Flatten[{\[FormalX], Array[\[FormalP], Length[t]]}] 
  ]

 ]

and i have this output :

Maximize[{-500 (100 - \[FormalX])^2 + (10000 - 
      100 \[FormalP][1]) (-10 - 
      0.06 \[FormalX] + \[FormalP][1]) + (10000 - 90 \[FormalP][1] - 
      10 \[FormalP][2]) (-10 - 
      0.07 \[FormalX] + \[FormalP][2]) + (10000 - 100 \[FormalP][1] + 
      10 \[FormalP][2] - 10 \[FormalP][3]) (-10 - 
      0.08 \[FormalX] + \[FormalP][3]) + (10000 - 100 \[FormalP][1] + 
      10 \[FormalP][3] - 10 \[FormalP][4]) (-10 - 
      0.09 \[FormalX] + \[FormalP][4]) + (10000 - 100 \[FormalP][1] + 
      10 \[FormalP][4] - 10 \[FormalP][5]) (-10 - 
      0.1 \[FormalX] + \[FormalP][5]), \[FormalP][1] > 
    0 && \[FormalP][2] > 0 && \[FormalP][3] > 0 && \[FormalP][4] > 
    0 && \[FormalP][5] > 0 && 
   10000 - 100 p[1] - 10 (p[1] + p[2]) > 0 && 
   10000 - 100 p[1] - 10 (p[2] + p[3]) > 0 && 
   10000 - 100 p[1] - 10 (p[3] + p[4]) > 0 && 
   10000 - 100 p[1] - 10 (p[4] + p[5]) > 0}, {\[FormalX], \[FormalP][
   1], \[FormalP][2], \[FormalP][3], \[FormalP][4], \[FormalP][5]}]
POSTED BY: Narjes Kandil
Posted 7 years ago

it does not run and returns Maximize [...] numerical expression. Does this mean that I have a syntax error ? I'm actually on the trial version and i'm learning Mathematica.

POSTED BY: Narjes Kandil

What do you mean by "it doesn't work"? does it run but not return an answer, does it throw an error, or does it not run and returns your Maximize[...] expression?

It ran for me (but warned about convergence). This would be a problem in your equations.

Did you change my "p" s to formalP's?

I suggest removing everything from the Module[] for debugging so you can check each expression and then package it up in the Module.

Your other constraint looks like this:

And @@ Thread[Array[p, Length[t]] > 0] && 
 And @@ Table[
   a - \[Beta] (p[i] + p[i - 1]) - \[Alpha] p[1] > 0, {i, 2, 
    Length[t]}]

to get

p[1]>0&&p[2]>0&&p[3]>0&&p[4]>0&&p[5]>0&&10000-100 p[1]-10 (p[1]+p[2])>0&&10000-100 p[1]-10 (p[2]+p[3])>0&&10000-100 p[1]-10 (p[3]+p[4])>0&&10000-100 p[1]-10 (p[4]+p[5])>0

I ran this and it returned an answer:

Maximize[{f[\[FormalX], Array[\[FormalP], Length[t]]], 
  And @@ Thread[Array[\[FormalP], Length[t]] > 0] && 
   And @@ Table[
     a - \[Beta] (\[FormalP][i] + \[FormalP][
           i - 1]) - \[Alpha] \[FormalP][1] > 0, {i, 2, 5}]}, 
 Flatten[{\[FormalX], Array[\[FormalP], Length[t]]}]]

to get:

{1.66918*10^7, {\[FormalX] -> 96.3516, \[FormalP][1] -> 
   5.00841*10^-12, \[FormalP][2] -> 498.529, \[FormalP][3] -> 
   501.471, \[FormalP][4] -> 498.529, \[FormalP][5] -> 501.471}}
POSTED BY: Neil Singer
Posted 7 years ago

I tried (even by copying and pasting) but it doesn't work... I also wanted to reformulate the constraint to a - \[Beta] (p[i] + p[i - 1]) - \[Alpha] p[1] > 0but I have an error message saying p[1] is longer than depth of object. Can you help me with this ?

POSTED BY: Narjes Kandil

Narjes,

You can add the extra constraints on at the end of your constraint list:

Maximize[{f[\[FormalX], Array[\[FormalP], Length[t]]], 
  And @@ Thread[Array[\[FormalP], Length[t]] > 0] && 
   And @@ Table[
     a - \[FormalP][i] + \[FormalP][i - 1] > 0, {i, 2, Length[t]}]}, 
 Flatten[{\[FormalX], Array[\[FormalP], Length[t]]}]]

Constraints now look like this (dropping the formalp). Note the index of the second set must start at 2 because you do p[i-1]

And @@ Thread[Array[p, Length[t]] > 0] && 
 And @@ Table[a - p[i] + p[i - 1] > 0, {i, 2, Length[t]}]

To get

p[1] > 0 && p[2] > 0 && p[3] > 0 && p[4] > 0 && p[5] > 0 && 
 10000 + p[1] - p[2] > 0 && 10000 + p[2] - p[3] > 0 && 
 10000 + p[3] - p[4] > 0 && 10000 + p[4] - p[5] > 0
POSTED BY: Neil Singer
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