clingo return the max subset - answer-set-programming

I have the following code:
1{p(1..10)}10.
:- p(I*2).
and it shows 31 answers, But I just need the max subset p(1) p(3) p(5) p(7) p(9).
How can I do it?

You are searching for an optimization. Easiest fix would be adding
#maximize{I:p(I)}.
resulting in
Solving...
Answer: 1
p(9)
Optimization: -9
Answer: 2
p(5) p(9)
Optimization: -14
Answer: 3
p(7) p(9)
Optimization: -16
Answer: 4
p(5) p(7) p(9)
Optimization: -21
Answer: 5
p(1) p(5) p(7) p(9)
Optimization: -22
Answer: 6
p(3) p(5) p(7) p(9)
Optimization: -24
Answer: 7
p(1) p(3) p(5) p(7) p(9)
Optimization: -25
OPTIMUM FOUND
Don't worry about the negative score, clingo converts #maximize into #minimize, therefore the negation. So what does #maximize{I:p(I)}. do? In general you want to maximize over I for all p(I), which in this case is the sum of I. Just counting would be
#maximize{1,I:p(I)}.
resulting in
Answer: 1
p(9)
Optimization: -1
Answer: 2
p(5) p(9)
Optimization: -2
Answer: 3
p(5) p(7) p(9)
Optimization: -3
Answer: 4
p(3) p(5) p(7) p(9)
Optimization: -4
Answer: 5
p(1) p(3) p(5) p(7) p(9)
Optimization: -5
OPTIMUM FOUND

Related

fmincon with lower bound fails, even though solution is at initial point

I'm trying to minimize a non-linear objective function (my actual function is much more complicated than that, but I found that even this simple function illustrates the point), where I know that minimum is obtained at the initial point x0:
fun = #(x) x(1)^2+x(2)^2;
x0 = [0 0];
lb1 = [0 0];
lb2 = [-1 -1];
[xc1 fvalc1] = fmincon(fun, x0, [],[],[],[], lb1, [Inf Inf])
Which outputs:
>> xc1 = 1.0e-03 * [0.6457 0.6457]
>> fvalc1 = 8.3378e-07
However, both using a different lower bound or using fminsearch instead work correctly:
[xc2 fvalc2] = fmincon(fun, x0, [],[],[],[], lb2, [Inf Inf])
>> xc2 = [0 0]
>> fvalc2 = 0
[xs fvals] = fminsearch(fun, x0)
>> xs = [0 0]
>> fvals = 0
What goes wrong in the first fmincon call?
We can diagnose this using the output output argument as specified in the docs
[xc1, fvalc1, ~, output] = fmincon(fun, x0, [],[],[],[], lb1, [Inf Inf])
The value output.stepsize is the final step size taken in the iterative solving process. In this case:
output.stepsize
>> ans = 6.586e-4
The estimated minima was at x = [6.457e-4, 6.457e-4] and the lower bounds you've permitted are [0 0], so the solver is not permitted to take another step! Another step would give x = [-1.29e-5, -1.29e-5] which is outside of the boundaries.
When you allow the lower bounds to be [-1, -1] the solver can over-shoot the minimum and approach it from all directions.
Moreover, we can use the options input to get even better insight!
options.Display = 'iter';
[xc1, fvalc1, ~, output] = fmincon(fun, x0, [],[],[],[], lb1, [Inf Inf], [], options);
Printed to the command window we see this:
Your initial point x0 is not between bounds lb and ub; FMINCON
shifted x0 to strictly satisfy the bounds.
First-order Norm of
Iter F-count f(x) Feasibility optimality step
0 3 1.960200e+00 0.000e+00 9.900e-01
1 6 1.220345e-02 0.000e+00 8.437e-01 1.290e+00
2 9 4.489374e-02 0.000e+00 4.489e-02 1.014e-01
3 12 1.172900e-02 0.000e+00 1.173e-02 1.036e-01
4 15 3.453565e-03 0.000e+00 3.454e-03 4.953e-02
5 18 1.435780e-03 0.000e+00 1.436e-03 2.088e-02
6 21 4.659097e-04 0.000e+00 4.659e-04 1.631e-02
7 24 2.379407e-04 0.000e+00 2.379e-04 6.160e-03
8 27 6.048934e-05 0.000e+00 6.049e-05 7.648e-03
9 30 1.613884e-05 0.000e+00 1.614e-05 3.760e-03
10 33 5.096660e-06 0.000e+00 5.097e-06 1.760e-03
11 36 2.470360e-06 0.000e+00 2.470e-06 6.858e-04
12 39 8.337765e-07 0.000e+00 8.338e-07 6.586e-04
So your x0 is invalid! This is why the solver doesn't return the result with 1 iteration and lower bounds of [0 0].
fminsearch also works for the same reason - you've not imposed a lower bound on which the solution sits.

