20 views (last 30 days)

Greetings,

I am writing a piece of code that composes and evaluates logical expressions for my research. It picks one of the 26 minimally functionally complete operator sets, chooses the number of variables and then composes expressions based on those criteria.

I have worked on this problem for some time now and I have reluctantly used Matlab's 'eval' command to evaluate these logical expressions. Although I do have it working for the "in series" case, I have considerable difficulty expanding it to the "parallel" case.

I have provided my script and the necessary functions below. I have also commented the script so that (hopefully) you may follow along. As you will see, my difficulties arise in the following line:

ExpResult(1+jj,k) = eval(Syntax{k});

I have read quite a bit about the eval function and its limitations as well as the considerable disadvantages of using it while developing dynamic variables. I still feel that all things considered this particular methodology for this particular problem was still necessary. However, I could be incorrect in that assessment. At this point, my objective is to get a working version of this script and then modify. If you could assist in that effort, I would be very thankful.

The script is as follows:

% This script stochastically evaluates propositional formulas of arity-2

% It also evaluates based on a certain set of atoms.

% In addition, this script employs T and F as constants which is

% different than the previous versions.

clc; % Clear output screen

AtomNum = 5; % Number of variables used

AtomMax = 5; % Maximum number of variables (for later)

LL = 100; % Number of evaluations before save

syms G; % For rest of code

N = 100; % Number of iterations to run

R(1:N) = 0; % Initialize Random number dimensions

% Create generalized atoms instead of p, q, r, etc...

for j = 1:AtomMax

for k=1:N

Atom{j,k} = strcat('p(', num2str(j),',',num2str(k),')');

end

end

% Create CDFs

RV1(1,1:N) = rand(1,N); % Create an array of random variables to use

RV1(2,1:N) = RV1(1,1:N) + rand(1,N);

RV1(1,1:N) = RV1(1,1:N)./RV1(2,1:N);

RV1(2,1:N) = RV1(2,1:N)./RV1(2,1:N);

RV2(1,1:N) = rand(1,N); % Create an array of random variables to use

RV2(2,1:N) = RV2(1,1:N) + rand(1,N);

RV2(3,1:N) = RV2(2,1:N) + rand(1,N);

RV2(1,1:N) = RV2(1,1:N) ./RV2(3,1:N);

RV2(2,1:N) = RV2(2,1:N) ./RV2(3,1:N);

RV2(3,1:N) = RV2(3,1:N) ./RV2(3,1:N);

RV3(1,1:N) = zeros(1,N);

for j=2:AtomMax+1

RV3(j,1:N) = RV3(j-1,1:N) + rand(1,N);

end

for j=1:AtomMax+1

RV3(j,1:N) = RV3(j,1:N)./RV3(AtomMax+1,1:N);

end

% Initialize string variables

for j=1:N

Constant{j} = '';

Q{j} = '';

BString{j} = '';

str1{j} = '';

end

p(1:AtomMax,1:N) = zeros(AtomMax,N);

ExpResult(1:2^AtomMax,1:N) = 0;

% Distribute computations over many processors.

% 1) Create well-formed propositional formulas

% 2) evaluate formulas

% 3) store results

parfor k = 1:N

Syntax{k} = 'Z'; % Seed for all propositional formulas is a nondescript atom

VarCnt(k) = 1; % Number of variables within a formula are counted

W(k) = randi([0, 2^52]); % To ensure longer formulas are more included

W(k) = log(W(k))/log(2);

OperNum(k) = 1+ floor(W(k));

L(k) = randi([1,26]); % Randomly select which language to use

% There are 26 minimally functionally complete languages. Choose

% which one to use. Must be coded in this way.

switch L(k)

case 1 % {NAND}

for jj = 1:OperNum(k)

R(k) = randi([1, VarCnt(k)]);

Syntax{k} = regexprep(Syntax{k},'Z','NAND(Z,Z)', R(k));

VarCnt(k) = VarCnt(k) + 1;

end

case 2 % {NOR}

for jj = 1:OperNum(k)

R(k) = randi([1, VarCnt(k)]);

Syntax{k} = regexprep(Syntax{k},'Z','NOR(Z,Z)', R(k));

VarCnt(k) = VarCnt(k) + 1;

end

case 3 % {NOT, AND}

for jj = 1:OperNum(k)

R(k) = randi([1, VarCnt(k)]);

r = rand; % <- shows up as [0,0,0,...0] in workspace

if r <= RV1(1,k)

Syntax{k} = regexprep(Syntax{k},'Z','not(Z)', R(k));

else

Syntax{k} = regexprep(Syntax{k},'Z','and(Z,Z)', R(k));

VarCnt(k) = VarCnt(k) + 1;

end

end

case 4 % {NOT, OR}

