function U = DAT_milp(T)
comptime = cputime;
[n dump] = size(T);
T.Number = (1:n)';
T.BAS = cell(n,1);
M = sum(T{:,"Duration"})+1;
if isnan(M)
    disp(T)
    disp("WTF?")
end
for i =1:n
    if T{i,"Type"} == "BAS"
        T{i,"BAS"}{1} = [string(T.Properties.RowNames{i})];
    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

t = sdpvar(n,1);
m = 0;
for i = 1:n
    if T{i,"Type"} == "SAND"
        m = m+1;
        kids = T{i,"Children"}{1};
        for j = 1:(length(kids)-1)
            m = m+length(T{kids(j),"BAS"}{1}).*length(T{kids(j+1),"BAS"}{1});
        end
    elseif T{i,"Type"} == "OR"
        m=m+length(T{i,"Children"}{1});
    end
end
b = binvar(m,1);
Objective = t(n);
Constraints = [];
counter = 1;
for i = 1:n
    if T{i,"Type"} == "BAS"
        Constraints = [Constraints; t(i) >= T{i,"Duration"}];
    elseif T{i,"Type"} == "OR"
        oldcounter = counter;
        for c = T{i,"Children"}{1}
            Constraints = [Constraints; t(i) >= t(T{c,"Number"})-M*(1-b(counter))];
            counter = counter+1;
        end
        X = zeros(1,m);
        for j = oldcounter:(counter-1)
            X(j) = 1;
        end
        Constraints = [Constraints; X*b >= 1];
    elseif T{i,"Type"} == "AND"
        for c = T{i,"Children"}{1}
            Constraints = [Constraints; t(i) >= t(T{c,"Number"})];
        end
    else %T{i,"Type"} == "SAND"
        oldcounter = counter;
        kids = T{i,"Children"}{1};
        Constraints = [Constraints; t(i) >= t(T{kids(length(kids)),"Number"}); t(i) >= M*b(counter)];
        counter = counter+1;
        for j = 1:(length(kids)-1)
            Constraints = [Constraints; b(oldcounter) >= (1+t(T{kids(j),"Number"})-M)/M];
            for bas1 = T{kids(j),"BAS"}{1}
                for bas2 = T{kids(j+1),"BAS"}{1}
                    Constraints = [Constraints; b(oldcounter) >= (t(T{bas1,"Number"})-t(T{bas2,"Number"})+T{bas2,"Duration"})/M-b(counter)];
                    Constraints = [Constraints; b(oldcounter) >= (M-t(T{bas1,"Number"}))/M-(1-b(counter))];
                    counter = counter+1;
                end
            end
        end
    end
end
ops = sdpsettings('verbose',0,'mosek.MSK_DPAR_OPTIMIZER_MAX_TIME',3600);
sol = optimize(Constraints,Objective,ops);
%sol = optimize(Constraints,Objective);
U = value(t(n));
comptime = cputime-comptime;      
end