function [U T] = DAT_naive(T)
[n dump] = size(T);
nBAS = sum(T{:,"Type"}=="BAS");
T.Attacks = cell(n,1);
T.Orders = cell(n,1);
T.BAS = cell(n,1);
T.BASno = zeros(n,1);
d = zeros(nBAS,1);
counter = 1;
for i = 1:n
    T{i,"Attacks"}{1} = {};
    T{i,"Orders"}{1} = {};
    if T{i,"Type"} == "BAS"
        T{i,"BAS"}{1} = [string(T.Properties.RowNames{i})];
        T{i,"BASno"} = counter;
        d(counter) = T{i,"Duration"};
        counter = counter+1;
    else
        T{i,"BAS"}{1} = [];
        for c = T{i,"Children"}{1}
            if length(T{i,"BAS"}{1}) == 0
                T{i,"BAS"}{1} = T{c,"BAS"}{1};
            else
                T{i,"BAS"}{1} = union(T{i,"BAS"}{1},T{c,"BAS"}{1});
            end
        end
    end
end
for i = 1:n
    if T{i,"Type"} == "BAS"
        T{i,"Attacks"}{1}{1} = zeros(nBAS,1);
        T{i,"Attacks"}{1}{1}(T{i,"BASno"}) = 1;
        T{i,"Orders"}{1}{1} = zeros(nBAS);
    elseif T{i,"Type"} == "OR"
        for c = T{i,"Children"}{1}
            T{i,"Attacks"}{1} = [T{i,"Attacks"}{1} T{c,"Attacks"}{1}];
            T{i,"Orders"}{1} = [T{i,"Orders"}{1} T{c,"Orders"}{1}];
        end
    elseif T{i,"Type"} == "AND"
        A = {zeros(nBAS,1)};
        O = {zeros(nBAS)};
        for c = T{i,"Children"}{1}
            [A,O] = combine_AND(A,O,T{c,"Attacks"}{1},T{c,"Orders"}{1});
        end
        T{i,"Attacks"}{1} = A;
        T{i,"Orders"}{1} = O;
    else
        kids = T{i,"Children"}{1};
        A = T{kids(1),"Attacks"}{1};
        O = T{kids(1),"Orders"}{1};
        for k = 1:(length(kids)-1)
            bas1 = zeros(nBAS,1);
            for b = T{kids(k),"BAS"}{1}
                bas1(T{b,"BASno"}) = 1;
            end
            bas2 = zeros(nBAS,1);
            for b = T{kids(k+1),"BAS"}{1}
                bas2(T{b,"BASno"}) = 1;
            end
            [A,O] = combine_SAND(A,O,bas1,T{kids(k+1),"Attacks"}{1},T{kids(k+1),"Orders"}{1},bas2);
        end
        T{i,"Attacks"}{1} = A;
        T{i,"Orders"}{1} = O;
    end
end
[dump m] = size(T{n,"Attacks"}{1});
F(1) = sum(T{:,"Duration"})+1;
for j = 1:m
    F(j+1) = Scorer(T{n,"Attacks"}{1}{j},T{n,"Orders"}{1}{j},d);
end
U = min(F);
end


function [A,O] = combine_AND(A1,O1,A2,O2)
A = {};
O = {};
    for i = 1:length(A1)
        for j = 1:length(A2)
            a = max(A1{i},A2{j});
            o = TransClose(max(O1{i},O2{j}));
            if IsSPO(o)
                A = [A a];
                O = [O o];
            end
        end
    end
end

function [A,O] = combine_SAND(A1,O1,bas1,A2,O2,bas2)
A = {};
O = {};
[nBAS dump] = size(bas1);
for i = 1:length(A1)
    for j = 1:length(A2)
        a = max(A1{i},A2{j});
        o = max(O1{i},O2{j});
        for k1 = 1:nBAS
            for k2 = 1:nBAS
                if a(k1)*bas1(k1)*a(k2)*bas2(k2) == 1
                    o(k1,k2) = 1;
                end
            end
        end
        o = TransClose(o);
        if IsSPO(o)
            A = [A a];
            O = [O o];
        end
    end
end
end