Find lowest element of predicates of the size 2 and ensure that there is only one lowest element? - answer-set-programming

I am working on a piece of encoding that finds the cell with the lowest coordinates in a grid. This cell will be used as a starting point for a path. Cells are defined with predicate cell(X,Y).
isLower(X,Y,F,G) :- cell(X,Y), cell(F,G), X<=F, Y<=G.
first(X,Y) :- cell(X,Y), not black(X,Y), cell(F,G), not black(F,G), cell(A,B), not black(A,B), isLower(X,Y,F,G), not isLower(A,B,X,Y).
I want to have only one first cell. My problem is that ASP starts giving me a bunch of cells, because first(X,Y) is a predicate and ASP will try to produce as many as it can.
How do I tell the program that it is supposed to find only the lowest element (i.e. the cell with the smallest coordinates)? How can I force it to be only ONE cell?

It is still unclear what exactly you want to achieve, but in case that cell/2 are facts, i would recommend a rule like:
first(X',Y') :- (X',Y') = #min {(X,Y) : cell(X,Y)}.

Related

LibreOffice Calc: How to use a variable in a function/formula?

Before
After
I'm trying to dynamically fill a range of cells in a column based on a number in another cell.
So, fill x amount of cells with a value set in, say, E4. x is the value in cell E5.
If cell E4 = 25, fill the cells in the desired column below a certain point with "25" for x amount of rows. If E5 = 5, and the start point is A3, it would fill A3 through A8 with "25" or whatever desired string or formula.
For me, it's ="Layer " & ROW() - 2 and I want to fill every cell in that column to a range determined in E5. The amount of cells in that row would change dynamically with whatever is set in E5
Problem is, I can't find a way to set a variable and retrieve it's value in a single cell's forumula, and I can't use nested formulas either (SUM(Var1:E1) where Var1 is the string E2 or something like that)
In this case, starting at cell A3 = Layer 1, it would named each cell Layer 2, and then Layer 3, until the layer number matches the value in E5.
I also need to be able to count how many layers there are for a forumula in the next column that depends on the total number of layers (plus 1)
I don't really know a ton about Calc or Excel formulas, but they don't follow a convention that makes sense to me.
I would guess a nested sum function would work like: =SUM(SUM(A1:A5):A6) or =SUM((SUM(A1:A5)):A6) but it doesn't. And it doesn't seem like there is a way to set variables inside one cell's formula.
I also tried creating a string that would create the relevant cell reference and thus the right value, like =("E"&E1) which would create E5 if E1 was set to 5, but I can't use this value as a cell reference
To get the result shown in the images, enter =IF(ROW(D1)<=E$1;"Layer " & ROW(D1);"") in cell D3 and fill down to D10 by dragging the dark dot in the corner. Then change the value in E1 to see the difference.
The $ means do not change the following 1 in the reference when the formula is copied or filled elsewhere. Otherwise the formula references will be adjusted relative to the new location.
If I use =("E"&E1) to create "E5" based on the value in E1, I want to use the E5 as a cell reference and not a string.
That is what INDIRECT does.
=SUM(SUM(A1:A5):A6)
It should be =SUM(SUM(A1:A5);A6) instead. The first one is a range of cells, whereas the second gives an additional cell to include. You could also do for example =SUM(SUM(A1:A5);A6:A10).
I don't really understand why you would expect a : to work where the ; is. That would define a range that starts at the row of the result of the SUM and ends at a cell I suppose? Doesn't make sense to me. Maybe you intended something like =SUM(INDIRECT("A"&SUM(A1:A5));A6). Still I don't get why anyone would want to do that.
Spreadsheet formulas are different from programming languages. It may take some time to learn how to use them effectively. LibreOffice also has extensive support for macros if you need an actual programming language, although in most cases there is probably a better way to solve the problem without resorting to macros.

LibreOffice Calc -- countif based on string length

