Information Technology Reference
In-Depth Information
{
}
spfun[vec :
spfun[vec :
spfun[vec :
{
{
Real
Real
Real
}
}
]:=
]:=
]:=
With[ {
vals = splitRange[vec] },
With[
With[
,
Abs[(Apply[Plus , Sqrt[ N [First[vals]]]]
{
{
vals = splitRange[vec]
vals = splitRange[vec]
}
}
,
Abs[(Apply[Plus , Sqrt[ N [First[vals]]]]
Apply[Plus , Sqrt[ N [Last[vals]]]])]]
Abs[(Apply[Plus , Sqrt[ N [First[vals]]]]
Apply[Plus , Sqrt[ N [Last[vals]]]])]]
Apply[Plus , Sqrt[ N [Last[vals]]]])]]
It may be a bit difficult to see what this does, so we illustrate again on our small exam-
ple. Supposing we have split smallset into two subsets as above, what is the objective
function? Well, what we do is take the first, sixth, and third elements, add their square
roots, and do likewise with the fifth, fourth, and second elements. We subtract one of
these sums from the other and take the absolute value of this difference. For speed we do
all of this in machine precision arithmetic. In exact form it would be:
sqrts = Sqrt[splitRange[vals]]
sqrts = Sqrt[splitRange[vals]]
1 , 6 , 3 , 5 , 2 , 2
sums = To tal[sqrts ,{
sums = To tal[sqrts ,
sums = To tal[sqrts ,{
} ]
1 + 3 + 6 , 2 + 2 + 5
{
2
2
2
}
} ]
]
sumdifference = Apply[Subtract , sums]
2 + 3
5 + 6
1
abssummdiffs = Abs[sumdifference]
abssummdiffs = Abs[sumdifference]
1 + 2
3 + 5
6
approxabs = N [abssummdiffs]
0 . 468741
As a check of consistency, observe that this is just what we get from evaluating our
objective function on vals .
spfun[vals]
spfun[vals]
0 . 468741
We now put these components together into a function that provides our set partition.
Outline of getHalfSet
getHalfSet
1. Input: An even integer n , and options to pass along to NMinimize .
2. Create a list of variables, vars, of length n .
3. Set up initial ranges that the variables all lie between 0 and 1 (these are not
hard constraints but just tell NMinimize where to take random initial values).
Search WWH ::




Custom Search