how to show all the maximum result in clingo when using the maximize? - answer-set-programming

I'm currently writing an asp program.
when I comment the line #maximize{S:ce(S)}. within the program, the answer shows as follows
omit the Answer 1 to 6.
Answer: 7
review(harris,english) review(ben,math) review(ben,chinese) review(nick,math) review(ben,english) review(nick,chinese) **ce(4)**
Answer: 8
review(harris,english) review(ben,math) review(ben,chinese) review(nick,math) review(nick,english) review(nick,chinese) **ce(4)**
SATISFIABLE
in which the answer 7 and answer 8 have the same ce value of 4.
when I uncomment the line #maximize{S:ce(S)}. the answer shows as follows
Answer: 1
review(ben,chinese) review(nick,math) ce(2) review(harris,math) review(ben,english) review(nick,english) review(nick,chinese)
Optimization: -2
Answer: 2
review(harris,english) review(ben,math) review(ben,chinese) ce(3) review(harris,math) review(nick,english) review(nick,chinese)
Optimization: -3
Answer: 3
review(harris,english) review(ben,math) review(ben,chinese) review(nick,math) **ce(4)** review(nick,english) review(nick,chinese)
Optimization: -4
OPTIMUM FOUND
there is only one result of ce(4), but I'd like to get all the result of the maximum value of ce, what should I do to get the two result with the value ce(4).

Maybe you could share your command when executing clingo. I simplified your program as I am not aware of the problem you are encoding.
review(harris,english).
review(ben,math).
review(ben,chinese).
review(nick,math).
review(ben,english) | review(nick, english). % miss one in your problem
review(nick,chinese).
ce(4).
#maximize{S:ce(S)}.
Basically, the missing condition of your problem is the enumeration of review(ben,english) and review(nick, english).
If executing with command clingo 0 filename.lp the output would generate only one answer set:
Answer: 1
review(harris,english) review(ben,math) review(ben,chinese) review(nick,math) review(nick,chinese) ce(4) review(ben,english)
Optimization: -4
OPTIMUM FOUND
Similar to your attempt, if I comment the last line, two answer sets will be shown:
Answer: 1
review(harris,english) review(ben,math) review(ben,chinese) review(nick,math) review(nick,chinese) ce(4) review(ben,english)
Answer: 2
review(harris,english) review(ben,math) review(ben,chinese) review(nick,math) review(nick,chinese) ce(4) review(nick,english)
SATISFIABLE
But if you uncomment the last line and add --opt-mode=optN after the original command, the output would be:
Answer: 1
review(harris,english) review(ben,math) review(ben,chinese) review(nick,math) review(nick,chinese) ce(4) review(ben,english)
Optimization: -4
Answer: 1
review(harris,english) review(ben,math) review(ben,chinese) review(nick,math) review(nick,chinese) ce(4) review(ben,english)
Optimization: -4
Answer: 2
review(harris,english) review(ben,math) review(ben,chinese) review(nick,math) review(nick,chinese) ce(4) review(nick,english)
Optimization: -4
OPTIMUM FOUND
Not sure if this is what you want. You might as well refer to clingo guide regarding the parameter of --opt-mode:
--opt-mode=mode Configure handling of optimization statements. Available values for mode are:
opt Compute an optimal model (requires --models=0).
enum Enumerate models with costs less than or equal to some fixed bound
(cf. --opt-bound).
optN Compute optimum, then enumerate optimal models.
ignore Ignore any optimization statements during computation.

Related

Histogram rebinning in Matlab 2020a, when I don't have a Matlab histogram object

I have two vectors that describe a histogram: bin values/labels and bin count.
I'd like to import those into a Matlab histogram object so that I can change parameters more easily, such as the number of bins.
Here is a simplification what I have:
Center bin values:
BinValues(1:21) = [-10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10];
and count values per bin
CountValues(1:21) = [ 16 12 3 21 7 8 2 6 0 3 4 6 1 8 3 25 16 5 7 10 16];
My actual histogram has 800000 bins, and I want to vary the number of bins.
In my simplified example above, it would be something like reducing the bins from 21 to 15 or 21 to 11.
What I have can be graphed as a simple bar graph.
bar(BinValues,CountValues);
But, if I want to experiment with different numbers of bins, I think it would be best to use the histogram function/object in Matlab, but I'm not sure how I do this from what I have.
OK Update:
I tried this:
h=histogram('BinEdges',[-10.5:10.5] , 'BinCounts', CountValues);
Now I have a nice histogram, but if I use fewerbins(h), I get an error saying I can't do that while BinCountsMode is in "manual". If I modify it to be in "auto" mode, then my original histogram disappears.
The brute force solution would be to generate a huge array with repeated values as dictated by binCount values, but that seems like a dumb way to solve this problem.

How to sum a series of values, ignoring any NaN values?

