Answer Set Programming Beginner - clingo

I am a beginner in Answer Set Programming and completely new to clingo. I tried the facts and constraints below on nurses shift on clingo, but I am not getting any model and clingo not flagging any error. I keep getting UNSAT. What am I not doing right?
nurselimits(x, min, max).
worklimits(min, max).
daylimits(x, t, min, max).
x(morning; afternoon; night; off; leave).
x(1..5).
day(1..28).
days(28).
nurse(1..40).
shift(1, morning, 8).
shift(2, afternoon, 14).
shift(3, night, 20).
shift(4, off, 0).
shift(5, leave, 10).
nurselimits(1,6,9).
nurselimits(2,6,9).
nurselimits(3,4,7).
daylimits(1,8,6,9).
daylimits(2,8,6,9).
daylimits(3,8,5,10).
worklimits(132,228).
{assign(N, X, D) : shift(X, Name, H), X != 4, X != 5} = 1:- nurse(N), day(D).
:- day(D), #count{N : assign(N, X, D)} > max, nurselimits(x, min, max).
:- day(D), #count{N : assign(N, X, D)} < min, nurselimits(x, min, max).
:- nurse(N), #sum{H, D : assign(N, X, D), shift(X, Name, H)} > max, worklimits(min, max).
:- nurse(N), #sum{H, D : assign(N, X, D), shift(X, Name, H)} < min, worklimits(min, max).
:- nurse(N), assign(N, X1, D), assign(N, X2, D+1), X2 < X1 , X1 <= 3.
:- nurse(N), day(D), days(DAYS), D <= DAYS-21,
#count{D1 : assign(N, 4, D1), D1 >= D, D1 <= D+21} = 1.
:- nurse(N), #count{D: assign(N, X, D)} > max, daylimits(x, t, min, max).
:- nurse(N), #count{D: assign(N, X, D)} < min, daylimits(x, t, min, max).

In this code
nurselimits(x, min, max).
worklimits(min, max).
daylimits(x, t, min, max).
x(morning; afternoon; night; off; leave).
x(1..5).
There are several problems. The symbols x, min, max, t. Are all non-numerical symbolic constants, much like morning, afternoon,etc. That is probably unintended, unless you are substituting them using the -c command line option. Certainly, the x in nurselimits has nothing to do with the x(1..5) in your code, which also seems unintended. Also, the constant min is the same symbolic value in nurselimits, worklimits and daylimits. Perhaps you wanted it to be variable instead?

You should remove first three lines and add #show (e.g. #show assign/3.) of any predicate you want to see in the answer sets.

Related

Calculated Field to get weighted sum

