function [lb, z, s, cpu_time] = pwa_ctrl(A, B, x0, T, C, d, G, h, g, p, binary);
[n, m] = size(B);
K = length(A);

cvx_begin quiet
cvx_precision high
  variables z(n, K, T) x(n, T) u(m, T)
  if binary
    variable s(K, T-1) binary
  else
    variable s(K, T-1)
  end
  J = g(x(:,T));
  for t = 1:T-1
    for i = 1:K
      J = J + p(z(:,i,t), s(i,t));
    end
  end
  minimize(J);
  subject to
    s >= 0;
    x(:,1) == x0;
    for t = 1:T-1
      sum(s(:,t)) == 1;
      x(:,t) == sum(z(:,:,t), 2);
      Az_sum = 0;
      for i = 1:K
        Az_sum = Az_sum + A{i}*z(:,i,t);
        C*z(:,i,t) <= d*s(i,t)
        G{i}*z(:,i,t) <= h{i}*s(i,t)
      end
      C*x(:,T) <= d;
      x(:,t+1) == Az_sum + B*u(:,t);
    end
cvx_end

lb = cvx_optval;
cpu_time = cvx_cputime;
if isequal(cvx_status, 'Suboptimal')
  lb = nan;
end