I use MATLAB version R2015a.
I get a series answer from solving the optimal problem several times, and I want to get their sum and average them. However, some of them are NaN. How do I write code to ignore those NaN and sum the others which are not NaN?
Option 1: toolbox free solution using sum and isnan from base MATLAB.
A = [1 2 3 4 5 6 7 8 9 NaN];
sum(A(~isnan(A))) % No toolbox required
Option 2: nansum (see this answer from OP)
Note: nansum requires the Statistics toolbox.
nansum(A) % Requires Statistics toolbox
Code tested using MATLAB R2018b.
Update from comments
Great suggestion from #Cris Luengo for those with more recent versions. Requires no toolbox.
sum(A,'omitnan') % No toolbox required
Another suggestion from #Ben Voigt for some applications. Also requires no toolbox.
sum(A(isfinite(A))) % No toolbox required
You can use inbuilt functions as suggested in the above answer. If you want to know the logic and use a loop..you can follow as shown below:
A = [NaN 1 2 NaN 3 4 7 -1 NaN] ;
count = 0 ;
thesum = 0 ;
for i = 1:length(A)
if ~isnan(A(i))
count = count+1 ;
thesum = thesum+A(i) ;
end
end
You can use the omitnan argument
A = [1 2 3 4 5 6 7 8 9 NaN];
s = sum( A, 'omitnan' )
Note, this is literally the same code as used by the nansum function from the Statistics toolbox, which was introduced before R2006a, so I would think compatibility is pretty good.

Strange behaviour of MATLAB combnk function

I am trying to generate all combination of 2 elements in a given range of numbers. I am using 'combnk' function as follows.
combnk(1:4,2)
ans =
3 4
2 4
2 3
1 4
1 3
1 2
combnk(1:6,2)
ans =
1 2
1 3
1 4
1 5
1 6
2 3
2 4
2 5
2 6
3 4
3 5
3 6
4 5
4 6
5 6
The order of combinations returned appears to change. I need to know the order in advance for my program to work properly.
Is there any solution to make sure I get the combinations in a consistent order?
Also, why is MATLAB showing this strange behavior?
The only solution I can think of so far is to first check the 1st entry of the result matrix and flip it up side down using 'flipud' function.
Update: By a little bit of experimenting I noticed the reverse order occurs only when the length of the set of numbers is less than 6. This is why combnk(1:6,2) produce the 'correct' order. Where as combnk(1:5,2) produce the results backwards. This is still big problem.
You could try nchoosek instead of combnk. I don't have the matlab statistics toolbox (only octave), so I don't know if nchoosek has any significant disadvanvatages.
This will solve the ordering issue:
a=combnk(1:4,2);
[~,idx]=sortrows(a);
aNew=a(idx,:);
I don't know why MATLAB is showing this behavior.

data rearrangement in matlab

I have the following code
[X1,Y1]=meshgrid(1:5,1:5);
z=X1.^2+Y1.^2
[X2,Y2]=meshgrid([1 2 3 3.5 4 5],[1 2 3 3.5 4 5]);
z2=interp2(X1,Y1,z,X2,Y2)
mesh(X2,Y2,z2)
Is there any way to stucture data such as command mesh(z2) would produce the same result ?
I am not exactly sure what you are asking, but you could certainly cut out a few lines in your code to do the same thing:
[X2,Y2]=meshgrid([1 2 3 3.5 4 5],[1 2 3 3.5 4 5]);
z2=X2.^2+Y2.^2;
mesh(X2,Y2,z2);
Or, you could just do:
[X1,Y1]=meshgrid(1:5,1:5);
z=X1.^2+Y1.^2;
mesh(z);
For this particular example, I don't think you will see much of a difference between the two methods.

devise a perceptron to jugde parity such as 1 2 3 4

Devise a perceptron, achieve a function that judges parity of 1 2 3 4 ....using matlab I have train a neural network, but it has very large variance.
I want to ask how to express sample?
If I directly use 1 2 3 4 5...as sample, the variance is very large. In the other words, the neural network is not used to classfiy.
I want to ask if the other functions can be used to transform the sample?
This is the program:
P= [0 1 2 3 4 5 6 7 8 9 10 11 12 13 14]; % Question if I can use another form to express the sample
T= [1 0 1 0 1 0 1 0 1 0 1 0 1 0 1];
net=newp([-1 10],1);
net.trainParam.epochs = 40;
net=train(net,P,T);
Y=sim(net,P)
E1=mae(Y-T)
plotpv(P,Y);
plotpc(net.iw{1},net.b{1})
I'm not sure I fully understand your question... but, I thought it worth mentioning that the (single layer) perceptron is (famously) unable to compute exclusive-or (XOR). The binary logic XOR function is equivalent to calculating parity for two bits. For this reason, while I'm not familiar with that specific Matlab package, I expect you to need a multi-layer perceptron... perhaps one with more layers than bits in the input string. If you were to use a perceptron model with too few layers - I'd expect you to fail to train it to calculate parity bits.
Calculating parity is not a task to which the perceptron is ideally suited. :)