function [A,gam,fval] = maxaff3(c1,c2,m) global nstate lenseq H C1 C2 C1 = c1; C2 = c2; nstate = m; %lenseq = n; T = 1; adim = m; gdim = 2*m; xdim = adim + gdim; % box constraints %LB = zeros(adim,1)+1e-10; %UB = ones(adim,1); LB = zeros(xdim,1)+1e-10; LB(adim+1:end) = LB(adim+1:end) - 1e9; % no box constraints on phi UB = ones(xdim,1); UB(adim+1:end) = UB(adim+1:end) + 1e9; % no box constraints on phi H = constrict(m); rind=0; Aing = zeros(0,gdim); mm = repmat([2],[1 m]); minds = list_all_ind(mm); for s=[-1 1] for jind = 1:size(minds,1) rind = rind+1; iseq = minds(jind,:); rho = ~all(iseq==iseq(1)); for j=1:m i = iseq(j); cind = coord2ind([i j],[2 m]); Aing(rind,cind) = s; Bing(rind,1) = c1+c2*rho; end end end Ain = assembleblock({zeros(0,adim),Aing}); Bin = Bing; A0 = rand(1,m); G0 = randn(2,m); %G0(:,1) = G0(:,1) + 1e6; %G0(:,2) = G0(:,2) - 1e6; x0 = [A0(:); G0(:)]; [x,fval] = fmincon(@myobjm,x0,Ain,Bin,[],[],LB,UB); global A FN a = reshape(x(1:adim),[1 m]); b = 1-a; A = [a; b]; gam = reshape(x(adim+1:end),[2 m]); %g = gam(2,:); %gg = repmat(g,[m-2,1]); %gam = [gam; gg]; %if fval<0 % keyboard %end return function y = myobjm(x) global nstate lenseq H C1 C2 m = nstate; %n = lenseq; T = 1; adim = m; gdim = 2*m; xdim = adim + gdim; a = reshape(x(1:adim),[1 m]); b = 1-a; A = [a; b]; gam = reshape(x(adim+1:end),[2 m]); %g = gam(2,:); %gg = repmat(g,[m-2,1]); %gam = [gam; gg]; s = sum(sum( A.*gam)); s = abs(s); A = A(1,:); y = C1 - abs(s) + C2*max(H*A(:)); return function H = constrict(m) % stricture matrix T = 1; SIGNS = 2*list_all_ind(repmat([2],[1 m]))-3; H = zeros(0,m); rind = 0; %I1 = [1 2]; %I2 = setdiff(1:m,I1); I1 = 1; I2 = 2:m; %for i1=I1 % for i2=I2 for i1=1:m-1 for i2=i1+1:m for s = 1:size(SIGNS,1) rind = rind+1; ss = SIGNS(s,:); %for j=1:m for j=1 ind1 = coord2ind([j i1],[1 m]); ind2 = coord2ind([j i2],[1 m]); H(rind,ind1) = ss(j); H(rind,ind2) = -ss(j); end end end end return