2
$\begingroup$

I am trying to solve a maximization problem, where my variable can only take a limited number of values. (Probably) the easiest example would be

dom = {0, 1/2}; NMaximize[{x^2, Element[x, dom]}, {x}]

While the Element[x,dom] syntax works just fine for dom = Integers, I keep getting error messages for any other domain. (edit: this seems to be because my chosen domain is invalid to use with ∈)

edit: I also tried the MemberQ syntax as stated below:

dom = {0, 1/2}; NMaximize[{x^2, MemberQ[dom, x]}, {x}]

This seems to be an acceptable constraint, but for some reason Mathematica seems unable to find a value that meets the constraint, as I get the following message:

NMaximize::nsol: There are no points that satisfy the constraints {False}.

Any other ideas?

edit: just to clarify. I need to restrict the variable(s) to a finite set of real numbers, hence I cannot use ">=/<=" constraints to get the job done. The above is just the simplest application I could think of.

$\endgroup$
4
  • 2
    $\begingroup$ Select Element and press F1 for an overview in the documentation. $\endgroup$ – user21 Sep 4 '13 at 15:15
  • $\begingroup$ Thanks, rubenko! I gather that my chosen domain was invalid to use the [Element] syntax. Another option I can think of is MemberQ, which unfortunately does not work either (I updated the post accordingly). So, I am out of ideas again... $\endgroup$ – Andreas Sep 4 '13 at 15:26
  • $\begingroup$ NMaximize[{x^2, 0 < x < .5, Element[x, Reals]}, {x}] something like this ? $\endgroup$ – Sejwal Sep 4 '13 at 15:35
  • $\begingroup$ Unfortunately, this will not work, as I need to restrict the variable to a set of discrete options. (Obviously it would work for this example, but my real application has some 300 variables and >500 other constraints, so there is no way I could guarantee a border solution like above) $\endgroup$ – Andreas Sep 4 '13 at 15:41
2
$\begingroup$

For multiple domain range check than you can use something like this,

Map[(NMaximize[{x^2, #[[1]] < x < #[[2]], 
     Element[x, Reals]}, {x}]) &, {{.4, .5}, {.4, .6}}]

{{0.25, {x -> 0.5}}, {0.36, {x -> 0.6}}}

You can put all your ranges as list.

$\endgroup$
2
$\begingroup$

Since Element accepts RegionQ objects as domains, you can create a custom domain with:

NMaximize[{x^2, {x} \[Element] Point[List /@ {0, 1/2}]}, x]

{0.25, {x -> 0.5}}

$\endgroup$

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.