0

I have created this function which calculate the figure of merit of data acquired. The Loading variable is feed with some random numbers for the forum. In real life, I have real data.

DD = 20; Days = 3; Peak = 1;
Loading = Table[Table[{RandomReal[{-1500, 1500}], RandomReal[{-1, 1}/100]}, {i, 1, 150}], {j, 1, Days}];
coo = Loading[[All, All, 1]];
TimeSwapFunction[lst_] := 
Norm[{Temp = 
Sort[Partition[
  Flatten[Riffle[
    Flatten[Table[coo[[i]] + lst[[i]], {i, 1, Days}]], 
    Flatten[Loading[[1 ;; Days, All, Peak + 1]]]]], 
  2], #1[[1]] < #2[[1]] &];
Mean[Select[
  Partition[
   Riffle[Mean[Transpose[Partition[Temp[[All, 1]], DD]]], 
    Abs[Mean[
       Transpose[
        Partition[Temp[[All, 2]], DD]]]/(StandardDeviation[
         Transpose[Partition[Temp[[All, 2]], DD]]]/Sqrt[DD])]], 
   2], #1[[1]] > 0 &][[All, 2]]]}]

The function is working now. As you can see, if we plot the TimeSwapFunction (see below), there is some maxima. The aim is to use NMaximize to find the maxima with the algorithms already coded in mathematica like a Genetic Algorithm.

But if I use directly NMaximize (with only one variable), it is not working. You can see it by looking at the inputs 19th and 21st. The output is some value that I don't understand from where it comes from.

If I allow two variables, that is the same story (input 25th). If i put the third one, it is not working anymore.

I would be really glad if someone can help me to go through this thing.

The image in order to put the error and the commands

toutsec
  • 55
  • 7
  • If you include just enough sample data for lst then someone might be able to reproduce the problem, diagnose it and perhaps even offer a solution. – Bill Jul 10 '14 at 18:12
  • @Bill: you are right. I add data sample. My lst is a list a variables that can be adjust in order to maximize my function. I have so many possibilities for them that I want to use a genetic algorithm at the end (method differential evolution). – toutsec Jul 11 '14 at 02:52
  • Thank you. I scrape your DD=20;..., paste that into MMA, append your TimeSwapFunction[{120, 0, 0}] and hit . It responds with a stream of errors about Data not being defined. I realize you have an infinite number of possibilities. Can you pare it down to one simple complete example that people can run, get the same "crap" you get, read your explanation of why that specific answer is "crap" and what the answer should be? Then I'll try to diagnose where you went wrong and see if I can get it to give the answer you say you should get. Note: What is Hold[lst] doing? Nothing? – Bill Jul 11 '14 at 06:27
  • @Bill: I was on holydays away from my computer. I improved the stuff and added a picture for sake of clearness. – toutsec Jul 27 '14 at 13:01

1 Answers1

1

This is fixed by defining your TimeSwapFunction so that it takes only numeric arguments:

 Clear[TimeSwapFunction]
 TimeSwapFunction[lst_ /; (And @@ ( NumericQ /@ lst))] :=  (*the rest the same*)


 NMaximize[TimeSwapFunction[{c, b, a}], {c, b, a}]                

{0.804359, {c -> 7.35047, b -> -4.85534, a -> 3.51407}}

This is a commonly encountered issue with many of the N numeric functions (NIntegrate,etc)

You should not use variables starting with upper case letters by the way, to avoid conflict with built-ins (not apparently an issue in the case however )

As another aside be aware with such a highly nonlinear function you are likely to find local rather than global extremes.

agentp
  • 6,849
  • 2
  • 19
  • 37
  • Thanks! I am aware about the upper case letters, I am always checking. An idea in order to make it parallel? And I know that it is highly non-linear. – toutsec Jul 29 '14 at 08:09