I m having trouble making calculated field to get Weighted window Sum.
It has to be in 12 months window size (for example, On 2023-02, it should be the sum of 2022-03 ~ 2023-02).
And, each value of the month is multiplied by index (like 1, 2, 3 ... 12)
So, the formula should look like,
For i = 1 ~ 12, Σ i * Xi (Xi is the value of the month)
I tried window_sum and running_sum ... but it didn't make the result i want...
WINDOW_SUM(SUM([Count]*INDEX(), -11, 0)
Thanks in advance!

T-SQL - Padding data points

I have a series of data points in a table in an Azure SQL database. When plotted, the points have the following distribution:
What I am trying to do is add padding so that they have the appearance of being in a continuous line - or at lease more continuous than it is now.
I tried adding points between each point, but the problem with that is that it's all relative. If you look closely, you can see some of the points are blue, and some are dark red. The blue points are the ones I added, but the line looks the same.
I'm looking for advice on the logic I should use to solve this issue. I want to add x number of points between each data point based on the distance between the nearest points... if that makes sense.
I think this works
declare #t table (x smallmoney primary key, y smallmoney);
declare #inc smallmoney = 1;
insert into #t(x, y) values
(1, 1)
, (5, 3)
, (8, 4)
, (10, 5)
, (11, 6);
with cte as
( select x, x as x0, y, y as y0, cnt = cast(1 as smallmoney)
, lead(x) over (order by x) as nextX
, lead(y) over (order by x) as nextY
from #t t
union all
select x + #inc, x0, y + #inc/(nextX-x0)*(nextY-y0), y0, cnt+1, nextX, nextY
from cte t
where x + #inc < nextX
)
select *
from cte t
order by t.x;
I'm not confident that this is the best solution, but I think you could build off of it. Here's an sqlfiddle
SELECT x + COALESCE(((nextx-x)/10)*inc, 0) as x, y + COALESCE(((nexty-y)/10)*inc, 0) as y
FROM
(SELECT x, y, nextx, nexty, inc.n + 0.0 as inc FROM
(SELECT x, y, lead(x) over (order by x) as nextx, lead(y) over (order by x) as nexty
FROM points) p inner join (VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) inc(n)
ON nextx is not null or inc.n = 0
) a ORDER BY x
This will add 9 points between each point (10 points total, including the "real" one).
The basic idea is that I'm using lead for each row to get the next x and next y, then I join that to a hardcoded list of values 0 to 9. Then for each value, I increment x by 1/10 of the difference between nextx and x, and increment y by 1/10 of the difference between nexty and y.
The join condition nextx is not null or inc.n = 0 is so that I only join inc(0) to the last x value (rather than joining 10 times).
You could change my hardcoded list of values and the hardcoded 10s to increment differently. Similarly, you'd probably need some changes if you only want integers, but the principle will be the same.

Calculating d value in RSA

I saw a couple questions about this but most of them were answered in unhelpful way or didn't get a proper answer at all. I have these variables:
p = 31
q = 23
e - public key exponent = 223
phi - (p-1)*(q-1) = 660
Now I need to calculate d variable (which I know is equal 367). The problem is that I don't know how. I found this equation on the internet but it doesn't work (or I can't use it):
e⋅d=1modϕ(n)
When I see that equation i think that it means this:
d=(1modϕ(n))/e
But apparently it doesn't because 367 (1modϕ(n))/e = 1%660/223 = 1/223 != 367
Maybe I don't understand and I did something wrong - that's why I ask.
I did some more research and I found second equation:
d=1/e mod ϕ(n)
or
d=e^-1 mod ϕ(n)
But in the end it gives the same result:
1/e mod ϕ(n) = 1/223 % 660 = 1/223 != 367
Then I saw some guy saying that to solve that equation you need extended Euclidean algorithm If anyone knows how to use it to solve that problem then I'd be very thankful if you help me.
If you want to calculate something like a / b mod p, you can't just divide it and take division remainder from it. Instead, you have to find such b-1 that b-1 = 1/b mod p (b-1 is a modular multiplicative inverse of b mod p). If p is a prime, you can use Fermat's little theorem. It states that for any prime p, ap = a mod p <=> a(p - 2) = 1/a mod p. So, instead of a / b mod p, you have to compute something like a * b(p - 2) mod p. b(p - 2) can be computed in O(log(p))
using exponentiation by squaring. If p is not a prime, modular multiplicative inverse exists if and only if GCD(b, p) = 1. Here, we can use extended euclidean algorithm to solve equation bx + py = 1 in logarithmic time. When we have bx + py = 1, we can take it mod p and we have bx = 1 mod p <=> x = 1/b mod p, so x is our b-1. If GCD(b, p) ≠ 1, b-1 mod p doesn't exist.
Using either Fermat's theorem or the euclidean algorithm gives us same result in same time complexity, but the euclidean algorithm can also be used when we want to compute something modulo number that's not a prime (but it has to be coprime with numer want to divide by).

Function plot from points result of other function combinations

I have 2 functions declared in wxmaxima: f1(x, y) and f2(x, y). Both contain if-then-else statements and basic arithmetic operations: addition, subtraction, multiplication and division.
For example (just an example, real functions look much more complicated):
f1(x, y) := block([],
if x * y < 123 then x + y
else if x / y > 7 then x - y
);
In both functions x and y change from 0.1 to 500000.
I need a 3D plot (graph) of the following points:
(x, y, z), where f1(x, y) == f2(z, x)
Note that it's impossible to extract z out from the equation above (and get a new shiny function f3(x, y)), since f1 and f2 are too complex.
Is this something possible to achieve using any computational software?
Thanks in advance!
EDIT:
What I need is the plot for
F(x, y, z) = 0
where
F(x, y, z) = f1(x, y) - f2(z, x)
For Maxima, try implicit_plot(f1(x, y) = f2(x, y), [x, <x0>, <x1>], [y, <y0>, <y1>]) where <x0>, <x1>, <y0>, <y1> are some floating point numbers which are the range of the plot. Note that load(implicit_plot) is needed since implicit_plot is not loaded by default.
As an aside, I see that your function f1 has the form if <condition1> then ... else if <condition2> then ... and that's all. That means if both <condition1> and <condition2> are false, then the function will return false, not a number. Either you must ensure that the conditions are exhaustive, or put else ... at the end of the if so that it will return a number no matter what the input.
set = Table[{i,j,0},{i,1,10},{j,0,10}];
Gives a list of desired x and y values and use those with a replace all /.
set = set /.{a_ ,b_ ,c_} -> {a,b, f1[a,b] - f2[a,b]} (*Simplified of course*)
Set is a 2d list of lists so it needs to be flattened by 1 dimension.
set = Flatten[set,1];
ListPlot3D[set (*add plot options*)]

Integration with parameters with maple

I would like to compute (formally) some integrals which are just rational fraction and which depends on 3 parameter. It works if I set two parameter to trivial value, else i must stop the computation after 5 min. Does anyone can help me to make it works?
Here is my worksheet:
restart;
omega(x,y):= 1/(1+x^(2)+y^(2))*<2*x,2*y,x^(2)+y^(2)-1>:
Omega(x,y, a,b, l):= simplify(omega(evalc(Re((l*(x+I*y)+a+Ib)/(1-(a-I*b)*(x+I*y))) ),evalc(`&Im;`((l*(x+I*y)+a+I*b)/(1-(a-I*b)*(x+I*y)))) )):
assume(0 < l);
simplify(int(int(Omega(x, y, a, b, l)[1]*(diff(Omega(x, y, a, b, l)[1], x)), x = -infinity .. infinity), y = -infinity .. infinity));
Warning, computation interrupted
simplify(int(int(Omega(x, y, 0, 1, l)[3]*(diff(Omega(x, y, 0, 1, l)[1], x)), x = -infinity .. infinity), y = -infinity .. infinity));
Warning, computation interrupted
simplify(int(int(Omega(x, y, 0, 0, l)[3]*(diff(Omega(x, y, 0, 0, l)[1], x)), x = -infinity .. infinity), y = -infinity .. infinity));
2 Pi
- ----
l
You have Ib where you may intend I*b.
You have &Im where you may intend Im.
You place an assumption on l but place no helpful assumptions on a and b. Are both a and b to be taken as being purely real? I have used such assumptions below.
Below, I utilize assuming rather than assume, as I find it more convenient and it doesn't tend to lead to a muddle with mixes of distinct instances of both unassumed names and assumed names which ostensibly appear equal.
The syntax you used for assigning omega and Omega can produce operators (the apparent intention) when used for 2D Math input, albeit via a diambuguation popup. But here we have plaintext source, and such syntax used as 1D Maple Notation code makes remember table assignments instead of operators. Below I use a syntax valid in both 1D and 2D modes for assigning operators to those two names.
The following results each took anywhere from a few seconds to about a minute on 64bit Linux running Maple 17 on an Intel i5.
restart;
omega:=(x,y)->1/(1+x^(2)+y^(2))*<2*x,2*y,x^(2)+y^(2)-1>:
Omega:=(x,y,a,b,l)->simplify(omega(evalc(Re((l*(x+I*y)+a+I*b)/
(1-(a-I*b)*(x+I*y)))),
evalc(Im((l*(x+I*y)+a+I*b)/
(1-(a-I*b)*(x+I*y)))))):
T31:=simplify(Int(Int(Omega(x,y,a,b,l)[3]
*(diff(Omega(x,y,a,b,l)[1],x)),
x=-infinity..infinity),y=-infinity..infinity),
size) assuming real, l>0:
simplify(value(subs(b=0,T31))) assuming real, l>0;
2
2 Pi (a + l)
- -------------
2 2
a + l
simplify(value(T31)) assuming real, l>0;
4 2 2 4 2 2 2 2 3
2 (a + a l - b + b l + a l - b l + l ) Pi
- -------------------------------------------------
4 2 2 2 2 4 2 2 4
a + 2 a b + 2 a l + b + 2 b l + l
T11:=simplify(Int(Int(Omega(x,y,a,b,l)[1]
*(diff(Omega(x,y,a,b,l)[1],x)),
x=-infinity..infinity),y=-infinity..infinity),
size) assuming real, l>0:
simplify(value(subs(b=0,T11))) assuming real, l>0;
0
simplify(value(T11)) assuming real, l>0;
0