for jj = 1:OperNum(k)

R(k) = randi([1, VarCnt(k)]);

r = rand;

if r <= RV1(1,k)

Syntax{k} = regexprep(Syntax{k},'Z','not(Z)', R(k));

else

Syntax{k} = regexprep(Syntax{k},'Z','or(Z,Z)', R(k));

VarCnt(k) = VarCnt(k) + 1;

end

end

case 5 % {NLA, LA} Eigenanti

for jj = 1:OperNum(k)

R(k) = randi([1, VarCnt(k)]);

r = rand;

if r <= RV1(1,k)

Syntax{k} = regexprep(Syntax{k},'Z','NLA(Z,Z)', R(k));

else

Syntax{k} = regexprep(Syntax{k},'Z','LA(Z,Z)', R(k));

end

VarCnt(k) = VarCnt(k) + 1;

end

case 6 % {NRA, RA} Eigen Anti

for jj = 1:OperNum(k)

R(k) = randi([1, VarCnt(k)]);

r = rand;

if r <= RV1(1,k)

Syntax{k} = regexprep(Syntax{k},'Z','NRA(Z,Z)', R(k));

else

Syntax{k} = regexprep(Syntax{k},'Z','RA(Z,Z)', R(k));

end

VarCnt(k) = VarCnt(k) + 1;

end

case 7 % {NLA, RA} Eigen Reverse Bit Flip

for jj = 1:OperNum(k)

R(k) = randi([1, VarCnt(k)]);

r = rand;

if r <= RV1(1,k)

Syntax{k} = regexprep(Syntax{k},'Z','NLA(Z,Z)', R(k));

else

Syntax{k} = regexprep(Syntax{k},'Z','RA(Z,Z)', R(k));

end

VarCnt(k) = VarCnt(k) + 1;

end

case 8 % {NRA, LA} Eigen Reverse Bit Flip

for jj = 1:OperNum(k)

R(k) = randi([1, VarCnt(k)]);

r = rand;

if r <= RV1(1,k)

Syntax{k} = regexprep(Syntax{k},'Z','NRA(Z,Z)', R(k));

else

Syntax{k} = regexprep(Syntax{k},'Z','LA(Z,Z)', R(k));

end

VarCnt(k) = VarCnt(k) + 1;

end

case 9 % {LA, XOR}

for jj = 1:OperNum(k)

R(k) = randi([1, VarCnt(k)]);

r = rand;

if r <= RV1(1,k)

Syntax{k} = regexprep(Syntax{k},'Z','LA(Z,Z)', R(k));

else

Syntax{k} = regexprep(Syntax{k},'Z','xor(Z,Z)', R(k));

end

VarCnt(k) = VarCnt(k) + 1;

end

case 10 % {RA, XOR}

for jj = 1:OperNum(k)

R(k) = randi([1, VarCnt(k)]);

r = rand;

if r <= RV1(1,k)

Syntax{k} = regexprep(Syntax{k},'Z','RA(Z,Z)', R(k));

else

Syntax{k} = regexprep(Syntax{k},'Z','xor(Z,Z)', R(k));

end

VarCnt(k) = VarCnt(k) + 1;

end

case 11 % {NLA, NXOR}

for jj = 1:OperNum(k)

R(k) = randi([1, VarCnt(k)]);

r = rand;

if r <= RV1(1,k)

Syntax{k} = regexprep(Syntax{k},'Z','NLA(Z,Z)', R(k));

else

Syntax{k} = regexprep(Syntax{k},'Z','NXOR(Z,Z)', R(k));

end

VarCnt(k) = VarCnt(k) + 1;

end

case 12 % {NRA, NXOR}

for jj = 1:OperNum(k)

R(k) = randi([1, VarCnt(k)]);

r = rand;

if r <= RV1(1,k)

Syntax{k} = regexprep(Syntax{k},'Z','NRA(Z,Z)', R(k));

else

Syntax{k} = regexprep(Syntax{k},'Z','NXOR(Z,Z)', R(k));

end

VarCnt(k) = VarCnt(k) + 1;

end

case 13 % {NOT,RA}

for jj = 1:OperNum(k)

R(k) = randi([1, VarCnt(k)]);

r = rand;

if r <= RV1(1,k)

Syntax{k} = regexprep(Syntax{k},'Z','not(Z)', R(k));

else

Syntax{k} = regexprep(Syntax{k},'Z','RA(Z,Z)', R(k));

VarCnt(k) = VarCnt(k) + 1;

end

end

case 14 % {NOT,LA}

for jj = 1:OperNum(k)

R(k) = randi([1, VarCnt(k)]);

r = rand;

if r <= RV1(1,k)

Syntax{k} = regexprep(Syntax{k},'Z','not(Z)', R(k));