I'd like to setup a countif function that counts the cells in a range based on the cell string length.
I've tried inserting the formula as an argument to the countif function, but I can't get it to work.
I wanted to compare the length for each cell in the range with a specific lenght defined somewhere else, but I don't know how to reference back the cell in the range.
One of my failed attempts was =COUNTIF($'Delovni list1'.$D$3:$D$102;">2")
given that the cells in the range are formatted as text. But the above doesn't even work if there are numbers in the cells.
My use case is this: I need to count the cells with 1 specific letter and cells which have many characters.
Thank you.
seba
I found this to work:
=COUNTIF($'Delovni list1'.$D$3:$D$102;"[:alnum:]..*")
regards,
seba

Using Matlab to find output at specified input values

spacing_Pin = transpose(-27:0.0001:2);
thetah_2nd = Phi_intrp3(ismembertol(spacing_Pin,P_in2nd));
With this code, I want to evaluate Phi_intrp3at indices where spacing_Pinis equal to P_in2nd
I know I have asked similar questions before. And I have got some really helpful answers already. But in this case they do not seem to apply. P_in2ndhas only 40 entries, whereas spacing_Pinhas far more. Therefore I cannot consider the absolute value of the difference of spacing_Pinand P_in2ndto find out where they are closest to equal.
so P_in2ndhas values between -25.9747 and -0.0147. The decimals have 4 digits after the dot, but these are sometimes rounded by Matlab (format short). That's the catch, I think, P_in2nd is not found in spacing_Pin. The result is an empty matrix.
Here's the first 5 entries of P_in2nd:
-25,9747431735299
-24,9747431735299
-23,9947431735299
-23,0047431735299
-22,0047431735299
Now, I want to evaluate ¸Phi_intrp3at these values. For this purpose I can change spacing_Pin, but not P_in2nd. For example, when I search for the first entry of P_in2ndin spacing_Pin, I find that entry 10254 = -25,9747000000000. So I want to evaluate Phi_intrp3at this input entry.
Is there a way of doing this?

How to merge two lists(or arrays) while keeping the same relative order?

For example,
A=[a,b,c,d]
B=[1,2,3,4]
my question is: how to generate all possible ways to merge A and B, such that in the new list we can have a appears before b, b appears before c,etc., and 1 appears before 2, 2 appears before 3,etc.?
I can think of one implementation:
We choose 4 slots from 8,then for each possible selection, there are 2 possible ways--A first or B first.
I wonder is there a better way to do this?
EDIT:
I've just learned a more intuitive way--use recursion.
For each spot, there are two possible cases, either taken from A or taken from B; keep recursing until A or B is empty, and concatenate the remaining.
If the relative order is different than what constitutes a sorted list (I assume it is, because otherwise it would not be a problem), then you need to formalize the initial order. Multiple ways to do that. the easiest being remembering the index of each element in each list. Example: valid position for a is 1 in the first array [...]
Then you could just go ahead and join the lists, then generate all the permutations of elements. Any valid permutation is one that keeps the order relationship of the new indexes with the order you have stored
Example of one valid permutation array
a12b3cd4
You can know and check that this is valid permutation because the index of element 'a' is smaller than the index of b, and so on. and you know the indexes must be smaller because this is what you have formulated at the first step
Similarly an invalid permutation array is
ba314cd2
same way of checking

Fill cell array with a certain value in matlab

First question: say I have a 3x3 cell array, lets call it A. So, if I want to fill A{1:2, 1:2} with the same cell array, how do I do it. MatLab requires both sides of the '=' to have the same number of elements. How do I assign the same value (a 2x1 cell) to A{1:2, 1:2}, in a single instruction?
Second question: I want to create a probability generator (not sure if it's the correct term) that will pick between a certain amount of option, based on a prior probability. For example, say that I want to randomly pick between A, B, and C, based on the following probabilities:
P(A) = .4
P(B) = .5
P(C) = .1
How do I accomplish this?
For your first question, repmat should work well.
For an example, see http://www.mathworks.com/matlabcentral/answers/8977
For your second question, combine <, cumsum, and find. If you want a more detailed explanation, open a second question covering just the probability generation.