0
$\begingroup$

This question already has an answer here:

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

$\endgroup$

marked as duplicate by Oleksandr R., MarcoB, user9660, Community May 9 '16 at 9:20

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    $\begingroup$ 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. $\endgroup$ – Oleksandr R. May 8 '16 at 16:21
  • 2
    $\begingroup$ 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. $\endgroup$ – Oleksandr R. May 8 '16 at 16:43
  • 1
    $\begingroup$ 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.) $\endgroup$ – Oleksandr R. May 8 '16 at 17:27
  • 1
    $\begingroup$ {-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. $\endgroup$ – Simon Woods May 8 '16 at 17:54
  • 2
    $\begingroup$ 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. $\endgroup$ – Oleksandr R. May 8 '16 at 17:54
0
$\begingroup$

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.

$\endgroup$

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