Re: Some linear constraints seem to be ignored in function

• To: mathgroup at smc.vnet.net
• Subject: [mg132371] Re: Some linear constraints seem to be ignored in function
• From: Bob Hanlon <hanlonr357 at gmail.com>
• Date: Sun, 2 Mar 2014 01:04:32 -0500 (EST)
• Delivered-to: l-mathgroup@mail-archive0.wolfram.com
• Delivered-to: l-mathgroup@wolfram.com
• Delivered-to: mathgroup-outx@smc.vnet.net
• Delivered-to: mathgroup-newsendx@smc.vnet.net
• References: <20140222080533.BDB3B6A13@smc.vnet.net>

```If the function and constraints are linear, NMinimize can always find
global minima, over both real and integer values. Otherwise, NMinimize may
sometimes find only a local minimum.

Better results

ff[lxw_, lwz_, c_, d_] = -J1 (lxw + lwz) - 2 J2 c +
T (-Log[2] - 1/2 (1 - lxw) Log[(1 - lxw)/4] -
1/2 (1 + lxw) Log[(1 + lxw)/4] - 1/2 (1 - lwz) Log[(1 - lwz)/4] -
1/2 (1 + lwz) Log[(1 + lwz)/4] + 1/2 (1 - d) Log[(1 - d)/16] +
1/8 (1 + 2 c + d - 2 lwz - 2 lxw)*
Log[1/16 (1 + 2 c + d - 2 lwz - 2 lxw)]);

var = {lxw, lwz, c, d};

cons = And @@ Cases[ff @@ var, Log[x_] -> x > 0, Infinity] // Simplify

d < 1 && lwz < 1 && 1 + lwz > 0 && 1 + 2 c + d > 2 (lwz + lxw) && lxw < 1
&&
1 + lxw > 0

T = 10;
J1 = 1;
J2 = -2/10;

sol1 = NMinimize[{ff @@ var, cons}, var, Method -> "DifferentialEvolution"]
//
Quiet

{-12.5767, {c -> -0.236399, d -> -0.976973, lwz -> -0.68191,
lxw -> -0.939971}}

Solving for all of the partial derivatives equal to zero

sol2 = NSolve[
Append[
Simplify[
D[ff @@ var, #] == 0 & /@ var],
cons],
var][[1]]

{lxw -> -0.0599281, lwz -> -0.0599281, c -> 4.21567, d -> -4.65527}

ff @@ var /. sol2

-14.1157

Which is less than the previously identified local minimum.

However, since

lxw == lwz /. sol2

True

investigate with lwz constrained to equal lxw

ff[lxw_, c_, d_] = ff[lxw, lxw, c, d] // Simplify

(2 c)/5 - 2 lxw +
5/4 (-4 (-1 + d) Log[(1 - d)/16] + (1 + 2 c + d - 4 lxw) Log[
1/16 (1 + 2 c + d - 4 lxw)] -
8 (Log[(1 - lxw)/8] - lxw Log[1 - lxw] + (1 + lxw) Log[1 + lxw]))

var2 = {lxw, c, d};

cons2 = And @@ Cases[ff @@ var2, Log[x_] -> x > 0, Infinity] // Simplify

d < 1 && -1 < lxw < 1 && 1 + 2 c + d > 4 lxw

sol3 = NMinimize[{ff @@ var2, cons2}, var2,
Method -> "DifferentialEvolution"] // Quiet

{-15.4231, {c -> -0.0657214, d -> -0.474906, lxw -> -0.994937}}

Which is again less than the previously identified local minima.

Bob Hanlon

On Sat, Feb 22, 2014 at 11:00 AM, Bob Hanlon <hanlonr357 at gmail.com> wrote:

> I don't know why the search methods go outside the constraints, but you
> can get better results using a Method other than the default of Automatic.
>
>
> \$Version
>
>
> "8.0 for Mac OS X x86 (64-bit) (October 5, 2011)"
>
>
> Clear[ff, T, J1, J2]
>
>
> ff[lxw_, lwz_, c_, d_] = -J1 (lxw + lwz) - 2 J2 c +
>
>    T (-Log[2] - 1/2 (1 - lxw) Log[(1 - lxw)/4] -
>       1/2 (1 + lxw) Log[(1 + lxw)/4] -
>       1/2 (1 - lwz) Log[(1 - lwz)/4] -
>       1/2 (1 + lwz) Log[(1 + lwz)/4] +
>       1/2 (1 - d) Log[(1 - d)/16] +
>       1/8 (1 + 2 c + d - 2 lwz - 2 lxw)*
>        Log[1/16 (1 + 2 c + d - 2 lwz - 2 lxw)]);
>
>
> cons = And @@ Cases[ff[lxw, lwz, c, d],
>
>   Log[x_] -> x > 0, Infinity] // Simplify
>
>
> d < 1 && lwz < 1 && 1 + lwz > 0 && 1 + 2 c + d > 2 (lwz + lxw) && lxw < 1
> &&
>  1 + lxw > 0
>
>
> T = 10;
> J1 = 1;
> J2 = -2/10;
>
>
> Off[NMinimize::nrnum]
>
>
> NMinimize[{ff[lxw, lwz, c, d], cons}, {lxw, lwz, c, d}]
>
>
> {-4.91624, {c -> 0.94525, d -> -0.315535, lwz -> 0.901231, lxw ->
> -0.191668}}
>
>
> NMinimize[{ff[lxw, lwz, c, d], cons}, {lxw, lwz, c, d},
>  Method -> "DifferentialEvolution"]
>
>
> {-12.5767, {c -> -0.236399, d -> -0.976973, lwz -> -0.68191,
>   lxw -> -0.939971}}
>
>
>
> Bob Hanlon
>
>
>
>
> On Sat, Feb 22, 2014 at 3:05 AM, Daniel Stariolo <
> daniel.stariolo at gmail.com> wrote:
>
>> I'm trying to minimize a non-linear function of four variables with some
>> linear constraints. Mathematica 8 is unable to find a good solution giving
>> complex values of the function at some point in the iteration. This implies
>> that one or some contraints are not being enabled in the process. Is this a
>> bug or limitation of the optimization function ?
>>
>> Function to minimize is
>>
>> ff[lxw_, lwz_, c_, d_] := - J1 (lxw + lwz) - 2 J2 c +
>>   T (-Log[2] - 1/2 (1 - lxw) Log[(1 - lxw)/4] -
>>      1/2 (1 + lxw) Log[(1 + lxw)/4] -
>>      1/2 (1 - lwz) Log[(1 - lwz)/4] -
>>      1/2 (1 + lwz) Log[(1 + lwz)/4] + 1/2 (1 - d) Log[(1 - d)/16] +
>>      1/8 (1 + 2 c + d - 2 lwz - 2 lxw) Log[
>>        1/16 (1 + 2 c + d - 2 lwz - 2 lxw)])
>>
>> where
>>
>> T = 10;
>> J1 = 1;
>> J2 = -0.2;
>>
>> are constant parameters. Then I try
>>
>> NMinimize[{ff[lxw, lwz, c, d],
>>   2 c + d - 2 lwz - 2 lxw  >= -0.999 && -0.999 <= lxw <=
>>    0.999 && -0.999 <= lwz <= 0.999 && -0.999 <= c <= 0.999 &&
>>   d <= 0.9999}, {lxw, lwz, c, d}]
>>
>> with the result
>>
>> NMinimize::nrnum: "The function value 5.87777-4.87764 I is not a real
>> number at {c,d,lwz,lxw} = {-0.718817,-1.28595,0.69171,-0.932461}."
>>
>> I would appreciate if someone can give a hint at what is happening here.
>>
>>
>

```

• Next by Date: Solving Equations with 3 variables and many parameters
• Next by thread: Re: Some linear constraints seem to be ignored in function