0
$\begingroup$

Here is my Mathematica function.

fourVertEnrgy[x11_, x12_,x13_, x14_, x21_, x22_, x23_, x24_, x31_, x32_, x33_, x34_, x41_, x42_, x43_, x44_]:=
1 + 2x11 x12 + 2x11 x13+ 2x11 x14+2x12 x13 + 2 x12 x14+ 2x13 x14 -x11 -x12 -x13 - x14 +
1+2x21 x22+2x21 x23+2x21 x24+ 2x22 x23+ 2x22 x24+ 2x23 x24 - x21 -x22 -x23-x24 + 
1 + 2x31 x32 + 2x31 x33 + 2x31 x34 + 2x32 x33 + 2 x32 x34 + 2 x33 x34 -x31 -x32- x33-x34 + 
1+2x41 x42 + 2x41 x43 +2 x41 x44 + 2x42 x43 + 2x42 x44 + 2x43 x44 - x41-x42 -x43 -x44 + 
1 + 2x11 x21 + 2x11 x31 + 2x11 x41 + 2x21 x31 + 2x21 x41 +2x31 x41 -x11-x21-x31-x41+
1+2x12 x22 + 2x12 x32 + 2x12 x42 + 2x22 x32 + 2x22 x42 + 2x32 x42 -x12 -x22-x32-x42 + 
1+2x13 x23 + 2x13 x33 + 2x13 x43 + 2x23 x33 + 2x23 x43 + 2x33 x43 -x13-x23-x33-x43+
1+2x14 x24 + 2x14 x34 + 2x14 x44+ 2x24 x34 + 2x24 x44 + 2x34 x44 -x14-x24 -x34 -x44;

I am trying to find the minima using Simulated Annealing.

destFile=OpenWrite["/home/shehab/Documents/shehab-isi/quantum-shehab/docs/presentations/lucas-gi-algo/values-"<>DateString[]]
i=0; list={{0,9999999}}; 
NMinimize[{fourVertEnrgy[x11, x12,x13, x14, x21, x22, x23, x24, x31, x32, x33, x34, x41, x42, x43,x44],
x11\[Element]Integers,x12\[Element]Integers,x13\[Element]Integers, x14\[Element]Integers,x21\[Element]Integers, x22\[Element]Integers, x23\[Element]Integers,x24\[Element]Integers, x31\[Element]Integers, x32\[Element]Integers,x33\[Element]Integers, x34\[Element]Integers, x41\[Element]Integers, x42\[Element]Integers, x43\[Element]Integers,x44\[Element]Integers,
0<=x11<=1,0<=x12<=1,0<=x13<=1,0<=x14<=1,0<=x21<=1,0<=x22<=1,0<=x23<=1,0<=x24<=1,0<=x31<=1,0<=x32<=1,0<=x33<=1,0<=x34<=1, 0<=x41<=1, 0<=x42<=1,0<=x43<=1,0<=x44<=1},{x11, x12,x13, x14, x21, x22, x23, x24, x31, x32, x33, x34, x41, x42, x43, x44},
Method->"SimulatedAnnealing",EvaluationMonitor:>Join[list,{{i++,fourVertEnrgy[x11, x12,x13, x14, x21, x22, x23, x24, x31, x32, x33, x34, x41, x42, x43, x44]}}]]
Export[destFile,list,"CSV"] 
Close[destFile]

I am using EvaluationMonitor[] to store the function value against the step number for all the steps in a CSV file. But the file only has $0, 9999999$ when the computation is done. Did I do anything wrong?

$\endgroup$
1
$\begingroup$

Several issues: You don't have to OpenWrite a file when you use Export. Export will do everything for you. OpenWrite is for situations where you want to do low-level file operations. You can use Element[{a,b,c},..] to say say that a,b,c should be Integers. When you want to store values of EvaluationMonitor it is maybe easier to use Sow and Reap.

{result, monitor} = 
  Reap@NMinimize[{fourVertEnrgy[x11, x12, x13, x14, x21, x22, x23, 
      x24, x31, x32, x33, x34, x41, x42, x43, x44], 
     Element[{x11, x12, x13, x14, x21, x22, x23, x24, x31, x32, 
           x33, x34, x41, x42, x43, x44}, Integers], 
        0 <= x11 <= 1, 0 <= x12 <= 1, 0 <= x13 <= 1, 0 <= x14 <= 1, 
        0 <= x21 <= 1, 0 <= x22 <= 1, 0 <= x23 <= 1, 0 <= x24 <= 1, 
        0 <= x31 <= 1, 0 <= x32 <= 1, 0 <= x33 <= 1, 0 <= x34 <= 1, 
        0 <= x41 <= 1, 0 <= x42 <= 1, 0 <= x43 <= 1, 0 <= x44 <= 1}, 
      {x11, x12, x13, x14, x21, x22, x23, x24, x31, x32, x33, x34, 
     x41, x42, 
        x43, x44}, Method -> "SimulatedAnnealing", 
      EvaluationMonitor :> Sow[fourVertEnrgy[x11, x12, x13, x14, x21, x22, x23, 
        x24, x31, x32, x33, x34, x41, x42, x43, x44]]];
list = Table[{i, monitor[[1, i]]}, {i, Length[monitor[[1]]]}];
Export["output.csv", list]
| improve this answer | |
$\endgroup$
  • $\begingroup$ thanks. When I run the code in the 'Out' dialogue (i.e. removing the semi-colons) it shows the global minima 0 but in the file the lowest value is 4. Any hint? $\endgroup$ – Omar Shehab Jul 15 '14 at 18:11

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.