Image compression using orthogonal polynomial Transformation [duplicate]

I am working on Image compression based on orthogonal polynomial transformation. My input image is gray scale image of size 256*256. i divide this image into 4 by 4 blocks. and than apply the orthogonal polynomial operator on each block. but I am not getting correct coefficient.
Example:
If I have a block 4x4 of Input image:
I=[5 11 8 10;9 8 4 12; 1 10 11 4;19 6 15 7];
and my polynomial operator for n=4 is:
[M] = [p0 p1 p2 p3]=[1 -3 1 -3;1 -1 -1 9;1 1 -1 -9;1 3 1 3]
After finding the outer product:
[20 -24 24 -16; -24 84 -80 24;24 -80 84 -24;-16 24 -24 20]
when I applied this on input image blocks, my answer is:
[-396 172 88 -104; 1012 -248 -376 616 -972 320 436 -552; 492 -104 4 172]
which is wrong. It should be
[140 0 -6 -10; 32 -112 2 -174; 22 -30 8 -40;34 -54 84 -8]
what I have done wrong?
My MATLAB code to find orthogonal polynomial operator is given below.
clc
clear all
close all
ID=imread('cameraman.tif'); % input image
I=double(ID);
imshow(ID);
[r,c]=size(I);
y1=zeros(r,c);
x=1:4;
n=4;
mu=(n+1)/2;
p0=[1 1 1 1];
p1=x-mu;
p2=(x-mu).^2-(n.^2-1)/12;
p3=(x-mu).^3-((x-mu)*(3*n.^2-7))/20;
P=[p0;p1; p2; p3];
N=[1 1 1 1;-1.5 -0.5 0.5 1.5;1 -1 -1 1;-0.3 0.9 -0.9 0.3];
M=[1 1 1 1;-3 -1 1 3;1 -1 -1 1;-3 9 -9 3]; % after sacling
v=M';
O=M.*v;
T=O'; % Orthogonal polynomial operator
for i=1:4:r
for j=1:4:c
y1(i:i+3,j:j+3)=I(i:i+3,j:j+3).*(double(T));
end
end
figure,imshow(uint8(y1))
y1

How to apply orthogonal polynomial transformation on an image for image compression?

I am working on Image compression based on orthogonal polynomial transformation. My input image is gray scale image of size 256*256. i divide this image into 4 by 4 blocks. and than apply the orthogonal polynomial operator on each block. but I am not getting correct coefficient.
Example:
If I have a block 4x4 of Input image:
I=[5 11 8 10;9 8 4 12; 1 10 11 4;19 6 15 7];
and my polynomial operator for n=4 is:
[M] = [p0 p1 p2 p3]=[1 -3 1 -3;1 -1 -1 9;1 1 -1 -9;1 3 1 3]
After finding the outer product:
[20 -24 24 -16; -24 84 -80 24;24 -80 84 -24;-16 24 -24 20]
when I applied this on input image blocks, my answer is:
[-396 172 88 -104; 1012 -248 -376 616 -972 320 436 -552; 492 -104 4 172]
which is wrong. It should be
[140 0 -6 -10; 32 -112 2 -174; 22 -30 8 -40;34 -54 84 -8]
what I have done wrong?
My MATLAB code to find orthogonal polynomial operator is given below.
clc
clear all
close all
ID=imread('cameraman.tif'); % input image
I=double(ID);
imshow(ID);
[r,c]=size(I);
y1=zeros(r,c);
x=1:4;
n=4;
mu=(n+1)/2;
p0=[1 1 1 1];
p1=x-mu;
p2=(x-mu).^2-(n.^2-1)/12;
p3=(x-mu).^3-((x-mu)*(3*n.^2-7))/20;
P=[p0;p1; p2; p3];
N=[1 1 1 1;-1.5 -0.5 0.5 1.5;1 -1 -1 1;-0.3 0.9 -0.9 0.3];
M=[1 1 1 1;-3 -1 1 3;1 -1 -1 1;-3 9 -9 3]; % after sacling
v=M';
O=M.*v;
T=O'; % Orthogonal polynomial operator
for i=1:4:r
for j=1:4:c
y1(i:i+3,j:j+3)=I(i:i+3,j:j+3).*(double(T));
end
end
figure,imshow(uint8(y1))
y1

