Message Boards Message Boards

0
|
4432 Views
|
4 Replies
|
1 Total Likes
View groups...
Share
Share this post:

[?] Iterate constraints over matrix elements?

Posted 4 years ago

Hi ,

I've been stuck on what may be a very simple problem with syntax. Would appreciate any help on the following:

Now, I'm trying to solve a linear optimisation homework problem So I have this matrix of decision variables:

x = {{0,    0, 0,  0,   0,    0, 0,  0,   0, 0},
  {0,   0,    0, 0,  0,   0, 0, 0,  0, 0},
  {0,   0,    0, 0,  0,   0, 0, 0,  0, 0},
  {0,   0,    0, 0,  0,   0, 0, 0,  0, 0},
  {0,   0,    0, 0,  0,   0, 0, 0,  0, 0},
  {0,   0,    0, 0,  0,   0, 0, 0,  0, 0},
  {0,   0,    0, 0,  0,   0, 0, 0,  0, 0},
  {0,   0,    0, 0,  0,   0, 0, 0,  0, 0},
  {0,   0,    0, 0,  0,   0, 0, 0,  0, 0},
  {0,   0,    0, 0,  0,   0, 0, 0,  0, 0}
  }

What I'm trying to do is add constraints:

  1. That all X(i,j) are integers between 0 and 5

  2. That the sum of the first column is less than 50

  3. The sum of the 3rd row is exactly 30

  4. The diagonals X(i,i) are all zero.

My problem is that I just can't figure out how to use indexing and iterations to apply these constraints in the Minimize function.

I tried using Thread, apply, Forall, but nothign seems to work.

What woud be ideal is if I can do something like this:

X(i,j) < some condition> where i=1,2,... n

If I can figure out how to do this, then i think I'll be able to proceed.

POSTED BY: Zain Khaishagi
4 Replies

Or perhaps a bit better

m = Table[If[i == j, 0, RandomInteger[{0, 5}]], {i, 10}, {j, 10}];
nn = 1;
sum = 0;
While[
  sum != 30,
  nn = nn + 1;
  sum = Total[
    tt = Table[If[j != 3, RandomInteger[{0, 5}], 0], {j, 10}]];
  ];
nn
tt
m[[3]] = tt;
m // MatrixForm
POSTED BY: Hans Dolhaine

Perhaps something like this?

m = Table[If[i == j, 0, RandomInteger[{0, 5}]], {i, 10}, {j, 10}];
Do[If[i == 3, m[[i, i]] = 0, m[[3, i]] == RandomInteger[{3, 5}]], {i, 
  4, 10}]
m[[3, 10]] = 30 - Sum[m[[3, j]], {j, 9}];
m // MatrixForm
sum1 = Sum[m[[j, 1]], {j, 10}]
sum2 = Sum[m[[3, j]], {j, 10}]
POSTED BY: Hans Dolhaine
Posted 4 years ago

Hi,

Thanks for the reply.

Can you also direct me to a few examples or documentation resources which i can study to get a better understanding of how to best approach a minimization problem using the kind of constraints i have ?

I do have a few days before I have to submit the homework, and I really want to try this in Mathematica.

POSTED BY: Zain Khaishagi
Anonymous User
Anonymous User
Posted 4 years ago

FIrst, your using x not as a matrix but as a List[], as opposed to Array[] Function[] or Expression[]. Mathematica does not use the word "matrix" to describe it's data structure(except inside a linear algebra pack and other special adaptations).

There are many syntaxes - and some new ones. But to answer the question I think most people will need to know this:

"to apply these constraints in the Minimize function"

To apply the constrains in which Minimization function? You cannot apply conditions inside the built-in function because "Evaluate" doesn't work in that way or order.

You can check constraints and correct constraints using code or functions, have them handled as a function or act inside an array as expressions or as expressions, and there are more possibilities.

"X(i,j) < some condition> where i=1,2,... n"

Is something you would see in a low level programming class text book source comment (you will find easier and better ways to do it in Mathematica). Back to Minimize: it's not just that you want conditions - how to do them really depends on what are you trying to do.

Being homework - you may need to study more about Mathematica before using it in a timed project (as in homework due tomorrow). Mathematica can be far easier and far more powerful than other languages but it is not a small language so it is not simply encapsulated in a few phrases like some small code languages are.

POSTED BY: Anonymous User
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