I am new to ASP programming and finding it a bit hard to understand how ASP works. I am using clingo tool for ASP coding. Currently, I am working on a seating arrangement problem. The original problem is a bit complicated as it involves lots of variables and constraints. I have started with a basic version of it which is as follows:
4 persons need to be seated in a horizontal row having 4 seats. There are no constraints among these persons. Each person can move left/right to any other person. The final goal is straightforward
"any person can sit at any location provided it is available"
The only condition is to save the individual steps about how the goal is reached.
To solve it, I have used a predicate
seated(A,B,Shift,Dir,t) which says that at time t A has shifted Shift units in the Dir from B
Example
seated(A,B,left,2,t) implies A has shifted 2 units to the left of B at time t
I have used two files
init.lp describing the initial positions and final goal
person(a).
person(b).
person(c).
person(d).
init(pos(a,0)).
init(pos(b,0)).
init(pos(c,0)).
init(pos(d,0)).
goal(pos(a,1)).
goal(pos(b,2)).
goal(pos(c,3)).
goal(pos(d,4)).
rules.lp where the rules are written
#include<incmode>.
#program base.
% Define
holds(F,0) :- init(F).
%{possible_location(X,Y,1) : person(X),Y = 1..4}.
%%possible_location(X,Y,1) : person(X),Y = 1..4.
#program step(t).
% Generate any seating arrangement
1 { seated(X,Z,Shift,Dir,t) : person(X), person(Z), X != Z,Shift = 1..3, Dir = (left;right) } 1.
% Test
% Remove the seated predicates which the shift is not possible
:- seated(X,Z,Shift,right,t), holds(pos(Z,Pos),t-1), Shift + Pos > 4.
:- seated(X,Z,Shift,left,t), holds(pos(Z,Pos),t-1), Pos - Shift < 1.
% Remove the seated predicates if the final position after shifting is already occupied
:- seated(X,Z,Shift,right,t), holds(pos(Z,Pos),t-1), Pos>0, holds(pos(A,Shift + Pos),t-1), A!=X.
:- seated(X,Z,Shift,left,t), holds(pos(Z,Pos1),t-1), Pos1>0, holds(pos(A,Pos1 - Shift),t-1), A!=X.
% Define
seateded(X,t) :- seated(X,Z,Shift,Dir,t).
% Relating the current move to the current state %
holds(pos(X,Y),t) :- seated(X,Y,Shift,Dir,t).
% This is for shifting the state to the next time %
holds(pos(X,Z),t) :- holds(pos(X,Z),t-1), not seateded(X,t).
#program check(t).
% Test
:- query(t), goal(F), not holds(F,t).
% Display
#show seated/5.
#show query/1.
On running the above files, it gives an error
rule_new.lp:21:75-87: info: operation undefined:
(Pos1-Shift)
I have googled it and found out that this error comes when I have not handled any edge case such as X/Y and Y=0 is not handled. In this case, I don't see any edge case. Hence, I am not able to rectify the error.
I have documented the rules so that I can explain the thought process behind writing the rules. I have spent a couple of hours solving it and still couldn't figure out the solution. If my thinking is not in the right direction then I would appreciate if I can get some other solution to the above problem.
I'm afraid I didn't manage to understand your solution (or problem), but using the option --text can help you figure this error out.
According to the error message the problem is in this rule:
seated(X,Z,Shift,left,t),holds(pos(Z,Pos1),t-1), Pos1 >0, holds(pos(A,Pos1 - Shift),t-1), A !=X.
You try to subtract Shift from Pos1. Both Variables should be substituted by integers to make this work.
Shift comes from the seated predicate and can take a value between 1..3, fine.
Pos1 comes from the holds(pos(_,Pos1),_) predicate.
In your rule:
holds(pos(X,Y),t) :- seated(X,Y,Shift,Dir,t).
You clearly derive holds predicates where Y is a person, aka. a, b, c, or d.
So you try to compute 3-a which clingo refuses to do and drops the according rule, raising this warning.
Related
I have a constrained nonlinear optimization problem, "A". Inside the computation is an om.Group which I'll call "B" that requires a nonlinear solve. Whether "B" finds a solution or crashes seems to depend on its initial conditions. So far I've found that some of the initial conditions given to "B" are inconsistent with the constraints on "A", and that this seems to be contributing to its propensity for crashing. The constraints on "A" can be computed before "B".
If the objective of "A" could be computed before "B" then I would put "A" in its own group and have it pass its known-good solution to "B". However, the objective of "A" can only be computed as a result of the converged solution of "B". Is there a way to tell OpenMDAO or the optimizer (right now I'm using ScipyOptimizerDriver and the SLSQP method) that when it chooses a new point in design-variable space, it should check that the constraints of "A" hold before proceeding to "B"?
A slightly simpler example (without the complication of an initial guess) might be:
There are two design variables 0 < x1 < 1, 0 < x2 < 1.
There is a constraint that x2 >= x1.
Minimize f(sqrt(x2 - x1), x1) where f crashes if given imaginary inputs. How can I make sure that the driver explores the design space without giving f a bad input?
I have two proposed solutions. The best one is highly problem dependent. You can either raise an AnalysisError or use numerical clipping.
import numpy as np
import openmdao.api as om
class SafeComponent(om.ExplicitComponent):
def setup(self):
self.add_input('x1')
self.add_input('x2')
self.add_output('y')
def compute(self, inputs, outputs):
x1 = inputs['x1']
x2 = inputs['x2']
diff = x1 - x2
######################################################
# option 1: raise an error, which causes the
# optimizer line search to backtrack
######################################################
# if (diff < 0):
# raise om.AnalysisError('invalid inputs: x2 > x1')
######################################################
# option 2: use numerical clipping
######################################################
if (diff < 0):
diff = 0.
outputs['y'] = np.sqrt(diff)
# build the model
prob = om.Problem()
prob.model.add_subsystem('sc', SafeComponent(), promotes=['*'])
prob.setup()
prob['x1'] = 10
prob['x2'] = 20
prob.run_model()
print(prob['y'])
Option 1: raise an AnalysisError
Some optimizers are set up to handle this well. Others are not.
As of V3.7.0, the OpenMDAO wrappers for SLSQP from scipy and pyoptsparse, and the SNOPT/IPOPT wrappers from pyoptsparse all handle AnalysisErrors gracefully.
When the error is raised, the execution stops and the optimizer recognizes a failed case. It backtracks on the linesearch a bit to try and get out of the situation. It will usually try a few steps backwards, but at some point it will give up. So the success of this situation depends a bit on why you ended up in the bad part of the space and how much the gradients are pushing you back into it.
This solution works very well with fully analytic derivatives. The reason is that (most) gradient based optimizers will only ever ask for function evaluations along a line search operation. So that means that, as long as a clean point is found, you're always able to be able to compute derivatives at that point as well.
If you're using finite-differences, you could end a line search right near the error condition, but not violating it (e.g. x1=1, x2=.9999999). Then during the FD step to compute derivatives, you might end up tripping the error condition and raising the error. The optimizer is not going to be able to recover from this condition. Errors during FD steps will effectively kill the whole opt.
So, for this reason I never recommend the AnalysisError approach if you're suing FD.
Option 2: Numerical Clipping
If you optimizer wrapper does not have the ability to handle an AnalysisError, you can try some numerical clipping instead. You can add a filter in your calcs to to keep the values numerically safe. However, you obviously need to use this very carefully. You should at least add an additional constraint that forces the optimizer to keep away from the error condition when converged (e.g. x1 >= x2).
One important note: if you provide analytic derivatives, include the clipping in them!
Sometimes the optimizer just wants to pass through this bad region on its way to the answer. In that case, the simple clipping I show here is probably fine. Other times it wants to ride the constraint (be sure you add that constraint!!!) and then you probably want a more smoothly varying type of clipping. In other words don't use a simple if-condition. Smooth the round corner a bit, and maybe make the value asymptotically approach 0 from a very small value. This way you have a c1 continuous function and the derivatives won't got to exactly 0 for these inputs.
I have a following lab where i was asked to write the command matlab lines for these questions:
If initial Conditions are: x(0)=[2;0].๐๐๐๐ ๐กโ๐ ๐๐๐ ๐๐๐๐ ๐ ๐ฆ(๐ก).
Find the response y(t) due to step input with Amplitude of
Find the Transfer Function for the above state space model
Derive back the state space model from (3).
a = [0,1;0,4];
b = [0;1];
c = [0 -5];
x0=[2;0];
sys = ss(a,b,c,2);
initial(sys,x0); % to get 1
[n,d]=ss2tf(a,b,c,0);
mySys_tf=tf(n,d) % to get 3
[num den] = tfdata(mySys_tf, 'v')
tf2ss(num,den) % to get 4
I have written this code but it seems like its not giving me any results in the response graph and thus i can't also solve 2 and it get error in 4 if you can help me out to check what is wrong
I believe that the error comes from the fact that the system is unstable. If you were to plot the system's reaction to a step input using step() then you will see how it goes to infinity. I also don't know how far you are into your controls course and if you've seen the root locus yet, but you can plot the root locus of the system via rlocus(sys) and you'll see that the real portion of the root is on the right half of the plane and therefore letting you know that the system is unstable.
The response is 0 and will stay zero as x(2) = 0. It requires an input u to get x(2) off zero. So the graph is totally fine.
use step(sys) and you will see the drop to -Inf. Optionally you can define the end-time. Call step(sys,1) to see a reasonable range.
You solved 3 & 4 yourself.
To check stability you simply need to ask MATLAB isstable(sys) (isn't it continent? Well, there is a danger that people will forget the theory behind it and how it is connected...)
To check observability: rank(obsv(sys)) and make sure that it is the same as the system matrix
assert(rank(obsv(sys)) == length(sys.A), 'System is not observable!')
I'm studying for my digital logic test tomorrow, and I'm doing some practice problems. I'm having problems with this one specifically. I think I solved it, but I'm not confident in my answer. Can anyone verify the logic and explain it? I have looked elsewhere but can't find the answer previously recorded anywhere.
wy'+wx'y+wxyz+wxz'
1. I factored wy out of wx'y and wxyz to get wy(x'+xz) which equates to wy(x'+z). Using the rule A+A'B = A+B
This changes the equation to wy'+wyx'+wyz+wxz'
2. I factored w out of wy' and wyx' to get w(y'+yx') which equates to w(y'+x'). Using the rule A+ A'B = A+B.
This changes the equation to wy'+wx'+wyz+wxz'
3. I factored w out of wy' and wyz to get w(y'+yz) which equates to w(y'+z'). Using the same rule again.
This changes the equation to wy'+wz'+wyx'+wxz'
4. I factored w out of wz' and wxz' to get w(z'+z'x) which equates to w(z') using the rule A+AB=A.
This changes the equation to wz'+wy'+wyx'
5. I factored w out of wy' and wyx' to get w(y'+yx') which equates to w(y'+x') using the rule A+A'B = A+B.
This changes the equation to wy'+wx'+wz' - This is where I am stuck, I feel like there are more steps to do, but I can't find anything.
You have a mistake in Step 3
you start with
wy'+wx'+wyz+wxz' (equivalent to wy'+wyz+wx'+wxz')
you then correctly factor w out of wy' and wyz to get w(y'+yz)
but then you suggest w(y'+yz) is equal to w(y'+z') (which is incorrect)
w(y'+yz) is equal to w(y'+z)
which leaves you with wy'+wz+wyx'+wxz' (instead of wy'+wz'+wyx'+wxz')
after which the remaining logic after Step 3 will need to be adjusted
I have a Matlab project in which I need to make a GUI that receives two mathematical functions from the user. I then need to find their intersection point, and then plot the two functions.
So, I have several questions:
Do you know of any algorithm I can use to find the intersection point? Of course I prefer one to which I can already find a Matlab code for in the internet. Also, I prefer it wouldn't be the Newton-Raphson method.
I should point out I'm not allowed to use built in Matlab functions.
I'm having trouble plotting the functions. What I basically did is this:
fun_f = get(handles.Function_f,'string');
fun_g = get(handles.Function_g,'string');
cla % To clear axes when plotting new functions
ezplot(fun_f);
hold on
ezplot(fun_g);
axis ([-20 20 -10 10]);
The problem is that sometimes, the axes limits do not allow me to see the other function. This will happen, if, for example, I will have one function as log10(x) and the other as y=1, the y=1 will not be shown.
I have already tried using all the axis commands but to no avail. If I set the limits myself, the functions only exist in certain limits. I have no idea why.
3 . How do I display numbers in a static text? Or better yet, string with numbers?
I want to display something like x0 = [root1]; x1 = [root2]. The only solution I found was turning the roots I found into strings but I prefer not to.
As for the equation solver, this is the code I have so far. I know it is very amateurish but it seemed like the most "intuitive" way. Also keep in mind it is very very not finished (for example, it will show me only two solutions, I'm not so sure how to display multiple roots in one static text as they are strings, hence question #3).
function [Sol] = SolveEquation(handles)
fun_f = get(handles.Function_f,'string');
fun_g = get(handles.Function_g,'string');
f = inline(fun_f);
g = inline(fun_g);
i = 1;
Sol = 0;
for x = -10:0.1:10;
if (g(x) - f(x)) >= 0 && (g(x) - f(x)) < 0.01
Sol(i) = x;
i = i + 1;
end
end
solution1 = num2str(Sol(1));
solution2 = num2str(Sol(2));
set(handles.roots1,'string',solution1);
set(handles.roots2,'string',solution2);
The if condition is because the subtraction will never give me an absolute zero, and this seems to somewhat solve it, though it's really not perfect, sometimes it will give me more than two very similar solutions (e.g 1.9 and 2).
The range of x is arbitrary, chosen by me.
I know this is a long question, so I really appreciate your patience.
Thank you very much in advance!
Question 1
I think this is a more robust method for finding the roots given data at discrete points. Looking for when the difference between the functions changes sign, which corresponds to them crossing over.
S=sign(g(x)-f(x));
h=find(diff(S)~=0)
Sol=x(h);
If you can evaluate the function wherever you want there are more methods you can use, but it depends on the size of the domain and the accuracy you want as to what is best. For example, if you don't need a great deal of accurac, your f and g functions are simple to calculate, and you can't or don't want to use derivatives, you can get a more accurate root using the same idea as the first code snippet, but do it iteratively:
G=inline('sin(x)');
F=inline('1');
g=vectorize(G);
f=vectorize(F);
tol=1e-9;
tic()
x = -2*pi:.001:pi;
S=sign(g(x)-f(x));
h=find(diff(S)~=0); % Find where two lines cross over
Sol=zeros(size(h));
Err=zeros(size(h));
if ~isempty(h) % There are some cross-over points
for i=1:length(h) % For each point, improve the approximation
xN=x(h(i):h(i)+1);
err=1;
while(abs(err)>tol) % Iteratively improve aproximation
S=sign(g(xN)-f(xN));
hF=find(diff(S)~=0);
xN=xN(hF:hF+1);
[~,I]=min(abs(f(xN)-g(xN)));
xG=xN(I);
err=f(xG)-g(xG);
xN=linspace(xN(1),xN(2),15);
end
Sol(i)=xG;
Err(i)=f(xG)-g(xG);
end
else % No crossover points - lines could meet at tangents
[h,I]=findpeaks(-abs(g(x)-f(x)));
Sol=x(I(abs(f(x(I))-g(x(I)))<1e-5));
Err=f(Sol)-g(Sol)
end
% We also have to check each endpoint
if abs(f(x(end))-g(x(end)))<tol && abs(Sol(end)-x(end))>1e-12
Sol=[Sol x(end)];
Err=[Err g(x(end))-f(x(end))];
end
if abs(f(x(1))-g(x(1)))<tol && abs(Sol(1)-x(1))>1e-12
Sol=[x(1) Sol];
Err=[g(x(1))-f(x(1)) Err];
end
toc()
Sol
Err
This will "zoom" in to the region around each suspected root, and iteratively improve the accuracy. You can tweak the parameters to see whether they give better behaviour (the tolerance tol, the 15, number of new points to generate, could be higher probably).
Question 2
You would probably be best off avoiding ezplot, and using plot, which gives you greater control. You can vectorise inline functions so that you can evaluate them like anonymous functions, as I did in the previous code snippet, using
f=inline('x^2')
F=vectorize(f)
F(1:5)
and this should make plotting much easier:
plot(x,f(x),'b',Sol,f(Sol),'ro',x,g(x),'k',Sol,G(Sol),'ro')
Question 3
I'm not sure why you don't want to display your roots as strings, what's wrong with this:
text(xPos,yPos,['x0=' num2str(Sol(1))]);
i have a problem in motion control in matlab
imagine a four bar linkage mechanism like this.as you you know in an ordinary 4 bar linkage we have 2 fix points but here we have just one & the second one it fixed to a pinion (small gear).we have the ratio of gears so we have a relation between teta1 & teta2
teta2 = 5*teta1 (the mechanism can rotate in the first fix point)
i used to write this code for motion control but the when i run it the graphs are not correct (because they should be something linke sin or cos graph)
d(n) is a auxiliry vector for solving equations
please ask if you have further questions
this is the code :
clc,
close all,
clear all,
ax=0;
ay=0;
r1=12;
r2=7;
r3=9;
r4=5;
n=0;
for teta1=0:pi/180:2*pi
n=n+1;
D = r1*exp(i*teta1)-r2*exp(i*5*teta1);
tetad(n) = angle(D);
d(n) = abs(D);
landa(n)=acos((d(n)^2+(r3)^2-(r4)^2)/(2*d(n)*r3));
alfa(n)=acos((d(n)^2+(r4)^2-(r3)^2)/(2*d(n)*r4));
teta3(n)=landa(n)+tetad(n);
teta4(n)=(+pi-alfa(n)+tetad(n));
end
aa(n)=teta1*180/pi;
hh(n)=tetad(n)*180/pi;
bb(n)=landa(n)*180/pi;
cc(n)=alfa(n)*180/pi;
nn(n)=teta3(n)*180/pi;
dd(n)=5*teta1*180/pi;
ee(n)=teta4(n)*180/pi;
figure(1),plot(aa,hh),xlabel('teta1'),ylabel('tetad');
figure(2),plot(aa,d),xlabel('teta1'),ylabel('d');
figure(3),plot(aa,bb),xlabel('teta1'),ylabel('landa');
figure(4),plot(aa,cc),xlabel('teta1'),ylabel('alfa');
figure(5),plot(aa,nn),xlabel('teta1'),ylabel('teta3');
figure(6),plot(aa,dd),xlabel('teta1'),ylabel('5*teta1');
figure(7),plot(aa,ee),xlabel('teta1'),ylabel('teta4');
I have no idea what you are trying to solve here, but probably you want to move the end of your for-loop a few lines down, so the vectors you plot contain all values and not only the last one.