Mathematica Asked on July 18, 2021
So I’m running NMaximize
for optimizing a value and for constraints, I need the parameters to belong to a discrete set of elements. Basically, the constraint looks like,
And @@Table[{Subscript[x, i], Subscript[y, i]} [Element] Table[Subscript[e, i, n], {i, 1, n}],{i, 1, k}]
,where {Subscript[x, i], Subscript[y, i]}
are my parameters satisfying the constraint that they should belong to Table[Subscript[ex, i, n], {i, 1, n}]
But NMaximize
do not consider this as a constraint. Then I changed the constraint to the convex hull of Table[Subscript[ex, i, n], {i, 1, n}]
with an additional constraint to pick out the extreme points aka the vertices. Now my code looks like,
And @@ Table[{Subscript[x, i], Subscript[y, i]} [Element]
ConvexHullMesh[Table[Subscript[ex, i, n], {i, 1, n}]] &&
Subscript[z, i] == 1/2 &&
Subscript[x, i]^2 + Subscript[y, i]^2 == Subscript[r, n]/2, {i, 1,
k}]
But, when I run this it outputs this error
Obtained solution does not satisfy the following constraints within
Tolerance -> 0.001`
What do I do?
FULL CODE
Subscript[r, n_] := Sqrt[Sec[Pi/n]];
Subscript[w, i_,
n_] := {Subscript[r, n] Cos[2 Pi i/n],
Subscript[r, n] Sin[2 Pi i/n], 1};
Subscript[e, i_, n_] :=
1/2 {Subscript[r, n] Cos[(2 i - 1) Pi/n],
Subscript[r, n] Sin[(2 i - 1) Pi/n], 1};
Subscript[ex, i_, n_] :=
1/2 {Subscript[r, n] Cos[(2 i - 1) Pi/n],
Subscript[r, n] Sin[(2 i - 1) Pi/n]};
u = {0, 0, 1};
f = (u - #) &;
(*Factors={1,2,3,12,13,23,123}*)
Factors = Times @@@ Subsets[Transpose@Tuples[{1, -1}, 3], {1, 3}];
(*Rearrange*)
(*Rearrange the numbers in the RHS to obtain different
combinations*)
Factors[[{1, 2, 3, 4, 5, 6, 7}]] = Factors[[{1, 2, 3, 4, 5, 6, 7}]];
Factors = Transpose[Factors];
Vec[j_] := {Subscript[x, j], Subscript[y, j], Subscript[z, j]};
AllParameters[k_] :=
Module[{i},
Flatten[Table[{Subscript[x, i], Subscript[y, i], Subscript[z,
i]}, {i, 1, k}]]];
AllConstraints[n_, k_] :=
Module[{i},
And @@ Table[{Subscript[x, i], Subscript[y, i]} [Element]
ConvexHullMesh[Table[Subscript[ex, i, n], {i, 1, n}]] &&
Subscript[z, i] == 1/2 &&
Subscript[x, i]^2 + Subscript[y, i]^2 == Subscript[r, n]/2, {i,
1, k}]];
GPT[n_, k_] := Module[{ro, co, ve, i},
FunFactor = Factors[[1 ;; 8, 1 ;; k]] /. {1 -> Identity, -1 -> f};
vec = Table[Subscript[v,
i], {i, 1, k}] /. {Subscript[v, j_] -> Vec[j]};
vecs = Table[
Total[Table[FunFactor[[ro, co]][vec[[co]]], {co, 1, k}]], {ro, 1,
8}];
max = Total[
Table[Max[
Map[vecs[[ve]].# &, Table[Subscript[w, i, n], {i, 1, n}]]], {ve,
1, 8}]];
{time, out} =
Timing[NMaximize[{max, AllConstraints[n, k]}, AllParameters[k],
Method -> "NelderMead"]];
Print[out, out[[1]]/(k 8), " ", time]; {time, out} =
Timing[NMaximize[{max, AllConstraints[n, k]}, AllParameters[k],
Method -> "DifferentialEvolution"]];
Print[out, out[[1]]/(k 8), " ", time]; {time, out} =
Timing[NMaximize[{max, AllConstraints[n, k]}, AllParameters[k],
Method -> "SimulatedAnnealing"]];
Print[out, out[[1]]/(k 8), " ", time];]
GPT[4, 7]
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP