Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?)

share|improve this question
2  
Have a look at the EvaluationMonitor examples. There are also some examples in the EvaluationMonitor section of NMinimize documentation –  ssch Oct 24 '13 at 22:10

1 Answer 1

up vote 6 down vote accepted
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

share|improve this answer
    
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? –  Alex Oct 24 '13 at 22:40
    
@Alex See edit, please –  belisarius Oct 24 '13 at 22:53
    
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? –  Alex Oct 24 '13 at 22:55
    
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? –  Michael E2 Oct 24 '13 at 22:55
2  
Great dynamic modelling is really great!!! –  Alex Oct 24 '13 at 22:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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