View source with raw comments or as raw
    1%% s(CASP) weight_seq_scasp.pl
    2
    3#include('select_instance.asp').
    4
    5?- query(Solution).    6
    7query([InnerTree, Color, W]) :-
    8    max_total_weight(MaxW),
    9    W .=<. MaxW,
   10    %       weight_seq_clp:num(Lenght),
   11    num(Lenght),
   12    create(0, Lenght, L),
   13    permutation(L, List),
   14    weight(List, InnerTree, Color, W).
   15
   16weight([L1, L2|Tree], [innerLeftRight(1, L1, L2)|InnerTree], [innerColor(1, Color, WNode)|ColorTree], Weight) :-
   17    Weight .=. WNode + WMid,
   18    leafWeightCardinality(L1, W1, _),
   19    colorWeight(W1, L2, Color, WNode),
   20    weightAux(1, [WNode|Tree], InnerTree, ColorTree, WMid).
   21
   22weightAux(N, [W1, L2|Tree], [innerLeftRight(N1, N, L2)|InnerTree], [innerColor(N1,Color, WNode)|ColorTree], Weight) :-
   23    N1 is N + 1,
   24    colorWeight(W1, L2, Color, WNode),
   25    weightAux(N1, [WNode|Tree], InnerTree, ColorTree, WMid),
   26    Weight is WNode + WMid.
   27
   28weightAux(_, [Weight], [], [], 0).
   29
   30
   31colorWeight(W1, L2, Color, W) :-
   32    leafWeightCardinality(L2, W2, C2),
   33    G is W2 + C2,
   34    R is W1 + W2,
   35    B is W1 + C2,
   36    choose(G,R,B,Color,W).
   37
   38choose(G,R,B,green,G) :-
   39    G .=<. R, G .=<. B.
   40choose(G,R,B,red,R) :-
   41    R .=<. B,
   42    not choose(G,R,B,green,G).
   43choose(G,R,B,blue,B) :-
   44    not choose(G,R,B,red,R),
   45    not choose(G,R,B,green,G).
   46
   47
   48permutation([], []).
   49
   50permutation(L, [T|Q]) :-
   51    select(T, L, L1),
   52    permutation(L1, Q).
   53
   54create(Max, Max, []).
   55create(N,   Max, [L|Ls]) :-
   56    N < Max,
   57    N1 is N + 1,
   58    atom_number(AN, N1),
   59    atom_concat(leaf, AN, L),
   60    create(N1, Max, Ls)