diff --git a/@logicalZonotope/and.asv b/@logicalZonotope/and.asv index c5fcaf5..4ca24bf 100644 --- a/@logicalZonotope/and.asv +++ b/@logicalZonotope/and.asv @@ -27,21 +27,27 @@ function Z = and(Z1,Z2,varargin) % Last revision: --- %------------- BEGIN CODE -------------- +if(~isempty(Z1.c) & ~isempty(Z2.c)) newcen = Z1.c & Z2.c; +else +newcen =[]; +end if(~isempty(Z1.G) & ~isempty(Z2.G)) %c2 * G1 index =1; - for i=1:length(Z1.G) - newGen{index} = Z2.c & Z1.G{i}; - index=index+1; + if(~isempty(Z2.c)) + for i=1:length(Z1.G) + newGen{index} = Z2.c & Z1.G{i}; + index=index+1; + end end - - for i=1:length(Z2.G) - newGen{index} = Z1.c & Z2.G{i}; - index=index+1; + if(~isempty(Z1.c)) + for i=1:length(Z2.G) + newGen{index} = Z1.c & Z2.G{i}; + index=index+1; + end end - %G1 * G1 for i=1:length(Z1.G) [rows,cols]=size(Z1.G{i}); @@ -49,42 +55,48 @@ if(~isempty(Z1.G) & ~isempty(Z2.G)) Gcon=[]; for j=1:cols gZ1 = Z1.G{i}(:,j); - Gcon = [Gcon (gZ1 & Z2.G{k})]; + newGen{index} = (gZ1 & Z2.G{k}); end - newGen{index} = Gcon; + index=index+1; end end Z = logicalZonotope(newcen,newGen); +elseif(isempty(Z1.G) & isempty(Z2.G)) + Z = logicalZonotope(newcen,{}); + elseif (isempty(Z2.G)) %c2 * G1 index =1; - for i=1:length(Z1.G) - newGen{index} = Z2.c & Z1.G{i}; - index=index+1; + newGen ={}; + if(~isempty(Z2.c)) + for i=1:length(Z1.G) + newGen{index} = Z2.c & Z1.G{i}; + index=index+1; + end end - Z = logicalZonotope(newcen,newGen); elseif (isempty(Z1.G)) %c2 * G1 newGen = Z2.G; index =length(newGen)+1; - for i=1:length(Z2.G) - newGen{index} = Z1.c & Z2.G{i}; - index=index+1; + if(~isempty(Z1.c)) + for i=1:length(Z2.G) + newGen{index} = Z1.c & Z2.G{i}; + index=index+1; + end end - newGen{index} = Z2.c ; Z = logicalZonotope(newcen,newGen); - +end Z = unique(Z); end diff --git a/@logicalZonotope/and.m b/@logicalZonotope/and.m index c0ee8a9..d9723c7 100644 --- a/@logicalZonotope/and.m +++ b/@logicalZonotope/and.m @@ -27,31 +27,32 @@ % Last revision: --- %------------- BEGIN CODE -------------- +if(~isempty(Z1.c) & ~isempty(Z2.c)) newcen = Z1.c & Z2.c; +else +newcen =[]; +end if(~isempty(Z1.G) & ~isempty(Z2.G)) %c2 * G1 index =1; - for i=1:length(Z1.G) - newGen{index} = Z2.c & Z1.G{i}; - index=index+1; + if(~isempty(Z2.c)) + for i=1:length(Z1.G) + newGen{index} = Z2.c & Z1.G{i}; + index=index+1; + end end - - for i=1:length(Z2.G) - newGen{index} = Z1.c & Z2.G{i}; - index=index+1; + if(~isempty(Z1.c)) + for i=1:length(Z2.G) + newGen{index} = Z1.c & Z2.G{i}; + index=index+1; + end end - %G1 * G1 for i=1:length(Z1.G) [rows,cols]=size(Z1.G{i}); for k=1:length(Z2.G) - Gcon=[]; - for j=1:cols - gZ1 = Z1.G{i}(:,j); - Gcon = [Gcon (gZ1 & Z2.G{k})]; - end - newGen{index} = Gcon; + newGen{index} = (Z1.G{i} & Z2.G{k}); index=index+1; end end @@ -64,24 +65,27 @@ %c2 * G1 index =1; - for i=1:length(Z1.G) - newGen{index} = Z2.c & Z1.G{i}; - index=index+1; + newGen ={}; + if(~isempty(Z2.c)) + for i=1:length(Z1.G) + newGen{index} = Z2.c & Z1.G{i}; + index=index+1; + end end - Z = logicalZonotope(newcen,newGen); elseif (isempty(Z1.G)) %c2 * G1 newGen = Z2.G; index =length(newGen)+1; - for i=1:length(Z2.G) - newGen{index} = Z1.c & Z2.G{i}; - index=index+1; + if(~isempty(Z1.c)) + for i=1:length(Z2.G) + newGen{index} = Z1.c & Z2.G{i}; + index=index+1; + end end - newGen{index} = Z2.c ; diff --git a/@logicalZonotope/enclosePoints.asv b/@logicalZonotope/enclosePoints.asv index dd6c8d1..b72ab4e 100644 --- a/@logicalZonotope/enclosePoints.asv +++ b/@logicalZonotope/enclosePoints.asv @@ -26,17 +26,24 @@ function Z = enclosePoints(points,varargin) %------------- BEGIN CODE -------------- [dim,numOfPoints] = size(points); - +cen = []; +newpoints = []; for i = 1:numOfPoints if zeros(dim,1) ==points(:,i) cen =zeros(dim,1); else - noCenFlag =1; + newpoints= [newpoints points(:,i)]; end end +newpoints=unique(newpoints','rows')'; -gen = {points}; +for i = 1:numOfPoints + gen{i} = newpoints(:,i); +end +%points=unique(points','rows')'; +% cen =zeros(dim,1); +% gen = {points}; Z =logicalZonotope(cen,gen); end diff --git a/@logicalZonotope/enclosePoints.m b/@logicalZonotope/enclosePoints.m index fc6b609..aef5fe4 100644 --- a/@logicalZonotope/enclosePoints.m +++ b/@logicalZonotope/enclosePoints.m @@ -25,20 +25,37 @@ % Last revision: --- %------------- BEGIN CODE -------------- -[dim,numOfPoints] = size(points); +% [dim,numOfPoints] = size(points); % cen = []; % newpoints = []; -% for i = 1:numOfPoints +% for i = 1:numOfPoints % if zeros(dim,1) ==points(:,i) % cen =zeros(dim,1); % else % newpoints= [newpoints points(:,i)]; % end % end -%gen = {newpoints}; +% newpoints=unique(newpoints','rows')'; +% +% [row,cols] = size(newpoints); +% for i = 1:cols +% gen{i} = newpoints(:,i); +% end + +%points=unique(points','rows')'; +% cen =zeros(dim,1); +% gen = {points}; points=unique(points','rows')'; -cen =zeros(dim,1); -gen = {points}; +[dim,numOfPoints] = size(points); + +cen = points(:,1); +index =1; +for i =2:numOfPoints + gen{index} = xor(cen,points(:,i)); + index = index +1; +end + + Z =logicalZonotope(cen,gen); end diff --git a/@logicalZonotope/evaluate.asv b/@logicalZonotope/evaluate.asv new file mode 100644 index 0000000..a03e806 --- /dev/null +++ b/@logicalZonotope/evaluate.asv @@ -0,0 +1,78 @@ +function points = evaluate(Z1) +% and - overloads & operator, computes the intersection of two zonotopes +% +% Syntax: +% Z = not(Z1,Z2) +% +% Inputs: +% Z1 - zonotope +% Z2 - zonotope, +% +% Outputs: +% Z - zonotope object enclosing the and zonotope +% +% Example: +% zono1 = zonotope([4 2 2;1 2 0]); +% zono2 = zonotope([3 1 -1 1;3 1 2 0]); +% +% res = zono1 & zono2 +% +% figure +% hold on +% plot(zono1,[1,2],'r'); +% plot(zono2,[1,2],'b'); +% plot(res,[1,2],'g'); +% +% Other m-files required: none +% Subfunctions: none +% MAT-files required: none +% +% See also: none + +% Author: Amr Alanwar +% Written: 8-Sept-2022 +% Last update: +% +% +% Last revision: --- + +%------------- BEGIN CODE -------------- + +if ~isempty(Z1.G) +numOfgen = length( Z1.G ); + + + +points = []; +%for o=1:numOfgen +%for k=1:numOfgen + + +table=truth_table(numOfgen); +[rowsTable,colsTable]=size(table); +for i =1:rowsTable + + % [rowsGen,colsGen]=size(gen1); + onePoint=[ table(i,1)&Z1.G{1}]; + for j=2:numOfgen + onePoint =xor( onePoint, (table(i,j)&Z1.G{j}) ); + end + if ~isempty(Z1.c) + points = [ points xor(Z1.c,onePoint)]; + else + points = [ points onePoint]; + end +end +else +points = Z1.c; +end + + +points=unique(points','rows')'; +end + +function genBeta = computeGenBeta () + +end + +%------------- END OF CODE -------------- \ No newline at end of file diff --git a/@logicalZonotope/evaluate.m b/@logicalZonotope/evaluate.m index 9a34b53..c914a78 100644 --- a/@logicalZonotope/evaluate.m +++ b/@logicalZonotope/evaluate.m @@ -68,42 +68,11 @@ end +points=unique(points','rows')'; +end -% %for o=1:numOfgen -% for k=1:numOfgen -% gen1=Z1.G{k}; -% [rowsGen,colsGen]=size(gen1); -% table=truth_table(colsGen); -% [rowsTable,colsTable]=size(table); -% for i =1:rowsTable -% onePoint=[ table(i,1)&gen1(:,1)]; -% for j=2:colsGen -% onePoint =[ onePoint, (table(i,j)&gen1(:,j)) ]; -% end -% points = [ points onePoint]; -% end -% if k ==1 -% oldPoints = points; -% elseif k ==numOfgen -% points = xor(Z1.c,points); -% oldPoints = points; -% else -% points = xor(oldPoints,points); -% oldPoints = points; -% end -% end - -%end - -% cen = ; +% function genBeta = computeGenBeta () % -% operand = [ ]; -% -% -% for i =1:numOfgen -% points = [ points , norLogic(operand,0&gen1{i} ) , norLogic(operand , 1& gen1{i} )]; % end -points=unique(points','rows')'; -end %------------- END OF CODE -------------- \ No newline at end of file diff --git a/@logicalZonotope/mtimes.m b/@logicalZonotope/mtimes.m index 0af9b45..39ba706 100644 --- a/@logicalZonotope/mtimes.m +++ b/@logicalZonotope/mtimes.m @@ -83,22 +83,23 @@ for i =1:length(Z.G) [mrows,mcols]=size(matrix); - [grows,gcols]=size(Z.G); + [grows,gcols]=size(Z.G{i}); - for j=1:gcols + % for j=1:gcols for k =1:mrows temp = 0; for ii =1:grows - temp=temp| (matrix(k,ii) & Z.G{i}(ii,j) ); + temp=temp| (matrix(k,ii) & Z.G{i}(ii,1) ); end - result.G{i}(k,j) = temp; + result.G{i}(k,1) = temp; end - end + % end end Z = logicalZonotope(result.c,result.G); + Z = unique(Z); else Z = logicalZonotope(result.c,{}); end diff --git a/@logicalZonotope/not.m b/@logicalZonotope/not.m index 495560d..8deeca0 100644 --- a/@logicalZonotope/not.m +++ b/@logicalZonotope/not.m @@ -37,7 +37,11 @@ % Last revision: --- %------------- BEGIN CODE -------------- +if (~isempty(Z1.c)) [rows,cols] = size(Z1.c); +else +[rows,cols] = size(Z1.G{1}); +end Zone = logicalZonotope(ones(rows,1),[]); Z = xor(Zone,Z1); Z =unique(Z); diff --git a/@logicalZonotope/or.asv b/@logicalZonotope/or.asv index a4a7ac3..d45893f 100644 --- a/@logicalZonotope/or.asv +++ b/@logicalZonotope/or.asv @@ -40,15 +40,28 @@ function Z = or(Z1,Z2) [crows1,~] = size(Z1.c); [crows2,~] = size(Z2.c); -[grows1,gcols1] = size(Z1.G); -[grows2,gcols2] = size(Z2.G); - +done=0; if ~isempty( Z1.G) - if Z1.c == zeros(crows1,1) && Z1.G{1} - output = Z2 + [grows1,gcols1] = size(Z1.G{1}); + if (isequal(Z1.c,zeros(crows1,1)) && isequal(Z1.G{1},zeros(grows1,gcols1) )) + Z = Z2; + done =1; + + elseif (isempty(Z1.c,zeros(crows1,1)) && isequal(Z1.G{1},zeros(grows1,gcols1) )) + + end +elseif ~isempty( Z2.G) + [grows2,gcols2] = size(Z2.G{1}); + if (isequal(Z2.c,zeros(crows2,1)) && isequal(Z2.G{1},zeros(grows2,gcols2) )) + Z = Z1; + done =1; end end -Z = nand(nand(Z1,Z1),nand(Z2,Z2)); + + +if ~done + Z = nand(nand(Z1,Z1),nand(Z2,Z2)); +end Z =unique(Z); end diff --git a/@logicalZonotope/or.m b/@logicalZonotope/or.m index 571cdf5..3139b82 100644 --- a/@logicalZonotope/or.m +++ b/@logicalZonotope/or.m @@ -40,21 +40,30 @@ [crows1,~] = size(Z1.c); [crows2,~] = size(Z2.c); - +done=0; if ~isempty( Z1.G) [grows1,gcols1] = size(Z1.G{1}); if (isequal(Z1.c,zeros(crows1,1)) && isequal(Z1.G{1},zeros(grows1,gcols1) )) Z = Z2; done =1; - end -elseif ~isempty( Z2.G) + elseif (isempty(Z1.c) && isequal(Z1.G{1},zeros(grows1,gcols1) ) ) + Z = Z2; + done =1; + end +end +if ~isempty( Z2.G) [grows2,gcols2] = size(Z2.G{1}); if (isequal(Z2.c,zeros(crows2,1)) && isequal(Z2.G{1},zeros(grows2,gcols2) )) Z = Z1; done =1; - end + elseif (isempty(Z2.c) && isequal(Z2.G{1},zeros(grows2,gcols2) ) ) + Z = Z1; + done =1; + end end + + if ~done Z = nand(nand(Z1,Z1),nand(Z2,Z2)); end diff --git a/@logicalZonotope/unique.m b/@logicalZonotope/unique.m index 96ef511..dadfdfc 100644 --- a/@logicalZonotope/unique.m +++ b/@logicalZonotope/unique.m @@ -49,8 +49,10 @@ for i = 1:length(Z1.G) flagRedun =0; for j = 1:length(rednewGen) - if Z1.G{i} == rednewGen{j} - flagRedun =1; + if size(Z1.G{i}) == size(rednewGen{j}) + if double(Z1.G{i}) == double(rednewGen{j}) + flagRedun =1; + end end end if flagRedun ==0 diff --git a/@logicalZonotope/xor.m b/@logicalZonotope/xor.m index ad495b4..d27c4c6 100644 --- a/@logicalZonotope/xor.m +++ b/@logicalZonotope/xor.m @@ -37,7 +37,16 @@ % Last revision: --- %------------- BEGIN CODE -------------- -newCen = xor( Z1.c,Z2.c ); + +if(~isempty(Z1.c) & ~isempty(Z2.c)) + newCen = xor( Z1.c,Z2.c ); +elseif (isempty(Z1.c) & isempty(Z2.c)) + newCen =[]; +elseif isempty(Z1.c) + newCen = Z2.c ; +elseif isempty(Z2.c) + newCen = Z1.c ; +end if(isempty(Z1.G)) newGen = Z2.G; diff --git a/Logical_Zono_XOR.m b/Logical_Zono_XOR.m index 2b6e7cc..244f856 100644 --- a/Logical_Zono_XOR.m +++ b/Logical_Zono_XOR.m @@ -1,10 +1,11 @@ clear all c1 = [0;0]; -c2 = [0;1]; -%g1 = {[0 ;0],[1;0]}; -g1 = {[0 1;1 1]}; +c2 = [1;1]; +g1 = {[0 ;0],[1;0],[1 ;1]}; +%g1 = {[0 1;1 1]}; g2 = {[1;0]}; +%g2 = {[1 0;0 1]}; Z1 = logicalZonotope(c1,g1); Z2 = logicalZonotope(c2,g2); @@ -13,58 +14,58 @@ p1=evaluate(Z1) p2=evaluate(Z2) [~,p1Col]=size(p1); -% [~,p2Col]=size(p2); -% andPoints=[]; -% orPoints =[]; -% norPoints =[]; -% nandPoints =[]; -% xorPoints = []; -% notPointsZ1 = []; -% notPointsZ2 = []; -% for i =1:p1Col -% notPointsZ1 = [notPointsZ1, ~p1(:,i)]; -% for j=1:p2Col -% andPoints = [andPoints, p1(:,i)&p2(:,j)]; -% orPoints = [orPoints, p1(:,i)|p2(:,j)]; -% norPoints = [norPoints, norLogic(p1(:,i),p2(:,j))]; -% nandPoints = [nandPoints, nandLogic(p1(:,i),p2(:,j))]; -% xorPoints = [xorPoints, xor(p1(:,i),p2(:,j))]; -% end -% end -% -% for j=1:p2Col -% notPointsZ2 = [notPointsZ2, ~p2(:,j)]; -% end -% andPoints = unique(andPoints','rows')' -% orPoints = unique(orPoints','rows')' -% norPoints = unique(norPoints','rows')' -% nandPoints = unique(nandPoints','rows')' -% xorPoints = unique(xorPoints','rows')' -% notPointsZ2 = unique(notPointsZ2','rows')' -% notPointsZ1 = unique(notPointsZ1','rows')' -% -% % using CORA -% notZ1 = not(Z1); -% notZ2 = not(Z2); -% Z1NorZ2 = nor(Z1 , Z2) -% Z1AndZ2 = and(Z1, Z2) -% Z1OrZ2 = or(Z1, Z2) -% Z1NandZ2 = nand(Z1, Z2) -% Z1XorZ2 = xor(Z1,Z2); -% -% notZ1Points= evaluate(notZ1) -% notZ2Points= evaluate(notZ2) -% Z1AndZ2Points= evaluate(Z1AndZ2) -% Z1OrZ2Points= evaluate(Z1OrZ2) -% Z1NorZ2Points= evaluate(Z1NorZ2) -% Z1NandZ2Points= evaluate(Z1NandZ2) -% Z1XorZ2Points= evaluate(Z1XorZ2) -% -% -% cNor = xor(c1,c2); -% g1Nor = g1{1}; -% g2Nor = g2{1}; -% AXorB=[ xor(cNor,xor(0*g1Nor,0*g2Nor)) xor(cNor,xor(0*g1Nor,1*g2Nor)) xor(cNor,xor(1*g1Nor,0*g2Nor)) xor(cNor,xor(1*g1Nor,1*g2Nor))] +[~,p2Col]=size(p2); +andPoints=[]; +orPoints =[]; +norPoints =[]; +nandPoints =[]; +xorPoints = []; +notPointsZ1 = []; +notPointsZ2 = []; +for i =1:p1Col + notPointsZ1 = [notPointsZ1, ~p1(:,i)]; + for j=1:p2Col + andPoints = [andPoints, p1(:,i)&p2(:,j)]; + orPoints = [orPoints, p1(:,i)|p2(:,j)]; + norPoints = [norPoints, norLogic(p1(:,i),p2(:,j))]; + nandPoints = [nandPoints, nandLogic(p1(:,i),p2(:,j))]; + xorPoints = [xorPoints, xor(p1(:,i),p2(:,j))]; + end +end + +for j=1:p2Col + notPointsZ2 = [notPointsZ2, ~p2(:,j)]; +end +andPoints = unique(andPoints','rows')' +orPoints = unique(orPoints','rows')' +norPoints = unique(norPoints','rows')' +nandPoints = unique(nandPoints','rows')' +xorPoints = unique(xorPoints','rows')' +notPointsZ2 = unique(notPointsZ2','rows')' +notPointsZ1 = unique(notPointsZ1','rows')' + +% using CORA +notZ1 = not(Z1); +notZ2 = not(Z2); +Z1NorZ2 = nor(Z1 , Z2) +Z1AndZ2 = and(Z1, Z2) +Z1OrZ2 = or(Z1, Z2) +Z1NandZ2 = nand(Z1, Z2) +Z1XorZ2 = xor(Z1,Z2); + +notZ1Points= evaluate(notZ1) +notZ2Points= evaluate(notZ2) +Z1AndZ2Points= evaluate(Z1AndZ2) +Z1OrZ2Points= evaluate(Z1OrZ2) +Z1NorZ2Points= evaluate(Z1NorZ2) +Z1NandZ2Points= evaluate(Z1NandZ2) +Z1XorZ2Points= evaluate(Z1XorZ2) + + +cNor = xor(c1,c2); +g1Nor = g1{1}; +g2Nor = g2{1}; +AXorB=[ xor(cNor,xor(0*g1Nor,0*g2Nor)) xor(cNor,xor(0*g1Nor,1*g2Nor)) xor(cNor,xor(1*g1Nor,0*g2Nor)) xor(cNor,xor(1*g1Nor,1*g2Nor))] %%% points = [1 0 0; 0 0 1]; diff --git a/classes/DataDrivenRA.asv b/classes/DataDrivenRA.asv index a085532..979fe9c 100644 --- a/classes/DataDrivenRA.asv +++ b/classes/DataDrivenRA.asv @@ -70,6 +70,11 @@ classdef DataDrivenRA < handle options; AB; UL; + X_0TL; + X_1TL; + ndigits; + dataStep; + ULogicalZono; end methods @@ -91,7 +96,9 @@ classdef DataDrivenRA < handle obj.yData = obj.mocaplogsRA(:,6)+obj.dataNoiseStateFactor*rand(length(obj.mocaplogsRA(:,6)),1); obj.timeDataState = obj.mocaplogsRA(:,3); - datavector = [1:2:10,200:300,900:1000,2000:2100, 3000:3100];%[1:2:10,400:450,900:1000,2000:2100]; + obj.dataStep=10; + %datavector = [1:datastep:10,200:datastep:300,900:datastep:1000,2000:datastep:2100, 3000:datastep:3100]; + datavector= 1:obj.dataStep:3000;%[1:2:10,400:450,900:1000,2000:2100]; obj.totalsamples=length(datavector);%ceil(length(obj.mocaplogsRA(:,1))-1); obj.X_0T = [obj.xData(datavector),obj.yData(datavector)]'; @@ -118,9 +125,10 @@ classdef DataDrivenRA < handle obj.maxU2 = max(obj.U_full(2,:)); obj.minU2= min(obj.U_full(2,:)); - obj.numXGrids=101; - obj.numYGrids=101; - obj.numUGrids=101; + obj.numXGrids=1023; + obj.numYGrids=1023; + obj.numUGrids=1023; + obj.ndigits = ceil(log2((obj.numXGrids-1)^2)); obj.hX=linspace(obj.minXState-1,obj.maxXState+1,obj.numXGrids);%for 128 resolution [~,~,obj.binX]=histcounts(obj.xData(datavector),obj.hX); obj.hY=linspace(obj.minYState-1,obj.maxYState+1,obj.numYGrids);%for 128 resolution @@ -145,7 +153,7 @@ classdef DataDrivenRA < handle end % convert to binary - pointB = dec2bin(points); + pointB = dec2bin(points,obj.ndigits); [rows,cols]=size(pointB); for i =1:rows @@ -161,7 +169,7 @@ classdef DataDrivenRA < handle end dataBinary=str2num(newpointB)'; - pointBU = dec2bin(pointsU); + pointBU = dec2bin(pointsU,obj.ndigits); [rows,cols]=size(pointBU); for i =1:rows row=[]; @@ -179,7 +187,7 @@ classdef DataDrivenRA < handle obj.X_0TL = [dataBinary(:,1:end-1)]; obj.X_1TL = [dataBinary(:,2:end)]; obj.UL = [UBinary(:,1:end-1)]; - + obj.ULogicalZono = logicalZonotope.enclosePoints(obj.UL); obj.AB = obj.X_1TL * pinv([obj.X_0TL;obj.UL ]); %obj.AB = X_1TL * pinv([X_0TL]); @@ -231,14 +239,25 @@ classdef DataDrivenRA < handle end %function R_data = reachLLogic(obj,loc,steps) - function R1 = reachLogic(obj,loc,steps) + function R_logic = reachLogic(obj,loc,steps) binaryVector = getGridIndex(obj,loc); - R0 = logicalZonotope(binaryVector,[]); - %R1=R0; - Ulog = logicalZonotope(obj.UL(:,1:end-1),[]); - [rowsR0, colsR0]= size(R0.c); - %R1 = or((obj.AB(:,1:rowsR0)>0.5) * R0,(obj.AB(:,rowsR0+1:end)>0.5)* Ulog); - R1 = todecimal(obj.AB(:,1:rowsR0)>0.7) * R0; + R0 = logicalZonotope.enclosePoints(binaryVector); + %R_logic=R0; + %Ulog = logicalZonotope(obj.UL(:,1:end-1),[]); + if isempty(R0.c) + [rowsR0, colsR0]= size(R0.c); + end + + if obj.ndigits == rowsR0 + %R1 = (obj.AB(1:rowsR0,1:rowsR0)>0.7) * R0; + halfWay = obj.hX(ceil(length(obj.hX)/2)); + quant = 0.2; + R_logic = or((obj.AB(:,1:rowsR0)>quant) * R0,(obj.AB(:,rowsR0+1:end)>quant)* obj.ULogicalZono); + else + R_logic=R0; + disp("error number of bits") + end + %R1=R0; end @@ -256,22 +275,31 @@ classdef DataDrivenRA < handle decPoints = [decPoints,d]; end % grid number to (x,y) - Ydata = floor( (decPoints-1)./obj.numXGrids) +1; - Xdata = decPoints - obj.numXGrids.*(Ydata -1) ; - location = [obj.hX(Xdata) obj.hY(Ydata) ]; + % remove zeros added by enclose points + newdecPoints=[]; + for i=1:length(decPoints) + if(decPoints(i) ~=0) + newdecPoints=[ newdecPoints decPoints(i)]; + end + end + + Ydata = floor( (newdecPoints)./obj.numXGrids) +1; + Xdata = newdecPoints - obj.numXGrids.*(Ydata -1) ; + location = [obj.hX(Xdata) ; obj.hY(Ydata) ]; end - function binaryVector = getGridIndex(obj,loc) + function binaryVector = getGridIndex(obj,locPoints) - [~,~,obj.binxR]=histcounts(loc(1),obj.hX); - [~,~,obj.binyR]=histcounts(loc(2),obj.hY); + [~,~,obj.binxR]=histcounts(locPoints(1,:),obj.hX); + [~,~,obj.binyR]=histcounts(locPoints(2,:),obj.hY); point = (obj.binyR-1)*obj.numXGrids + obj.binxR; % convert to binary - pointB = dec2bin(point); + pointB = dec2bin(point,obj.ndigits); + [rows,cols]=size(pointB); for i =1:rows diff --git a/classes/DataDrivenRA.m b/classes/DataDrivenRA.m index 68db913..247d7b8 100644 --- a/classes/DataDrivenRA.m +++ b/classes/DataDrivenRA.m @@ -239,20 +239,24 @@ function prepareUWLip(obj,ZepsFlag,moreinputdata) end %function R_data = reachLLogic(obj,loc,steps) - function R1 = reachLogic(obj,loc,steps) + function R_logic = reachLogic(obj,loc,steps) binaryVector = getGridIndex(obj,loc); - R0 = logicalZonotope(binaryVector,[]); - %R1=R0; - %Ulog = logicalZonotope(obj.UL(:,1:end-1),[]); - [rowsR0, colsR0]= size(R0.c); + R0 = logicalZonotope.enclosePoints(binaryVector); + %R_logic=R0; + %%%Ulog = logicalZonotope(obj.UL(:,1:end-1),[]); + if ~isempty(R0.c) + [rowsR0, colsR0]= size(R0.c); + else + [rowsR0, colsR0]= size(R0.G{1}); + end if obj.ndigits == rowsR0 %R1 = (obj.AB(1:rowsR0,1:rowsR0)>0.7) * R0; - halfWay = obj.hX(ceil(length(obj.hX)/2)); + %halfWay = obj.hX(ceil(length(obj.hX)/2)); quant = 0.2; - R1 = or((obj.AB(:,1:rowsR0)>quant) * R0,(obj.AB(:,rowsR0+1:end)>quant)* obj.ULogicalZono); + R_logic = or((obj.AB(:,1:rowsR0)>quant) * R0,(obj.AB(:,rowsR0+1:end)>quant)* obj.ULogicalZono); else - R1=R0; + R_logic=R0; disp("error number of bits") end @@ -273,22 +277,31 @@ function prepareUWLip(obj,ZepsFlag,moreinputdata) decPoints = [decPoints,d]; end % grid number to (x,y) - Ydata = floor( (decPoints-1)./obj.numXGrids) +1; - Xdata = decPoints - obj.numXGrids.*(Ydata -1) ; + % remove zeros added by enclose points + newdecPoints=[]; + for i=1:length(decPoints) + if(decPoints(i) ~=0) + newdecPoints=[ newdecPoints decPoints(i)]; + end + end + + Ydata = floor( (newdecPoints)./obj.numXGrids) +1; + Xdata = newdecPoints - obj.numXGrids.*(Ydata -1) ; location = [obj.hX(Xdata) ; obj.hY(Ydata) ]; end - function binaryVector = getGridIndex(obj,loc) + function binaryVector = getGridIndex(obj,locPoints) - [~,~,obj.binxR]=histcounts(loc(1),obj.hX); - [~,~,obj.binyR]=histcounts(loc(2),obj.hY); + [~,~,obj.binxR]=histcounts(locPoints(1,:),obj.hX); + [~,~,obj.binyR]=histcounts(locPoints(2,:),obj.hY); point = (obj.binyR-1)*obj.numXGrids + obj.binxR; % convert to binary pointB = dec2bin(point,obj.ndigits); + [rows,cols]=size(pointB); for i =1:rows diff --git a/run_CAR_logic.asv b/run_CAR_logic.asv index 424fb44..9dd2ef7 100644 --- a/run_CAR_logic.asv +++ b/run_CAR_logic.asv @@ -160,13 +160,14 @@ while (t_last - t_start) < t_stop drawnow; end if walltime >= plotZonotime + DRA.timeStepDataIn*stepsReach - loc = [z(1);z(2)]; - - R_logic=DRA.reachLogic( loc,stepsReach ); + loc = [z(1) z(1)+0.2 z(1) z(1)+0.2 z(1)-0.2 z(1) z(1)-0.2 ; ... + z(2) z(2) z(2)+0.2 z(2)+0.2]; + + R_logic=DRA.reachLogic(loc,stepsReach); locations=DRA.getPoints(R_logic); - ZLoc = enclosePoints(locations); + ZLoc = mptPolytope.enclosePoints([locations locations+0.1]); updatePlotZono(h_logic,ZLoc); - + R_data=DRA.reachDataLip(zonotope([z(1);z(2)]),stepsReach); for ii =1:stepsReach updatePlotZono(h_reach(ii),R_data{ii},[1,2],'r'); diff --git a/run_CAR_logic.m b/run_CAR_logic.m index 7a54192..da22676 100644 --- a/run_CAR_logic.m +++ b/run_CAR_logic.m @@ -160,13 +160,14 @@ drawnow; end if walltime >= plotZonotime + DRA.timeStepDataIn*stepsReach - loc = [z(1);z(2)]; + loc = [z(1) z(1)+0.05 z(1) z(1)+0.05 z(1)-0.05 z(1) z(1)-0.05 ; ... + z(2) z(2) z(2)+0.05 z(2)+0.05 z(2) z(2)-0.05 z(2)-0.05]; - R_logic=DRA.reachLogic( loc,stepsReach ); + R_logic=DRA.reachLogic(loc,stepsReach); locations=DRA.getPoints(R_logic); - ZLoc = zonotope.enclosePoints([locations locations+0.1]); - updatePlotPoly(h_logic,ZLoc); - + ZLoc = zonotope.enclosePoints([locations]); + updatePlotZono(h_logic,ZLoc); + R_data=DRA.reachDataLip(zonotope([z(1);z(2)]),stepsReach); for ii =1:stepsReach updatePlotZono(h_reach(ii),R_data{ii},[1,2],'r');