7
$\begingroup$

I have this optimization code:

vars1 = Array[Subscript[x, #] &, {4}];
ka = {35, 10, 20, 25};
objectiveFunction = Total[ka.vars1^5];
NMinimize[{objectiveFunction/100, 
Apply[And, Thread[GreaterEqual[vars1, 0]]] && Total[vars1] == 100 &&
Element[vars1, Integers]}, Flatten[vars1], MaxIterations -> 200]

How is it possible in MMA to capture the values of variables and also objective function in each iteration and plot them versus iteration number??(Separate plots or all in one plot?)

$\endgroup$
9
$\begingroup$
vars1 = Array[x, {4}];
ka = {35, 10, 20, 25};
objectiveFunction = ka.vars1^5;
s = {};
Dynamic@If[Length@s > 3, ListLinePlot[Transpose@s, PlotRange -> {{0, 200}, {0, 100}}, 
                                      GridLines -> {{Length@s}, {}}],,]
k[vars1_] := NMinimize[{objectiveFunction, And @@ Thread[GreaterEqual[vars1, 0]] &&  
                        Tr@vars1 == 100 && vars1 ∈ Integers},
                        vars1, MaxIterations -> 200, StepMonitor :> AppendTo[s, vars1]]
k[vars1]

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ Great belisarius!!! that is what I was looking for but is it also possible to have the monitoring plot during iterations at the same time??I mean exactly when we have the iteration data?Or it is different story? $\endgroup$ – Alex Oct 24 '13 at 22:40
  • $\begingroup$ @Alex See edit, please $\endgroup$ – Dr. belisarius Oct 24 '13 at 22:53
  • $\begingroup$ one more thing why when I changing the separated variables that you put in Sow[{x[1], x[2], x[3], x[4]}]] to Sow[vars1] it is not returning the x[1] value but the rest?What if I have 100 variables?Can I do that same way? $\endgroup$ – Alex Oct 24 '13 at 22:55
  • $\begingroup$ Nice +1. Just how I would do it. Why is the vertical plot range going to 100, when the option sets the limit at 40? $\endgroup$ – Michael E2 Oct 24 '13 at 22:55
  • 2
    $\begingroup$ Great dynamic modelling is really great!!! $\endgroup$ – Alex Oct 24 '13 at 22:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.