Message Boards Message Boards

0
|
7666 Views
|
5 Replies
|
3 Total Likes
View groups...
Share
Share this post:

Dynamically creating variables for optimization problem?

Posted 8 years ago

I have a function that takes two input matrices an creates some series of variables to be used with Minimize. The function should generate the lazy equations dynamically based on those variables.

SPOP[p_, v_] := (
  (* expects |cols(p)| == |cols(v)| *)
  {vecy, matx}; (* declare local variables *)
  vecy = Table[y[i], {i, Length[p]}]; (* create y[i] for each elements of p *)


  )

Now I want to create an objective function and several constraints, So I need to know how to create an equation with these variables. My objective function may look like

y[1] + y[2] + .... y[Length[vecy]]

How can I generate these equations. I was trying the following which did not work

Expand[Sum[vecy, {i, 1, Length[vecy]}]]

Also How can I do the same with Product of elements in vecy ?

POSTED BY: Neel Basu
5 Replies

Thanks solved. Learning how Do loop works was necessary. Also I should use AppendTo instead of Append and the problem that I was actually facing is by default all arguments to a function are immutable. So I had to use HoldFirst to get this to work

POSTED BY: Neel Basu
Posted 8 years ago

Would it be possible for you to take a few minutes to create a simple concrete example that doesn't include some abstract p and some abstract v? Two rows and columns might be enough. And then manually construct what you want the output should be.

With that concrete example someone might be able to show how to accomplish what you want.

POSTED BY: Bill Simpson

Thank You that worked. However is the way of declaring local variables correct ? I am also creating a matrix

matx = Table[x[i, j], {i, Length[v]}, {j, Length[p]}]

I want one equation for each of its row and append it to the constraints list. e.g.

x[i, j] + x[i, j+1]+.... +x[i, Length[p]] == K[[i]]   ? i

I have a list of equations

eqs = {}

I can create the inner loop like

C[vecx_] := Plux @@ vecx == 42
C[matx[[[1]]]

However how to run the outer loop ? and also how to pass the iterator i while iterating over the outer loop ?

In[14]:= Apply[C, matx]
Out[14]= C[{x[1, 1], x[1, 2]}, {x[2, 1], x[2, 2]}, {x[3, 1],  x[3, 2]}, {x[4, 1], x[4, 2]}] 

Applying C over matx is not working

I need something like

loop eq in C @@ matx
    Append[eqs, eq]
 end loop

I have tried

Do[
 Print[ac],
 Append[eqs, ac], {
  ac, 
  C @@@ matx
  }
 ]

But that did not work with error

Do::itform: "Argument Append[eqs,assignmentc] at position 2 does not have the correct form for an iterator"

POSTED BY: Neel Basu

Above methods seem fine. I often do similar but using Array to give indexed variables. So I might use a line like the one below.

vars = Array[y,Length[vecy]]

Then one might use Total[vars] or Apply[Times,vars] to get sum and product respectively.

Again, this is just a variant of the answer from @BillSimpson, and what to use is largely a maetter of taste I guess.

POSTED BY: Daniel Lichtblau
Posted 8 years ago

.

In[1]:= vecy = {1, 2, 3, 4};
mysum = Plus @@ Table[y[i], {i, 1, Length[vecy]}]

Out[2]= y[1] + y[2] + y[3] + y[4]

or

mysum = Apply[Plus, Table[y[i], {i, 1, Length[vecy]}]]

and

In[3]:= myprod = Times @@ Table[y[i], {i, 1, Length[vecy]}]

Out[3]= y[1] y[2] y[3] y[4]
POSTED BY: Bill Simpson
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