matlab fmincon error "Solver stopped prematurely"

I have a given path for 6 DOF manipulator for given knots. path(dof,knot)
path_f=[-0.5131 -0.6587 -1.0058 -1.4202 -1.7674 -1.9130
-0.8696 -0.6711 -0.1980 0.3667 0.8399 1.0383
-0.8961 -0.7433 -0.3789 0.0560 0.4205 0.5733
1.1714 0.9639 0.4691 -0.1215 -0.6163 -0.8238
-3.1000 -2.5800 -1.3400 0.1400 1.3800 1.9000
-1.1514 -0.9439 -0.4491 0.1415 0.6363 0.8438]
I generated cubic splines between each joints and and each time interval is defined as h(i) and I tried to find the minimum h value which is h(1)+h(2)+h(3)+h(4)+h(5).
to solve that problem I wrote a code:
options=optimset('Algorithm','sqp','Display','iter','DiffMinChange',1e-16,'DiffMaxChange',1e-4,'TolFun',1e-14,'TolX',1e-20,'MaxFunEvals',60000,'MaxIter',1000);
h0=[0.001; 0.001; 0.001; 0.001; 0.001]
h=fmincon(#(h)objecfun(h,path), h0, [], [], [], [], [0; 0; 0; 0; 0] , [], #(h) nonlconstraint(h,Robot,path_f,dof), options);
When I run the code it says:
Solver stopped prematurely.
fmincon stopped because it exceeded the iteration limit,
options.MaxIter = 1000 (the selected value).
After 5th iteration f(x) value doesn't change and the results:
Norm of First-order
Iter F-count f(x) Feasibility Steplength step optimality
0 6 2.500000e-01 2.001e+01 1.000e+00
1 12 3.903637e-01 8.013e+00 1.000e+00 6.374e-02 6.077e-01
2 18 5.115308e-01 2.485e+00 1.000e+00 5.914e-02 3.255e-01
3 24 5.519283e-01 4.258e-01 1.000e+00 2.941e-02 1.712e-01
4 30 5.528961e-01 1.288e-02 1.000e+00 5.382e-03 3.098e-02
5 36 5.530002e-01 4.399e-06 1.000e+00 2.153e-04 2.442e-02
6 42 5.530000e-01 3.403e-12 1.000e+00 1.305e-07 1.828e-06
7 48 5.530000e-01 8.704e-14 1.000e+00 1.050e-13 3.103e-07
8 54 5.530000e-01 8.349e-14 1.000e+00 3.211e-15 1.465e-07
9 65 5.530000e-01 6.573e-14 1.681e-01 2.713e-16 1.319e-07
10 72 5.530000e-01 3.020e-14 7.000e-01 9.286e-16 3.740e-08
I have no idea about what the reason behind it. Could you please help me?
Your f(x) probably is changing but you don't see it in this resolution. Look at the magnitude of your step size. Try scaling your problem or (if reasonable for your case) increasing DiffMinChange.

polyfit does not seem to fit data very well in matlab

I am using polyfit in matlab to plot a straight line of best fit through points. However, the best fit line is not as I would expect - the line does not look like it fits the data very well... does it? Here's my code, any help with improving this would be great. (Note, the axis limits I have set are important).
x = [0 1 10 5 1 1.5];
y = [-14 -18 -22 -26 -30 -34];
scatter(x,y)
hold on
p = polyfit(x,y,1);
f = polyval(p,x);
plot(x,f,'-r','linewidth',1.2)
ylim([-35 -10])
xlim([-30 30])
Your fit looks proper to me, but this might be what you're looking for instead
x = [0 1 10 5 1 1.5];
y = [-14 -18 -22 -26 -30 -34];
scatter(x,y)
hold on
p = polyfit(y,x,1);
f = polyval(p,y);
plot(f,y,'-r','linewidth',1.2)
ylim([-35 -10])
xlim([-30 30])
Here's a total least squares solution
data=[x(:), y(:)];
c=mean(data);
data=bsxfun(#minus,data,c);
[~,~,v]=svd(data);
d=v(:,end);
k=dot(d,c);
ezplot(#(x,y) x*d(1)+ y*d(2)-k, [-30 30 -35 -10])