ANN: A pure Python implementation of Cassowary

147 views
Skip to first unread message

Russell Keith-Magee

unread,
May 3, 2014, 9:07:51 PM5/3/14
to overcon...@googlegroups.com
Hi all,

I wanted to let you know I've just released a port of the Cassowary algorithm in Python. 


I'm aware there are a couple of existing Python wrappers around the C++ implementation, but my version is a pure Python implementation. I've also adapted the API to exploit some useful language characteristics of Python. In particular:

* There's no need to explicitly create Expression, Equality or Inequality objects. I've overridden the mathematical and comparison operators so that mathematical operations involving a Variable compose Expressions, and comparisons of Expressions generate Constraints. As a result, syntax such as:

    solver.add_constraint(3 * left >= right + 10)

is possible.

* I've introduced the use of context managers in place of the begin_edit() and end_edit() calls. 

    with solver.edit():
        solver.suggest_value(left, 42)

This automatically does the begin_edit() and end_edit() calls, and provides a syntactical cue that an edit context is underway. 

If you've got any questions, or if you notice any bugs, let me know; contact details for the project are on the Read The Docs page.

Yours,
Dr Russell Keith-Magee %-)

Håvard Fossli

unread,
Oct 3, 2014, 7:17:30 AM10/3/14
to overcon...@googlegroups.com
I just wan to let you know that I find the documentation you've written to be of great help.

Russell Keith-Magee

unread,
Oct 4, 2014, 12:06:44 AM10/4/14
to overcon...@googlegroups.com
Hi Håvard,

You're most welcome - and if anyone has any suggestions for improvements, I'm open to them - my biggest issue getting into Cassowary was understanding how it worked, 

--
You received this message because you are subscribed to the Google Groups "overconstrained" group.
To unsubscribe from this group and stop receiving emails from it, send an email to overconstrain...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Russell Keith-Magee

unread,
Oct 4, 2014, 12:09:07 AM10/4/14
to overcon...@googlegroups.com

(dangit... hit send accidentally)

I was going to say - my biggest issue was understanding how it worked, so if I can contribute back to the pool of knowledge, I'm happy to do so. The repository, including the documentation, is all up on Github [1], so improvements can be submitted there, either as pull requests or as tickets.


Yours,
Russ Magee %-)

Håvard Fossli

unread,
Oct 6, 2014, 6:48:16 AM10/6/14
to overcon...@googlegroups.com

Excellent! 

I am not sure about the format – wether it should be a tutorial, an faq or just plain descriptive chunks. Anyway, here's some stuff I've been wanting to read about. 

  • What is the relation between strength on stay variables and strength on constraints? 
  • When is stay constants appropriate? When are they not? Should one mix stay and edit variables? 
  • How can I create external "read-only" variables? By setting a strength? Or is it by refraining from making it a stay variable? Much like constants there are some things that should never be updated by the solver. For instance the size of the screen on the device. The only time the size of the screen changes is when the user is rotating thus flipping the width and height. 
  • Relation between strengths. Is required special in some way? Can "required" have weight? 
  • How should one go about making infrastructure independent of its coordinate systems? By adding each variable in between the views to the constraint?

Some in-depth explanation about terminology

  • variables
  • stay variables 
  • edit variables
  • strength 
  • constraint 
  • inequality expression
  • simplex solver

I would love to write about this, but I don't have the required experience nor knowledge to do so.

Håvard Fossli

unread,
Oct 8, 2014, 5:12:54 AM10/8/14
to overcon...@googlegroups.com
Let me know if this is something you want to have a look at. We could arrange so that you would be paid by the hour for improving the documentation. 

Best regards,

Håvard

Håvard Fossli

unread,
Oct 27, 2014, 6:35:01 AM10/27/14
to overcon...@googlegroups.com
In Rhea I found this API



namespace rhea

{

// Forward declaration

class solver;

/** Base class for constraints. */

class abstract_constraint

{

public:


   
/** Mark a variables as read-only. */

    abstract_constraint
& mark_as_readonly(const variable& v)
   
{
        readonly_vars_
.insert(v);
       
return *this;
   
}


A description about read-only would also be nice!

Amirouche Boubekki

unread,
Oct 27, 2014, 9:09:48 AM10/27/14
to overcon...@googlegroups.com
2014-10-06 12:48 GMT+02:00 Håvard Fossli <hfo...@agens.no>:

Excellent! 

I am not sure about the format – wether it should be a tutorial, an faq or just plain descriptive chunks. Anyway, here's some stuff I've been wanting to read about. 

Here is my try at answering the questions:

  • What is the relation between strength on stay variables and strength on constraints?
stay means it will never be changed by the algorithm.

Strength and Weight can be compared to CSS specificity [1]. It is used to order the constraints using tuple comparison like (1, 0) < (0, 3), I don't know the algorithm, but I assume that it can also drop a constraint with (1, 0) to use the (0, 3) if the constraint with (1, 0) is not possible to achieve.

[1] http://www.stuffandnonsense.co.uk/archives/css_specificity_wars.html
 
  • When is stay constants appropriate? When are they not? Should one mix stay and edit variables?
It can be a mix, like screen size, or fluid vs fixed layout.
 
  • How can I create external "read-only" variables? By setting a strength? Or is it by refraining from making it a stay variable? Much like constants there are some things that should never be updated by the solver. For instance the size of the screen on the device. The only time the size of the screen changes is when the user is rotating thus flipping the width and height. 
If you set a variable as stay, you can change it by editing it. cf. second code snipped in http://cassowary.readthedocs.org/en/latest/topics/theory.html#editing-constraints.
  • Relation between strengths. Is required special in some way? Can "required" have weight? 
  • How should one go about making infrastructure independent of its coordinate systems? By adding each variable in between the views to the constraint?
REQUIRED is the equivalent of stay for constraints, like:

 FULL_WIDTH = BORDER_LEFT + BORDER_LEFT + PADDING_LEFT + WIDTH + PADDING_RIGHT + BORDER_RIGHT + BORDER_RIGHT
 

Some in-depth explanation about terminology

  • variables = variables that you want to be computed
  • stay variables = a constant
  • edit variables = when a variable is declared as stay, you can edit using edit variable function to change it
  • strength = defines constraints force as x in (x, y) tuple, y is weight
  • constraint = a arithmetic relation between variables: W1 + W2 < W3
  • inequality expression = it's a constraint that is defined with the inequality operator "!=", it can also be superior ">" or inferior "<" or equal '='
  • simplex solver = it's an algorithm that solve a constraint problem ie. a set of constraints and value to minize

HTH

I would love to write about this, but I don't have the required experience nor knowledge to do so.

--
Reply all
Reply to author
Forward
0 new messages