-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
for #410, improvements to user layout delation
* beqin conversion of cassowary solver from python to ruby
- Loading branch information
Cecil
committed
Oct 15, 2018
1 parent
ffec6c9
commit d117ff1
Showing
27 changed files
with
4,632 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from __future__ import print_function, unicode_literals, absolute_import | ||
|
||
from .expression import Variable | ||
from .error import RequiredFailure, ConstraintNotFound, InternalError | ||
from .simplex_solver import SimplexSolver | ||
from .utils import REQUIRED, STRONG, MEDIUM, WEAK | ||
|
||
# Examples of valid version strings | ||
# __version__ = '1.2.3.dev1' # Development release 1 | ||
# __version__ = '1.2.3a1' # Alpha Release 1 | ||
# __version__ = '1.2.3b1' # Beta Release 1 | ||
# __version__ = '1.2.3rc1' # RC Release 1 | ||
# __version__ = '1.2.3' # Final Release | ||
# __version__ = '1.2.3.post1' # Post Release 1 | ||
|
||
__version__ = '0.5.1' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,238 @@ | ||
module Cassowary | ||
########################################################################### | ||
# Constraint | ||
# | ||
# Constraints are the restrictions on linear programming; an equality or | ||
# inequality between two expressions. | ||
########################################################################### | ||
=begin | ||
class AbstractConstraint(object): | ||
def __init__(self, strength, weight=1.0): | ||
self.strength = strength | ||
self.weight = weight | ||
self.is_edit_constraint = False | ||
self.is_inequality = False | ||
self.is_stay_constraint = False | ||
@property | ||
def is_required(self): | ||
return self.strength == REQUIRED | ||
def __repr__(self): | ||
return '%s:{%s}(%s)' % (repr_strength(self.strength), self.weight, self.expression) | ||
=end | ||
class AbstractConstraint | ||
attr_accessor :strength, :weight, :s_edit_constraint, :is_inequality, | ||
:is_stay_contraint | ||
|
||
def initialize(strength, weight=1.0) | ||
@strength = strength | ||
@weight = weight | ||
@is_edit_constraint = false | ||
@is_inequality = false | ||
@is_stay_contraint = false | ||
end | ||
|
||
def is_required | ||
@strength == REQUIRED | ||
end | ||
end | ||
|
||
class EditConstraint < AbstractConstraint | ||
attr_accessor :variable, :expression | ||
|
||
def initialize(variable, strength=STRONG, weight=1.0) | ||
super(strength, weight) | ||
@variable = variable | ||
@expression = Expression.new(variable, -1.0, variable.value) | ||
@is_edit_constraint = true | ||
end | ||
end | ||
|
||
class StayConstraint < AbstractConstraint | ||
attr_accessor :variable, :expression | ||
|
||
def initialize(variable, strength=STRONG, weight=1.0) | ||
super(strength, weight) | ||
@variable = variable | ||
@expression = Expression.new(variable, -1.0, variable.value) | ||
@is_stay_constraint = true | ||
end | ||
end | ||
|
||
=begin | ||
class EditConstraint(AbstractConstraint): | ||
def __init__(self, variable, strength=STRONG, weight=1.0): | ||
super(EditConstraint, self).__init__(strength, weight) | ||
self.variable = variable | ||
self.expression = Expression(variable, -1.0, variable.value) | ||
self.is_edit_constraint = True | ||
def __repr__(self): | ||
return 'edit:%s' % super(EditConstraint, self).__repr__() | ||
class StayConstraint(AbstractConstraint): | ||
def __init__(self, variable, strength=STRONG, weight=1.0): | ||
super(StayConstraint, self).__init__(strength, weight) | ||
self.variable = variable | ||
self.expression = Expression(variable, -1.0, variable.value) | ||
self.is_stay_constraint=True | ||
def __repr__(self): | ||
return 'stay:%s' % super(StayConstraint, self).__repr__() | ||
=end | ||
|
||
class Constraint < AbstractConstraint | ||
LEQ = -1 | ||
EQ = 0 | ||
GEQ = 1 | ||
|
||
def initialize(param1, operator=EQ, param2=None, strength=REQUIRED, weight=1.0) | ||
# Define a new linear constraint. | ||
# | ||
# param1 may be an expression or variable | ||
# param2 may be an expression, variable, or constant, or may be ommitted entirely. | ||
# If param2 is specified, the operator must be either LEQ, EQ, or GEQ | ||
|
||
if param1.kind_of? Expression | ||
if param2 == nil | ||
super(strength=strength, weight=weight) | ||
@expression = param1 | ||
elsif param2.kind_of? Expression | ||
super(strength=strength, weight=weight) | ||
@expression = param1.clone() | ||
if operator == self.LEQ | ||
@expression.multiply(-1.0) | ||
@expression.add_expression(param2, 1.0) | ||
elsif operator == self.EQ | ||
@expression.add_expression(param2, -1.0) | ||
elsif operator == self.GEQ | ||
@expression.add_expression(param2, -1.0) | ||
else | ||
raise InternalError("Invalid operator in Constraint constructor") | ||
end | ||
elsif param2.kind_of? Variable | ||
super(strength=strength, weight=weight) | ||
@expression = param1.clone() | ||
if operator == self.LEQ | ||
@expression.multiply(-1.0) | ||
@expression.add_variable(param2, 1.0) | ||
elsif operator == self.EQ | ||
@expression.add_variable(param2, -1.0) | ||
elsif operator == self.GEQ | ||
@expression.add_variable(param2, -1.0) | ||
else | ||
raise InternalError("Invalid operator in Constraint constructor") | ||
end | ||
elsif param2.kind_of? Numeric | ||
super(strength=strength, weight=weight) | ||
@expression = param1.clone() | ||
if operator == self.LEQ | ||
@expression.multiply(-1.0) | ||
@expression.add_expression(Expression(constant=param2), 1.0) | ||
elsif operator == self.EQ | ||
@expression.add_expression(Expression(constant=param2), -1.0) | ||
elsif operator == self.GEQ | ||
@expression.add_expression(Expression(constant=param2), -1.0) | ||
else | ||
raise InternalError("Invalid operator in Constraint constructor") | ||
end | ||
else | ||
raise InternalError("Invalid parameters to Constraint constructor") | ||
end | ||
elsif param1.kind_of? Variable | ||
if param2 == nil | ||
super(strength=strength, weight=weight) | ||
@expression = Expression.new(param1) | ||
elsif param2.kind_of? Expression | ||
super(strength=strength, weight=weight) | ||
@expression = param2.clone() | ||
if operator == self.LEQ | ||
@expression.add_variable(param1, -1.0) | ||
elsif operator == self.EQ | ||
@expression.add_variable(param1, -1.0) | ||
elsif operator == self.GEQ | ||
@expression.multiply(-1.0) | ||
@expression.add_variable(param1, 1.0) | ||
else | ||
raise InternalError("Invalid operator in Constraint constructor") | ||
end | ||
elsif param2.kind_of? Variable | ||
super(strength=strength, weight=weight) | ||
@expression = Expression.new(param2) | ||
if operator == self.LEQ | ||
@expression.add_variable(param1, -1.0) | ||
elsif operator == self.EQ | ||
@expression.add_variable(param1, -1.0) | ||
elsif operator == self.GEQ | ||
@expression.multiply(-1.0) | ||
@expression.add_variable(param1, 1.0) | ||
else | ||
raise InternalError("Invalid operator in Constraint constructor") | ||
end | ||
elsif param2.kind_of? Numeric | ||
super(strength=strength, weight=weight) | ||
@expression = Expression.new(constant=param2) | ||
if operator == self.LEQ | ||
@expression.add_variable(param1, -1.0) | ||
elsif operator == self.EQ | ||
@expression.add_variable(param1, -1.0) | ||
elsif operator == self.GEQ | ||
@expression.multiply(-1.0) | ||
@expression.add_variable(param1, 1.0) | ||
else | ||
raise InternalError("Invalid operator in Constraint constructor") | ||
end | ||
else | ||
raise InternalError("Invalid parameters to Constraint constructor") | ||
end | ||
elsif param1.kind_of? Numeric | ||
if param2 == nil | ||
super(strength=strength, weight=weight) | ||
@expression = Expression(constant=param1) | ||
elsif param2.kind_of? Expression | ||
super(strength=strength, weight=weight) | ||
@expression = param2.clone() | ||
if operator == self.LEQ | ||
@expression.add_expression(Expression.new(constant=param1), -1.0) | ||
elsif operator == self.EQ | ||
@expression.add_expression(Expression.new(constant=param1), -1.0) | ||
elsif operator == self.GEQ | ||
@expression.multiply(-1.0) | ||
@expression.add_expression(Expression.new(constant=param1), 1.0) | ||
else | ||
raise InternalError("Invalid operator in Constraint constructor") | ||
end | ||
elsif param2.kind_of? Variable | ||
super(strength=strength, weight=weight) | ||
@expression = Expression,new(constant=param1) | ||
if operator == self.LEQ | ||
@expression.add_variable(param2, -1.0) | ||
elsif operator == self.EQ | ||
@expression.add_variable(param2, -1.0) | ||
elsif operator == self.GEQ | ||
@expression.multiply(-1.0) | ||
@expression.add_variable(param2, 1.0) | ||
else | ||
raise InternalError("Invalid operator in Constraint constructor") | ||
end | ||
elsif param2.kind_of? Numeric | ||
raise InternalError("Cannot create an inequality between constants") | ||
else | ||
raise InternalError("Invalid parameters to Constraint constructor") | ||
end | ||
else | ||
raise InternalError("Invalid parameters to Constraint constructor") | ||
end | ||
@is_inequality = operator != self.EQ | ||
end | ||
|
||
def clone() | ||
c = Constraint(@expression, strength=@strength, weight=@weight) | ||
c.is_inequality = @is_inequality | ||
return c | ||
end | ||
end # class Constraint | ||
end # module Cassowary |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from __future__ import print_function, unicode_literals, absolute_import, division | ||
|
||
|
||
class EditInfo(object): | ||
def __init__(self, constraint, edit_plus, edit_minus, prev_edit_constant, index): | ||
self.constraint = constraint | ||
self.edit_plus = edit_plus | ||
self.edit_minus = edit_minus | ||
self.prev_edit_constant = prev_edit_constant | ||
self.index = index | ||
|
||
def __repr__(self): | ||
return '<cn=%s ep=%s em=%s pec=%s index=%s>' % ( | ||
self.constraint, | ||
self.edit_plus, | ||
self.edit_minus, | ||
self.prev_edit_constant, | ||
self.index | ||
) |
Oops, something went wrong.