Information Technology Reference
In-Depth Information
3. Give them initial ranges from 0 to 1.
4. Form an objective function that sums the n 2 products of elements of the first
matrix and elements of the row-and-column permuted second matrix. As in
QAP , the permutation is determined by the ordering of values of the variables
vector.
5. Iterate some number of times (a reasonable value is 4).
Swap a random pair of elements in the variables vector.
Check whether we got improvement in the objective function.
If so, keep this improved vector.
If not, possibly still keep it depending on whether a random value between
0 and 1 is larger than p , and also whether the better vector is the best seen thus
far (we never replace the best one we have).
Depending on an input flag setting, either restart the swapping (if we are not
done iterating) with our original vector, or else continue with the one created
from prior swaps.
6. Call NMinimize on the objective function, using the above ranges, con-
straints, and input option settings.
7. Return the minimal value found, along with the permutation that gives rise to
that value.
QAP4[mat1 , mat2 , cp , it , sp , sc , maxj :4 , keep :0 . 4 , restorevector ,
QAP4[mat1 , mat2 , cp , it , sp , sc , maxj :4 , keep :0 . 4 , restorevector ,
printFlag :False]:=Module[
printFlag :False]:=Module[
{
printFlag :False]:=Module[
{
{
len = Length[mat1] , objfunc , objfunc2 , vars , vv , nmin , vals , rnges , best ,
len = Length[mat1] , objfunc , objfunc2 , vars , vv , nmin , vals , rnges , best ,
bestvec , indx = 0 , i = 0 , tt = TimeUsed[]
len = Length[mat1] , objfunc , objfunc2 , vars , vv , nmin , vals , rnges , best ,
bestvec , indx = 0 , i = 0 , tt = TimeUsed[]
bestvec , indx = 0 , i = 0 , tt = TimeUsed[]
}
}
}
,
,
,
vars = Array[vv , len];
vars = Array[vv , len];
rnges = Map[
& , vars];
objfunc2[vec ]:=objfunc2[vec]=
rnges = Map[
rnges = Map[
{
{
{
# , 0 , 1
# , 0 , 1
# , 0 , 1
}
}
}
& , vars];
& , vars];
objfunc2[vec ]:=objfunc2[vec]=
To tal[Flatten[mat1
objfunc2[vec ]:=objfunc2[vec]=
To tal[Flatten[mat1
To tal[Flatten[mat1
permuteMatrix[mat2 , vec]]];
permuteMatrix[mat2 , vec]]];
permuteMatrix[mat2 , vec]]];
{
}
objfunc[vec :
objfunc[vec :
objfunc[vec :
{
{
Real
Real
Real
}
}
]:=Module[
]:=Module[
]:=Module[
{
val1 , val2 , r1 , r2 , vec1 = vec , vec2 = vec , max = Max[Abs[vec]] , j = 0
}
,
{
{
val1 , val2 , r1 , r2 , vec1 = vec , vec2 = vec , max = Max[Abs[vec]] , j = 0
val1 , val2 , r1 , r2 , vec1 = vec , vec2 = vec , max = Max[Abs[vec]] , j = 0
}
}
,
,
{
vec1 , vec2
}
=
{
vec1 , vec2
}
/ max;
{
/ max;
val1 = objfunc2[Ordering[vec1]];
{
vec1 , vec2
vec1 , vec2
}
}
=
=
{
{
vec1 , vec2
vec1 , vec2
}
}
/ max;
val1 = objfunc2[Ordering[vec1]];
val1 = objfunc2[Ordering[vec1]];
While[ j
While[ j
maxj ,
While[ j
maxj ,
maxj ,
j ++;
{
j ++;
{
{
r1 , r2
r1 , r2
r1 , r2
}
}
}
= RandomInteger[
{
{
{
1 , len
1 , len
1 , len
}
}
}
,
,
,
{
{
{
2
}
}
}
];
= RandomInteger[
= RandomInteger[
2
2
];
];
If[restorevector , vec2 = vec1];
If[restorevector , vec2 = vec1];
vec2[[
]];
val2 = objfunc2[Ordering[vec2]];
vec2[[
vec2[[
{
{
{
r1 , r2
r1 , r2
r1 , r2
}
}
}
]] = vec2[[
]] = vec2[[
]] = vec2[[
{
{
{
r2 , r1
r2 , r1
r2 , r1
}
}
}
]];
]];
val2 = objfunc2[Ordering[vec2]];
If[val2 < best , j -;
val2 = objfunc2[Ordering[vec2]];
If[val2 < best , j -;
If[val2 < best , j -;
If[printFlag , Print[“locally improved” ,
If[printFlag , Print[“locally improved” ,
If[printFlag , Print[“locally improved” ,
{
{
{
best , val2
best , val2
best , val2
}
}
}
]]];
]]];
]]];
If[val2
If[val2
If[val2
(val1 > best&&RandomReal[] > keep) ,
Optimize`NMinimizeDump`vec = vec2;
val1
val1
val1
(val1 > best&&RandomReal[] > keep) ,
(val1 > best&&RandomReal[] > keep) ,
Optimize`NMinimizeDump`vec = vec2;
If[val2 < val1 , vec1 = vec2];
Optimize`NMinimizeDump`vec = vec2;
If[val2 < val1 , vec1 = vec2];
Search WWH ::




Custom Search