else

Syntax{k} = regexprep(Syntax{k},'Z','LA(Z,Z)', R(k));

VarCnt(k) = VarCnt(k) + 1;

end

end

case 15 % {NOT,NRA}

for jj = 1:OperNum(k)

R(k) = randi([1, VarCnt(k)]);

r = rand;

if r <= RV1(1,k)

Syntax{k} = regexprep(Syntax{k},'Z','not(Z)', R(k));

else

Syntax{k} = regexprep(Syntax{k},'Z','NRA(Z,Z)', R(k));

VarCnt(k) = VarCnt(k) + 1;

end

end

case 16 % {NOT,NLA}

for jj = 1:OperNum(k)

R(k) = randi([1, VarCnt(k)]);

r = rand;

if r <= RV1(1,k)

Syntax{k} = regexprep(Syntax{k},'Z','not(Z)', R(k));

else

Syntax{k} = regexprep(Syntax{k},'Z','NLA(Z,Z)', R(k));

VarCnt(k) = VarCnt(k) + 1;

end

end

case 17 % {NLA, T}

for jj = 1:OperNum(k)

R(k) = randi([1, VarCnt(k)]);

Syntax{k} = regexprep(Syntax{k},'Z','NLA(Z,Z)', R(k));

VarCnt(k) = VarCnt(k) + 1;

end

case 18 % {NRA, T}

for jj = 1:OperNum(k)

R(k) = randi([1, VarCnt(k)]);

Syntax{k} = regexprep(Syntax{k},'Z','NRA(Z,Z)', R(k));

VarCnt(k) = VarCnt(k) + 1;

end

case 19 % {LA, F}

for jj = 1:OperNum(k)

R(k) = randi([1, VarCnt(k)]);

Syntax{k} = regexprep(Syntax{k},'Z','LA(Z,Z)', R(k));

VarCnt(k) = VarCnt(k) + 1;

end

case 20 % {RA, F}

for jj = 1:OperNum(k)

R(k) = randi([1, VarCnt(k)]);

Syntax{k} = regexprep(Syntax{k},'Z','RA(Z,Z)', R(k));

VarCnt(k) = VarCnt(k) + 1;

end

case 21 % {AND, NXOR, F}

for jj = 1:OperNum(k)

R(k) = randi([1, VarCnt(k)]);

r = rand;

if r <= RV1(1,k)

Syntax{k} = regexprep(Syntax{k},'Z','and(Z,Z)', R(k));

else

Syntax{k} = regexprep(Syntax{k},'Z','NXOR(Z,Z)', R(k));

end

VarCnt(k) = VarCnt(k) + 1;

end

case 22 % {OR, XOR, T}

for jj = 1:OperNum(k)

R(k) = randi([1, VarCnt(k)]);

r = rand;

if r <= RV1(1,k)

Syntax{k} = regexprep(Syntax{k},'Z','or(Z,Z)', R(k));

else

Syntax{k} = regexprep(Syntax{k},'Z','xor(Z,Z)', R(k));

end

VarCnt(k) = VarCnt(k) + 1;

end

case 23 % {AND, XOR, NXOR}

for jj = 1:OperNum(k)

R(k) = randi([1, VarCnt(k)]);

r = rand;

if r <= RV2(1,k)

Syntax{k} = regexprep(Syntax{k},'Z','and(Z,Z)', R(k));

elseif r <= RV2(2,k)

Syntax{k} = regexprep(Syntax{k},'Z','xor(Z,Z)', R(k));

else

Syntax{k} = regexprep(Syntax{k},'Z','NXOR(Z,Z)', R(k));

end

VarCnt(k) = VarCnt(k) + 1;

end

case 24 % {OR, XOR, NXOR}

for jj = 1:OperNum(k)

R(k) = randi([1, VarCnt(k)]);

r = rand;

if r <= RV2(1,k)

Syntax{k} = regexprep(Syntax{k},'Z','or(Z,Z)', R(k));

elseif r <= RV2(2,k)

Syntax{k} = regexprep(Syntax{k},'Z','xor(Z,Z)', R(k));

else

Syntax{k} = regexprep(Syntax{k},'Z','NXOR(Z,Z)', R(k));

end

VarCnt(k) = VarCnt(k) + 1;

end

case 25 % {AND, XOR, T}

for jj = 1:OperNum(k)

R(k) = randi([1, VarCnt(k)]);

r = rand;

if r <= RV1(1,k)

Syntax{k} = regexprep(Syntax{k},'Z','and(Z,Z)', R(k));

else

Syntax{k} = regexprep(Syntax{k},'Z','xor(Z,Z)', R(k));

end

VarCnt(k) = VarCnt(k) + 1;

end

