0

The Objective function I would like to Maximize takes less than a second to evaluate. There are several packages needed to evaluate it, so I'm not showing it here.

Timing[fitcst[{-0.3, -0.2}, {0.2, 0.1}, sol]]

Returns correctly,

{0.284273, 0.000898708}

I was wondering why using NMaximize I never get a single result (I tried setting the time limit to 10000):

t=10000;

 TimeConstrained[
     NMaximize[ {fitcst[{wl1, wl2}, {wu1, wu2}, sol], -1 < wl1 < 0, -1 < 
        wl2 < 0, 0 < wu1 < 1, 
       0 < wu2 < 1}, {{wl1, -0.7, -0.5}, {wl2, -0.5, -0.4}, {wu1, 0.5, 
        0.6}, {wu2, 0.2, 0.3}}, 
      Method -> {"DifferentialEvolution", "CrossProbability" -> 0.5, 
        "SearchPoints" -> 1 }, AccuracyGoal -> 1, WorkingPrecision -> 2, 
      PrecisionGoal -> 1, 
      StepMonitor :> Print["Step to x = ", {wl1, wl2,  wu1, wu2}]], t]

Am I wrongly setting something?

UPDATE

I tried evaluating the following.

    bubu[wl1_, wl2_, wu1_, wu2_] := 
 With[{a = 0}, wl1^10 + wl2^20 + wu1^20 + wu2^20 + a]

TimeConstrained[
 NMinimize[{bubu[wl1, wl2, wu1, wu2], -1 < wl1 < 0, -1 < wl2 < 0, 
   0 < wu1 < 1, 
   0 < wu2 < 1}, {{wl1, -0.4, -0.3}, {wl2, -0.4, -0.3}, {wu1, 0.4, 
    0.5}, {wu2, 0.3, 0.4}} , 
  Method -> {"DifferentialEvolution", "CrossProbability" -> 0.5, 
    "SearchPoints" -> 2 }, AccuracyGoal -> 1, WorkingPrecision -> 2, 
  PrecisionGoal -> 1, 
  StepMonitor :> Print["Step to x = ", {wl1, wl2,  wu1, wu2}]], 20]

And it works. So I have effectively setted correctly NMaximize.

Have you any suggestions? What might be the problem??

UPDATE 2

I've added a command inside my function that would save its partial results to see what it is doing. I have found that the partial results contained the design space variables as symbols instead of assigning numbers. This might explain why it isn't working.

UPDATE 3

Oleksandr suggested to add _?NumericQ to my variables. It still doesn't work, giving me an error msg. Also, if I try evaluating my function now it doesn't evaluate results anymore. This is how I defined my function:

 fitcst[wlo_?NumericQ, wup_?NumericQ, sol_?NumericQ] := 
 With[{m = 50, \[Alpha] = 2 Degree},
  pts = cst[wlo, wup];
(...)
]

UPDATE 4

As pointed out, the first two variables are vectors so this wasn't the proper way to use NumericQ. Using the following it now evaluates but NMaximize still won't work.

fitcst[wlo_?NumericQ, wup_?NumericQ, sol_?NumericQ] := 
     With[{m = 50, \[Alpha] = 2 Degree},
      pts = cst[wlo, wup];
    (...)
    ]

Indeed this is my output.

NMaximize::nnum: "The function value {-0.534412} is not a number at {wl1,wl2,wu1,wu2} = {-0.32,-0.31,0.45497152228804144113993856990418862551`2.,0.396484375`}"

Why does it say it isn't a number??

Mirko Aveta
  • 2,192
  • 11
  • 26
  • What is fitcst? – bobbym May 08 '16 at 10:13
  • Its my function. I can't post it here because it needs several packages to evaluate. Anyhow It perfectly works. – Mirko Aveta May 08 '16 at 10:15
  • What does your fitcst function do with symbolic input? – Simon Woods May 08 '16 at 13:38
  • It reads Class Shape Transforation design variables for airfoil generation link, evaluates the lift coefficient and verifies a geometrical condition, finally it returns a fitness function through goal programming – Mirko Aveta May 08 '16 at 13:43
  • Your differential evolution parameters are grossly inappropriate, so it is not surprising that it doesn't work. You would need at least 20 search points, not 1 or 2. And your crossover probability should be close to 0 or 1, not 0.5. – Oleksandr R. May 08 '16 at 15:50
  • Ok, I'll try as you suggest. – Mirko Aveta May 08 '16 at 16:07
  • I still have the same problem. I also tried using other type of methods but it simply won't evaluate. I believe that there must be some kind of problem when NMaximize assigns a value to my function. – Mirko Aveta May 08 '16 at 16:13
  • 1
    You may try adding ?NumericQ guards on the parameters of your function. Without a concrete example that reproduces the problem, I think we will just be guessing at possible solutions. However, please note that what I said above is true independently of whether it resolves this particular issue or not. – Oleksandr R. May 08 '16 at 16:21
  • Ok. I've done something to prove my suspects. I added a command that would save what Mathematica is doing at every evaluation. It saved, instead of properly evaluated functions, the functions evaluating symbols!! Why is it doing so? – Mirko Aveta May 08 '16 at 16:37
  • 2
    Please search on this site about NumericQ; a list of some relevant questions/answers is given here. I believe that this is such a common question that there is little point in reiterating everything here. If it doesn't solve your problem (and, consider that you may have to restart Mathematica to clear out old definitions), please update your question accordingly. – Oleksandr R. May 08 '16 at 16:43
  • Thank you. I've just updated my question. – Mirko Aveta May 08 '16 at 17:17
  • 1
    A 2-vector is not a numeric quantity. Your definition should have had e.g. fitcst[wlo : {_?NumericQ, _?NumericQ}, wup : {_?NumericQ, _?NumericQ}, sol_?NumericQ] := ... (I assume sol is a numeric quantity, but you don't say how it is defined, so this may not be the case.) – Oleksandr R. May 08 '16 at 17:27
  • Indeed wlo and wup are vectors. Ok I'll try this! Thanks again – Mirko Aveta May 08 '16 at 17:31
  • I updated again. It still outputs a funny error. – Mirko Aveta May 08 '16 at 17:47
  • 1
    {-0.534412} is not a number, it's a list. Your function appears to be returning a list instead of a number at the given values of the parameters. – Simon Woods May 08 '16 at 17:54
  • 2
    You are apparently returning your result inside a list for some reason. You can get the same error with NMaximize[{{-x^2}}, x]. Remove this unnecessary list head and it will most likely work. By the way, this entire process of trial and error could have been avoided if you had provided a minimal working example that reproduces your problem rather than stating only what doesn't work. In fact, questions that do not provide a MWE are often closed as unanswerable. – Oleksandr R. May 08 '16 at 17:54

1 Answers1

0

As pointed out kindly by Oleksandr I bumped into a common beginner's error. Reference to this can be found in the following links: HERE and HERE The correct way to do this, in my case, as I have vectors as variables would have been:

    fitcst[wlo : {_?NumericQ, _?NumericQ}, wup : {_?NumericQ, _?NumericQ},
  sol_] := With[{m = 50, \[Alpha] = 2 Degree},
  pts = cst[wlo, wup];
(...)
]

Important suggestions on how to use the "Differential Evolution" method can be found in the comments.

Mirko Aveta
  • 2,192
  • 11
  • 26