Hi again, Doug,
Here's a model which does the following:
1) Find the optimal (minimal) value of the sides,
2) Find the minimum sum of the second value of each side, where "second value" means the second in an increasing list.
It uses the decompositions sort/2 and permutation3/3 for this.
I guess it's still not really what you want, but it might give you some ideas.
```
% Doug:
% "I would like a solution that controls the outcome, to allow, for example,
% finding the second lowest value of each side."
go2 =>
% Step1: Find the optimal value of each side
s(_B,LS),
println(min=LS),
nl,
% Step 2: Find the optimal solution with the minimum sum of the
% second lowest value of each side
s(B,LS),
nl.
s(B,LS) =>
B = [B0, B1, B2, B3, B4, B5, B6, B7, B8, B9],
B :: 0..9,
all_different(B),
/*
4 rows, triangular shape,
B0, B1 & B2, B3 &B4 & B5, B6 & B7 & B8 & B9
*/
Left = [B0 , B1 , B3 , B6],
Right = [B0 , B2 , B5 , B9],
Bottom = [B6 , B7 , B8 , B9],
LS #= sum(Left),
RS #= sum(Right),
BS #= sum(Bottom),
LS #= RS,
RS #= BS,
Sides = flatten([Left, Right, Bottom]),
SumSides #= sum(Sides),
if var(LS) then
solve($[min(LS)],Vars),
else
% Minimize the sum of the second value in each side
Left2 = new_list(4),
Left2 :: 0..9,
sorted(Left,Left2),
Right2 = new_list(4),
Right2 :: 0..9,
sorted(Right,Right2),
Bottom2 = new_list(4),
Bottom2 :: 0..9,
sorted(Bottom,Bottom2),
% The second value in each side
V = [Left2[2],Right2[2],Bottom2[2]],
solve($[min(sum(V))],V++Vars)
end,
println(Vars),
println(Left),
println(Right),
println(Bottom),
println(eachSideTotal=LS),
nl,
nl.
%
% The permutation from A <-> B using the permutation P
%
permutation3(A,P,B) =>
foreach(I in 1..A.length)
PI #= P[I],
BI #= B[I],
element(PI, A, BI)
end.
%
% L2 contains the elements in L but sorted.
%
sorted(L,L2) =>
N = L.len,
P = new_list(N),
P :: 1..N,
all_different(P),
permutation3(L,P,L2),
increasing(L2).
```
The output is
[0,3,5,8,9,7,2,4,6,1,39]
[0,3,8,2]
[0,5,7,1]
[2,4,6,1]
eachSideTotal = 13
min = 13
[0,4,5,8,9,6,1,3,7,2,39]
[0,4,8,1]
[0,5,6,2]
[1,3,7,2]
eachSideTotal = 13
"""
/Hakan