case 26 % {OR, NXOR, F}

for jj = 1:OperNum(k)

R(k) = randi([1, VarCnt(k)]);

r = rand;

if r <= RV1(1,k)

Syntax{k} = regexprep(Syntax{k},'Z','or(Z,Z)', R(k));

else

Syntax{k} = regexprep(Syntax{k},'Z','NXOR(Z,Z)', R(k));

end

VarCnt(k) = VarCnt(k) + 1;

end

end

% For those languages which have TRUE or FALSE constants, we have an

% additional step to consider

if (L(k) >= 17 && L(k) <= 22) || L(k) == 25 || L(k) == 26

if L(k) == 17 || L(k) == 18 || L(k) == 22 || L(k) == 25

% Add atom for TRUE

Constant{k} = '1';

else

% Add atom for FALSE

Constant{k} = '0';

end

% Select which atom index to assign to each nondescript atom

for jj = 1:VarCnt(k)

for m = 1:AtomNum

r = rand;

if r >= RV3(m,k) && r < RV3(m+1,k)

Syntax{k} = regexprep(Syntax{k},'Z',Atom{m,k}, randi([1, VarCnt(k)-jj+1]));

end

end

end

Syntax{k} = regexprep(Syntax{k},'Z',Constant{k});

else

% Select which atom index to assign to each nondescript atom

for jj = 1:VarCnt(k)

for m = 1:AtomNum

r = rand;

if r >= RV3(m,k) && r < RV3(m+1,k)

Syntax{k} = regexprep(Syntax{k},'Z',Atom{m,k},randi([1, VarCnt(k)-jj+1]));

end

end

end

Syntax{k} = regexprep(Syntax{k},'Z',Atom{AtomNum,k});

end

% Just like in a truth table with multiple variables so too must we

% set up our variables to do the same thing.

for jj = 0:2^AtomNum-1

BString{k} = dec2bin(jj);

for n = 1:length(dec2bin(2^(AtomNum)-1))-length(dec2bin(jj))

BString{k} = strcat('0',BString{k});

end

p(:,k) = str2num(BString{k} (:));

% Up until this point everything works as it should. Each 'Syntax'

% is ready to be evaluated. The problem is actually evaluating it.

% Substitute 'p(*,k)' values w/actual values

ExpResult(1+jj,k) = eval(Syntax{k}); % <- This line is the issue

% Q = strcat(Q,num2str(ExpResult(1+j,1)));

end

% The rest of the routine is here. I left it out since it is not

% necessaey

end

Also, the necessary functions are below. They are the remaining Boolean functions of arity 2 which are not standard Matlab functions:

function [V] = NOR(p,q)

V = not(or(p,q));

end

function [V] = NAND(p,q)

V = not(and(p,q));

end

function [V] = LA(p,q)

V = or(not(xor(p,q)),not(p));

end

function [V] = NLA(p,q)

V = and(xor(p,q),p);

end

function [V] = NRA(p,q)

V = and(xor(p,q),not(p));

end

function [V] = RA(p,q)

V = or(not(xor(p,q)),p);

end

function [V] = NXOR(p,q)

V = not(xor(p,q));

end

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 12 Comments

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/296919-using-the-eval-function-in-parfor#comment_381012

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/296919-using-the-eval-function-in-parfor#comment_381012

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/296919-using-the-eval-function-in-parfor#comment_381015

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/296919-using-the-eval-function-in-parfor#comment_381015

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/296919-using-the-eval-function-in-parfor#comment_381018

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/296919-using-the-eval-function-in-parfor#comment_381018

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/296919-using-the-eval-function-in-parfor#comment_381084

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/296919-using-the-eval-function-in-parfor#comment_381084

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/296919-using-the-eval-function-in-parfor#comment_381103

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/296919-using-the-eval-function-in-parfor#comment_381103

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/296919-using-the-eval-function-in-parfor#comment_381180

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/296919-using-the-eval-function-in-parfor#comment_381180

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/296919-using-the-eval-function-in-parfor#comment_381223

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/296919-using-the-eval-function-in-parfor#comment_381223

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/296919-using-the-eval-function-in-parfor#comment_381327

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/296919-using-the-eval-function-in-parfor#comment_381327

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/296919-using-the-eval-function-in-parfor#comment_381496

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/296919-using-the-eval-function-in-parfor#comment_381496

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/296919-using-the-eval-function-in-parfor#comment_381545

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/296919-using-the-eval-function-in-parfor#comment_381545

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/296919-using-the-eval-function-in-parfor#comment_381621

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/296919-using-the-eval-function-in-parfor#comment_381621

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/296919-using-the-eval-function-in-parfor#comment_381622

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/296919-using-the-eval-function-in-parfor#comment_381622

Sign in to comment.