Access object numbers in examples - alloy

If I have just one sig A and want to chain multiple instances of it (for example by a successor relation), Alloy will number them (A1, A2, A3 ,...) randomly.
Is there a way to tell it, that I want these in ascending order ?
Or that A3 has to come after A1, but A2 before A4 or similar constraints.

If your goal is to impose a total order on A then I would suggest using the util/ordering library:
open util/ordering[A]
If you use this library then the Analyzer will do its best to keep the A atoms in ascending order (according to the next relation that is declared in the library, meaning that A$1.next will be A$2 and so on). Also, analysis will be more efficient due to improved symmetry breaking. However, you need to be aware that signature A will become fully saturated, in the sense that a scope of 5 A will be the same as exactly 5 A.

There is no way of connecting names of sig instances to whatever relation you defined in your model (be it with your "chaining relation" or util/ordering library), in the general case. (Essentially, this is up to Alloy's solver which might instantiate fresh names unpredictably.)
One alternative, that could perhaps work in your case, might be to declare multiple sigs, for example:
one sig A1, A2, A3, ... extends A {}
with a "chaining relation":
succ = A1 -> A2 + A2 -> A3 + ...
Now, since the ordering is fixed explicitly on signatures with names, that are ordered, the models found by Alloy will indeed satisfy your desired property.

Related

Excel: Can you use IFS for multiple criteria on median

Excel does not have a function to sort for multiple criteria for MEDIAN() as it does with AVERAGEIFS() or SUMIFS().
I have been sorting multiple criteria for median with nested IF() statements. For example, suppose I have a rent roll and I'm looking to calculate the median rent of a particular unit type that's occupied, I would do:
=MEDIAN(IF(unitType="1x1",IF(leaseStatus="Occd",rent)))
Where unitType, leaseStatus, and rent are arrays of data.
Is it possible to rewrite this function using IFS()? I have not been successful. I tried for example:
MEDIAN(IFS(unitType="1x1",leaseStatus="Occd",rent))
with no success.
Thanks in advance.
This post does not discuss how:
Optimizing multiple-criteria IFs
Here /screenshot(s) refer.
RE your Q:
"Is it possible to rewrite this function using IFS()? "
(1) (v.) SHORT ANS..
YES
(if one must... cf. cell G8):
=MEDIAN(IFS((Table1[Letter]="A")*(Table1[Colour]="blue"),Table1[value],1,""))
High level conclusions
See below for further info RE: M1-4 (corres. Method 1-4 resp.)
M1. Nested if (M1, given) is natural starting pt. yet rendered unnecessary by latest software version (i.e. Office 365 compatible Excel); thus room for improvement/refinement remains
M2. Prosaic 'single-if' method appears to be an improvement over M1 given elimination of nested if method (however, this depends upon one's defn. of optimality to begin with).
M3. Parsimonious: filter - per below: recommended method provided Office 365 Excel version is avail. (failing which, M2)
M4. Ifs (M4) possible yet inappropriate/superfluous given nature of
filter in Q
---------------------------------------------------------------------
(2) VARIANTS & REL. MERITS
IFs statement akin to If/else if/else if etc. compound statement; you're looking for a compound If statement as opposed to a series of sequential filters that suffer from the unnecessary ordered dependency for the filtration in question.
Here is a comparison table highlighting relative merits/otherwise for 4 different methods/calc. bases (incl. the variant given in the body of the Q [M1] and the IFs variant / equivalent [M4]:
functions corres. to respective cells/comparison table (G5-G7):
M1: cell G5
(see Q for gen. basis of formula/compound-nested If function)
M2: cell G6
M3: cell G7
M4: cell G8
Relies upon double ifs per Q.
fn: see v. first function 'v. short A' above
---------------------------------------------------------------------
(3) COMPARISON OF M1-4
See table in first screenshot provided above

Minizinc: partial search strategy with sharing indexes

Given 3 vectors variables of the same length (let's say A, B, and C), is there a way in Minizinc to apply a search strategy on one of them(let's say B), and at each variable of B instantiated, the search goes to the variables in the same position in A and C?
E.g: A =[a1,a2,a3], B=[b1,b2,b3], C=[c1,c2,c3]
I want a search like this:
search on B with some criteria (e.g: first fail, to be sure that we're not going in order)
find the next variable to assign (b2)
assign values to a2 and c2 (same position/same index of b2)
continue the search on B etc...
Complex search heuristics are generally not supported in MiniZinc in general. However, your description of the search might be close to priority search: a search mechanism published for the Chuffed solver.
You can find a description of priority search in the following workshop paper: https://ozgurakgun.github.io/ModRef2017/files/ModRef2017_PrioritySearchWithMiniZinc.pdf

Defining Data Structures/ Types In Haskell

How would it possible to define a data structure in Haskell, such that there are certain constraints/rules that apply to the elements of the structure, AND be able to reflect this in the type.
For example, if I have a type made up of a list of another type, say
r = [x | x <- input, rule1, rule2, rule3].
In this case, the type of r is a list of elements of (type of x). But by saying this, we loose the rules. So how would it be possible to retain this extra information in the type definition.
To give more concreteness to my question, take the sudoko case. The grid of sudoko is a list of rows, which in turn is a list of cells. But as we all know, there are constraints on the values, frequency. But when one expresses the types, these constraints don't show up in the definition of the type of the grid and row.
Or is this not possible?
thanks.
In the example of a sodoku, create a data type that has multiple constructors, each representing a 'rule' or semantic property.
I.E.
data SodokuType = NotValidatedRow | InvalidRow | ValidRow
Now in some validation function you would return an InvalidRow where you detect a validation of the sodoku rules, and a ValidRow where you detect a successful row (or column or square etc). This allows you to pattern match as well.
The problem you're having is that you're not using types, you're using values. You're defining a list of values, while the list does not say anything about the values it contains.
Note that the example I used is probably not very useful as it does not contain any information about the rows position or anything like that, but you can define it yourself as you'd like.

Algorithm to iteratively discover a boolean rule defining a vector condition

I am interested in finding an algorithm to iteratively discover an boolean rule which defines a condition of a vector. For example, let's say the vector is all the letters in a book and the rule purports to tell you whether the book was written by James Joyce, but we don't know what the rule is, we want to discover it. The rule engine will always answer True or False for any submitted vector. So, for example, if we submit a vector array containing all the characters in "Ulysses", then the engine might respond "True", if the rule is a good one.
So, imagine we split "Ulysses" into two halves, A and B, and submit each half to the engine separately. The engine answers True to A, but False to B. From this we can infer that whatever the rule is looking for is only found in A. So, now we divide A into two halves again, A1 and A2. The engine this time says False for both. Now, we can infer there must be an AND condition in the rule, and one of the atoms of the AND condition must be in A1 and the other in A2. For example, the rule might be: "if the words 'meatjuice' and 'carracarracarra' are in the vector return True. This is consistent with our test results because "meatjuice" is in the first quarter of the book, and "carracarracarra" is in the second quarter.
By successively dividing our text and re-submitting to the engine we can eventually discover the hidden rule the engine is using.
I suspect that there already exists an algorithm to do this, but I do not know what it is called or how to go about finding it.
I would describe this problem as learning monotone DNF formulas using membership queries. Angluin (Queries and Concept Learning, 1988) gave an algorithm similar to yours, but it uses equivalence queries (i.e., find an example on which the current hypothesis doesn't work) as well as membership queries. The problem otherwise is that finding that last term may take quite a while. For example, suppose that the rule is
(A1 && B1) || (A2 && B1) || ... || (An && B1)
|| (A1 && B2) || (A2 && B2) || ... || (An && B2)
|| (B1 && B2).
The hypothesis consisting of the first two lines only differs from the rule with respect to exactly one out of 2^(n + 2) inputs.

Excel Named Function Parameters or UDF without Macros

It seems like a bit of an omission that there's no easy way to create a user-defined declarative function in Excel without defining a macro. I can't use XSLM with the uphill battle that will entail in the Enterprise, but I want to be able to define a function with intent thus.
I want to do this;
=BreakEven(C1:C20)
But I can't use a macro, although I can use a "named formula". The trouble is how to pass parameters to that? I've seen a couple of tricks (kludgy workarounds) but not for xslx.
I'd like to be able to define a Breakeven() function in another tab and reference it here passing in MORE THAN one parameter, two ranges in fact. I'm sure there's some way using string parsing but I can't see it.
I don't mind if the function doesn't look exactly like that, as long as it evaluates within the cell and I can parse it for 'intent'. For instance, this example (http://www.jkp-ads.com/articles/ExcelNames09.asp) which I was unable to get to work in xlsx uses this syntax;
=IF(ROW(D3),CellColor)
Where "cellcolor" is the name of the function and D3 is the range parameter. The other solution I'm toying with is to define a function in column format with a variable argument list (this is two rows of an excel spreadsheet);
[Value][function][parameter1][parameter2][parameter3]
24050 BreakEven C1:C20 A1:A20
It's not pretty, but the benefit of the latter is that it describes the function to an external reader. We know it's a breakeven function, whereas if we put the actual formula "OFFSET,INDIRECT,SUM()()()()etc" it would not be readable/parseable. Of course, in that case, I'd have to construct the value field by parsing the cells to the right in Excel, which would make the Value formula messy but at least it would be a self-describing row.
Can anyone suggest a better method?
Poor-man's UDF
So I think what we're going to have to do is this;
A B C D E
1 [Value][function][parameter1][parameter2][parameter3]
2 24050 BreakEven C1:C20 A1:A20
3 111 mySum 1 10 100
Where "BreakEven" is a "named function". Here's the formula for "mySum";
=sum(C1:E1)
To evaluate functions listed in B, we just put this in column A (transposing the same value for all rows in column A;
=value(B)
This works because A2 and A3 both evaluate column B as a value, which causes BreakEven and Sum to run (as poor-man's UDFs) in the context of A2 and A3. The range (C1:E1) is relative of course.
So in effect, we can write any function name in column B (as long as there's a corresponding named function defined in the workbook which can be as complex as you like). Columns C, D and E act as the parameters for the function on the same row.
I would have loved to just be able to write the following in column A instead;
=mySum(1,10,100)
But in the absence of that support, the mechanism above serves to provide a readable parameterised function that would be understandable by a user, that's also machine readable (works in CSV too) and allows us to offload our re-usable functions to a library sheet somewhere in the workbook for maintenance.
Not perfect, but an acceptable compromise, unless anyone has a clever way of doing this in a single cell?
Not really an answer, but easier to illustrate here than in a comment. Although you can't rename formulas in a simplistic way - I like your suggestion actually I've never thought about that before; but then I've never worked in a non-macro environment so this has never occurred- you can add notes into the actual formula explaining what it does. For example:
=N("This is a really complex BreakEven Formula")+SUM(3,4,5)
Is a perfectly valid formula. As I said, not really an answer, but could potentially add clarity to a complex formula
You can do this with a small trick
For example to create effectively a cuberoot UDF that emulates =cuberoot(x) then name a variable as cuberoot with a 'value' like this.
=(RC[-1])^(1/3)
Now you can either do this using a temporary switch to RC mode, or put the cursor in say cell E5 and type the name value as =(D5)^(1/3)
Now whenever you need a cuberoot you can put the argument in any cell and put =cuberoot in the cell to its right. It really works and follows true Excel rules.
I use it for multiparameter models that have the single 'argument' Time as a dependent variable. I then define the term Model as the model equation eg =a+bTime+cTime^2
where a,b,c are already named locations holding unique parameter values -
and then define Time as =RC[-1]
My sheets are filled with cells simply saying =Model and have the required time value to the left (ie their argument). It is simple to extend to multi arg functions using multiple cells. It usually fits in well with spreadsheet layouts. Change the definition of your model once in the define name box and all places change simultaneously.
I have a function called ToDMS which takes the decimal degree value in the preceding cell and converts it to a deg Min and Sec string - very tidy.
You need the degrees to be in a single cell but want it in the alt. form in another cell
elegant, simple and it works
Bob Jordan

Resources