Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
Front-End
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
abdullh.alsoleman
Front-End
Commits
7dcd8115
Commit
7dcd8115
authored
Jun 25, 2015
by
Chinmay Garde
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Avoid exposing internal classes from the cassowary library
parent
891085b7
Changes
10
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
125 additions
and
125 deletions
+125
-125
cassowary.dart
packages/cassowary/lib/cassowary.dart
+3
-3
constant_member.dart
packages/cassowary/lib/constant_member.dart
+1
-1
equation_member.dart
packages/cassowary/lib/equation_member.dart
+8
-8
expression.dart
packages/cassowary/lib/expression.dart
+11
-11
param.dart
packages/cassowary/lib/param.dart
+1
-1
parser_exception.dart
packages/cassowary/lib/parser_exception.dart
+1
-1
row.dart
packages/cassowary/lib/row.dart
+12
-12
solver.dart
packages/cassowary/lib/solver.dart
+85
-85
symbol.dart
packages/cassowary/lib/symbol.dart
+2
-2
term.dart
packages/cassowary/lib/term.dart
+1
-1
No files found.
packages/cassowary/lib/cassowary.dart
View file @
7dcd8115
// Copyright (c) 2015, <your name>. All rights reserved. Use of this source code
// Copyright (c) 2015 The Chromium Authors. All rights reserved.
// is governed by a BSD-style license that can be found in the LICENSE file.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/// The cassowary library.
library
cassowary
;
library
cassowary
;
part
'constraint.dart'
;
part
'constraint.dart'
;
...
...
packages/cassowary/lib/constant_member.dart
View file @
7dcd8115
...
@@ -4,7 +4,7 @@
...
@@ -4,7 +4,7 @@
part of
cassowary
;
part of
cassowary
;
class
ConstantMember
extends
EquationMember
{
class
ConstantMember
extends
_
EquationMember
{
double
value
=
0.0
;
double
value
=
0.0
;
bool
get
isConstant
=>
true
;
bool
get
isConstant
=>
true
;
...
...
packages/cassowary/lib/equation_member.dart
View file @
7dcd8115
...
@@ -4,26 +4,26 @@
...
@@ -4,26 +4,26 @@
part of
cassowary
;
part of
cassowary
;
abstract
class
EquationMember
{
abstract
class
_
EquationMember
{
Expression
asExpression
();
Expression
asExpression
();
bool
get
isConstant
;
bool
get
isConstant
;
double
get
value
;
double
get
value
;
Constraint
operator
>=(
EquationMember
m
)
=>
asExpression
()
>=
m
;
Constraint
operator
>=(
_
EquationMember
m
)
=>
asExpression
()
>=
m
;
Constraint
operator
<=(
EquationMember
m
)
=>
asExpression
()
<=
m
;
Constraint
operator
<=(
_
EquationMember
m
)
=>
asExpression
()
<=
m
;
/* Constraint */
operator
==(
EquationMember
m
)
=>
asExpression
()
==
m
;
/* Constraint */
operator
==(
_
EquationMember
m
)
=>
asExpression
()
==
m
;
Expression
operator
+(
EquationMember
m
)
=>
asExpression
()
+
m
;
Expression
operator
+(
_
EquationMember
m
)
=>
asExpression
()
+
m
;
Expression
operator
-(
EquationMember
m
)
=>
asExpression
()
-
m
;
Expression
operator
-(
_
EquationMember
m
)
=>
asExpression
()
-
m
;
Expression
operator
*(
EquationMember
m
)
=>
asExpression
()
*
m
;
Expression
operator
*(
_
EquationMember
m
)
=>
asExpression
()
*
m
;
Expression
operator
/(
EquationMember
m
)
=>
asExpression
()
/
m
;
Expression
operator
/(
_
EquationMember
m
)
=>
asExpression
()
/
m
;
int
get
hashCode
=>
int
get
hashCode
=>
throw
"An equation member is not comparable and cannot be added to collections"
;
throw
"An equation member is not comparable and cannot be added to collections"
;
...
...
packages/cassowary/lib/expression.dart
View file @
7dcd8115
...
@@ -4,7 +4,7 @@
...
@@ -4,7 +4,7 @@
part of
cassowary
;
part of
cassowary
;
class
Expression
extends
EquationMember
{
class
Expression
extends
_
EquationMember
{
final
List
<
Term
>
terms
;
final
List
<
Term
>
terms
;
final
double
constant
;
final
double
constant
;
...
@@ -21,7 +21,7 @@ class Expression extends EquationMember {
...
@@ -21,7 +21,7 @@ class Expression extends EquationMember {
Expression
asExpression
()
=>
this
;
Expression
asExpression
()
=>
this
;
Constraint
_createConstraint
(
Constraint
_createConstraint
(
EquationMember
/* rhs */
value
,
Relation
relation
)
{
_
EquationMember
/* rhs */
value
,
Relation
relation
)
{
if
(
value
is
ConstantMember
)
{
if
(
value
is
ConstantMember
)
{
return
new
Constraint
(
return
new
Constraint
(
new
Expression
(
new
List
.
from
(
terms
),
constant
-
value
.
value
),
new
Expression
(
new
List
.
from
(
terms
),
constant
-
value
.
value
),
...
@@ -51,16 +51,16 @@ class Expression extends EquationMember {
...
@@ -51,16 +51,16 @@ class Expression extends EquationMember {
return
null
;
return
null
;
}
}
Constraint
operator
>=(
EquationMember
value
)
=>
Constraint
operator
>=(
_
EquationMember
value
)
=>
_createConstraint
(
value
,
Relation
.
greaterThanOrEqualTo
);
_createConstraint
(
value
,
Relation
.
greaterThanOrEqualTo
);
Constraint
operator
<=(
EquationMember
value
)
=>
Constraint
operator
<=(
_
EquationMember
value
)
=>
_createConstraint
(
value
,
Relation
.
lessThanOrEqualTo
);
_createConstraint
(
value
,
Relation
.
lessThanOrEqualTo
);
operator
==(
EquationMember
value
)
=>
operator
==(
_
EquationMember
value
)
=>
_createConstraint
(
value
,
Relation
.
equalTo
);
_createConstraint
(
value
,
Relation
.
equalTo
);
Expression
operator
+(
EquationMember
m
)
{
Expression
operator
+(
_
EquationMember
m
)
{
if
(
m
is
ConstantMember
)
{
if
(
m
is
ConstantMember
)
{
return
new
Expression
(
new
List
.
from
(
terms
),
constant
+
m
.
value
);
return
new
Expression
(
new
List
.
from
(
terms
),
constant
+
m
.
value
);
}
}
...
@@ -83,7 +83,7 @@ class Expression extends EquationMember {
...
@@ -83,7 +83,7 @@ class Expression extends EquationMember {
return
null
;
return
null
;
}
}
Expression
operator
-(
EquationMember
m
)
{
Expression
operator
-(
_
EquationMember
m
)
{
if
(
m
is
ConstantMember
)
{
if
(
m
is
ConstantMember
)
{
return
new
Expression
(
new
List
.
from
(
terms
),
constant
-
m
.
value
);
return
new
Expression
(
new
List
.
from
(
terms
),
constant
-
m
.
value
);
}
}
...
@@ -109,13 +109,13 @@ class Expression extends EquationMember {
...
@@ -109,13 +109,13 @@ class Expression extends EquationMember {
return
null
;
return
null
;
}
}
EquationMember
_applyMultiplicand
(
double
m
)
{
_
EquationMember
_applyMultiplicand
(
double
m
)
{
var
newTerms
=
terms
.
fold
(
new
List
<
Term
>(),
(
list
,
term
)
=>
list
var
newTerms
=
terms
.
fold
(
new
List
<
Term
>(),
(
list
,
term
)
=>
list
..
add
(
new
Term
(
term
.
variable
,
term
.
coefficient
*
m
)));
..
add
(
new
Term
(
term
.
variable
,
term
.
coefficient
*
m
)));
return
new
Expression
(
newTerms
,
constant
*
m
);
return
new
Expression
(
newTerms
,
constant
*
m
);
}
}
_Pair
<
Expression
,
double
>
_findMulitplierAndMultiplicand
(
EquationMember
m
)
{
_Pair
<
Expression
,
double
>
_findMulitplierAndMultiplicand
(
_
EquationMember
m
)
{
// At least on of the the two members must be constant for the resulting
// At least on of the the two members must be constant for the resulting
// expression to be linear
// expression to be linear
...
@@ -135,7 +135,7 @@ class Expression extends EquationMember {
...
@@ -135,7 +135,7 @@ class Expression extends EquationMember {
return
null
;
return
null
;
}
}
EquationMember
operator
*(
EquationMember
m
)
{
_EquationMember
operator
*(
_
EquationMember
m
)
{
_Pair
<
Expression
,
double
>
args
=
_findMulitplierAndMultiplicand
(
m
);
_Pair
<
Expression
,
double
>
args
=
_findMulitplierAndMultiplicand
(
m
);
if
(
args
==
null
)
{
if
(
args
==
null
)
{
...
@@ -147,7 +147,7 @@ class Expression extends EquationMember {
...
@@ -147,7 +147,7 @@ class Expression extends EquationMember {
return
args
.
first
.
_applyMultiplicand
(
args
.
second
);
return
args
.
first
.
_applyMultiplicand
(
args
.
second
);
}
}
EquationMember
operator
/(
EquationMember
m
)
{
_EquationMember
operator
/(
_
EquationMember
m
)
{
if
(!
m
.
isConstant
)
{
if
(!
m
.
isConstant
)
{
throw
new
ParserException
(
throw
new
ParserException
(
"The divisor was not a constant expression"
,
[
this
,
m
]);
"The divisor was not a constant expression"
,
[
this
,
m
]);
...
...
packages/cassowary/lib/param.dart
View file @
7dcd8115
...
@@ -4,7 +4,7 @@
...
@@ -4,7 +4,7 @@
part of
cassowary
;
part of
cassowary
;
class
Param
extends
EquationMember
{
class
Param
extends
_
EquationMember
{
final
Variable
variable
;
final
Variable
variable
;
Param
.
withVariable
(
this
.
variable
);
Param
.
withVariable
(
this
.
variable
);
...
...
packages/cassowary/lib/parser_exception.dart
View file @
7dcd8115
...
@@ -6,7 +6,7 @@ part of cassowary;
...
@@ -6,7 +6,7 @@ part of cassowary;
class
ParserException
implements
Exception
{
class
ParserException
implements
Exception
{
final
String
message
;
final
String
message
;
List
<
EquationMember
>
members
;
List
<
_
EquationMember
>
members
;
ParserException
(
this
.
message
,
this
.
members
);
ParserException
(
this
.
message
,
this
.
members
);
String
toString
()
{
String
toString
()
{
...
...
packages/cassowary/lib/row.dart
View file @
7dcd8115
...
@@ -4,18 +4,18 @@
...
@@ -4,18 +4,18 @@
part of
cassowary
;
part of
cassowary
;
class
Row
{
class
_
Row
{
final
Map
<
Symbol
,
double
>
cells
;
final
Map
<
_
Symbol
,
double
>
cells
;
double
constant
=
0.0
;
double
constant
=
0.0
;
Row
(
this
.
constant
)
:
this
.
cells
=
new
Map
<
Symbol
,
double
>();
_Row
(
this
.
constant
)
:
this
.
cells
=
new
Map
<
_
Symbol
,
double
>();
Row
.
fromRow
(
Row
row
)
_Row
.
fromRow
(
_
Row
row
)
:
this
.
cells
=
new
Map
<
Symbol
,
double
>.
from
(
row
.
cells
),
:
this
.
cells
=
new
Map
<
_
Symbol
,
double
>.
from
(
row
.
cells
),
this
.
constant
=
row
.
constant
;
this
.
constant
=
row
.
constant
;
double
add
(
double
value
)
=>
constant
+=
value
;
double
add
(
double
value
)
=>
constant
+=
value
;
void
insertSymbol
(
Symbol
symbol
,
[
double
coefficient
=
1.0
])
{
void
insertSymbol
(
_
Symbol
symbol
,
[
double
coefficient
=
1.0
])
{
double
val
=
_elvis
(
cells
[
symbol
],
0.0
)
+
coefficient
;
double
val
=
_elvis
(
cells
[
symbol
],
0.0
)
+
coefficient
;
if
(
_nearZero
(
val
))
{
if
(
_nearZero
(
val
))
{
...
@@ -25,18 +25,18 @@ class Row {
...
@@ -25,18 +25,18 @@ class Row {
}
}
}
}
void
insertRow
(
Row
other
,
[
double
coefficient
=
1.0
])
{
void
insertRow
(
_
Row
other
,
[
double
coefficient
=
1.0
])
{
constant
+=
other
.
constant
*
coefficient
;
constant
+=
other
.
constant
*
coefficient
;
other
.
cells
.
forEach
((
s
,
v
)
=>
insertSymbol
(
s
,
v
*
coefficient
));
other
.
cells
.
forEach
((
s
,
v
)
=>
insertSymbol
(
s
,
v
*
coefficient
));
}
}
void
removeSymbol
(
Symbol
symbol
)
{
void
removeSymbol
(
_
Symbol
symbol
)
{
cells
.
remove
(
symbol
);
cells
.
remove
(
symbol
);
}
}
void
reverseSign
()
=>
cells
.
forEach
((
s
,
v
)
=>
cells
[
s
]
=
-
v
);
void
reverseSign
()
=>
cells
.
forEach
((
s
,
v
)
=>
cells
[
s
]
=
-
v
);
void
solveForSymbol
(
Symbol
symbol
)
{
void
solveForSymbol
(
_
Symbol
symbol
)
{
assert
(
cells
.
containsKey
(
symbol
));
assert
(
cells
.
containsKey
(
symbol
));
double
coefficient
=
-
1.0
/
cells
[
symbol
];
double
coefficient
=
-
1.0
/
cells
[
symbol
];
cells
.
remove
(
symbol
);
cells
.
remove
(
symbol
);
...
@@ -44,14 +44,14 @@ class Row {
...
@@ -44,14 +44,14 @@ class Row {
cells
.
forEach
((
s
,
v
)
=>
cells
[
s
]
=
v
*
coefficient
);
cells
.
forEach
((
s
,
v
)
=>
cells
[
s
]
=
v
*
coefficient
);
}
}
void
solveForSymbols
(
Symbol
lhs
,
Symbol
rhs
)
{
void
solveForSymbols
(
_Symbol
lhs
,
_
Symbol
rhs
)
{
insertSymbol
(
lhs
,
-
1.0
);
insertSymbol
(
lhs
,
-
1.0
);
solveForSymbol
(
rhs
);
solveForSymbol
(
rhs
);
}
}
double
coefficientForSymbol
(
Symbol
symbol
)
=>
_elvis
(
cells
[
symbol
],
0.0
);
double
coefficientForSymbol
(
_
Symbol
symbol
)
=>
_elvis
(
cells
[
symbol
],
0.0
);
void
substitute
(
Symbol
symbol
,
Row
row
)
{
void
substitute
(
_Symbol
symbol
,
_
Row
row
)
{
double
coefficient
=
cells
[
symbol
];
double
coefficient
=
cells
[
symbol
];
if
(
coefficient
==
null
)
{
if
(
coefficient
==
null
)
{
...
...
packages/cassowary/lib/solver.dart
View file @
7dcd8115
...
@@ -5,13 +5,13 @@
...
@@ -5,13 +5,13 @@
part of
cassowary
;
part of
cassowary
;
class
Solver
{
class
Solver
{
final
Map
<
Constraint
,
Tag
>
_constraints
=
new
Map
<
Constraint
,
Tag
>();
final
Map
<
Constraint
,
_Tag
>
_constraints
=
new
Map
<
Constraint
,
_
Tag
>();
final
Map
<
Symbol
,
Row
>
_rows
=
new
Map
<
Symbol
,
Row
>();
final
Map
<
_Symbol
,
_Row
>
_rows
=
new
Map
<
_Symbol
,
_
Row
>();
final
Map
<
Variable
,
Symbol
>
_vars
=
new
Map
<
Variable
,
Symbol
>();
final
Map
<
Variable
,
_Symbol
>
_vars
=
new
Map
<
Variable
,
_
Symbol
>();
final
Map
<
Variable
,
EditInfo
>
_edits
=
new
Map
<
Variable
,
EditInfo
>();
final
Map
<
Variable
,
_EditInfo
>
_edits
=
new
Map
<
Variable
,
_
EditInfo
>();
final
List
<
Symbol
>
_infeasibleRows
=
new
List
<
Symbol
>();
final
List
<
_Symbol
>
_infeasibleRows
=
new
List
<
_
Symbol
>();
final
Row
_objective
=
new
Row
(
0.0
);
final
_Row
_objective
=
new
_
Row
(
0.0
);
Row
_artificial
=
new
Row
(
0.0
);
_Row
_artificial
=
new
_
Row
(
0.0
);
int
tick
=
0
;
int
tick
=
0
;
Result
addConstraint
(
Constraint
constraint
)
{
Result
addConstraint
(
Constraint
constraint
)
{
...
@@ -19,12 +19,12 @@ class Solver {
...
@@ -19,12 +19,12 @@ class Solver {
return
Result
.
duplicateConstraint
;
return
Result
.
duplicateConstraint
;
}
}
Tag
tag
=
new
Tag
(
_Tag
tag
=
new
_
Tag
(
new
Symbol
(
SymbolType
.
invalid
,
0
),
new
Symbol
(
SymbolType
.
invalid
,
0
));
new
_Symbol
(
SymbolType
.
invalid
,
0
),
new
_
Symbol
(
SymbolType
.
invalid
,
0
));
Row
row
=
_createRow
(
constraint
,
tag
);
_
Row
row
=
_createRow
(
constraint
,
tag
);
Symbol
subject
=
_chooseSubjectForRow
(
row
,
tag
);
_
Symbol
subject
=
_chooseSubjectForRow
(
row
,
tag
);
if
(
subject
.
type
==
SymbolType
.
invalid
&&
_allDummiesInRow
(
row
))
{
if
(
subject
.
type
==
SymbolType
.
invalid
&&
_allDummiesInRow
(
row
))
{
if
(!
_nearZero
(
row
.
constant
))
{
if
(!
_nearZero
(
row
.
constant
))
{
...
@@ -50,28 +50,28 @@ class Solver {
...
@@ -50,28 +50,28 @@ class Solver {
}
}
Result
removeConstraint
(
Constraint
constraint
)
{
Result
removeConstraint
(
Constraint
constraint
)
{
Tag
tag
=
_constraints
[
constraint
];
_
Tag
tag
=
_constraints
[
constraint
];
if
(
tag
==
null
)
{
if
(
tag
==
null
)
{
return
Result
.
unknownConstraint
;
return
Result
.
unknownConstraint
;
}
}
tag
=
new
Tag
.
fromTag
(
tag
);
tag
=
new
_
Tag
.
fromTag
(
tag
);
_constraints
.
remove
(
constraint
);
_constraints
.
remove
(
constraint
);
_removeConstraintEffects
(
constraint
,
tag
);
_removeConstraintEffects
(
constraint
,
tag
);
Row
row
=
_rows
[
tag
.
marker
];
_
Row
row
=
_rows
[
tag
.
marker
];
if
(
row
!=
null
)
{
if
(
row
!=
null
)
{
_rows
.
remove
(
tag
.
marker
);
_rows
.
remove
(
tag
.
marker
);
}
else
{
}
else
{
_Pair
<
Symbol
,
Row
>
rowPair
=
_Pair
<
_Symbol
,
_
Row
>
rowPair
=
_getLeavingRowPairForMarkerSymbol
(
tag
.
marker
);
_getLeavingRowPairForMarkerSymbol
(
tag
.
marker
);
if
(
rowPair
==
null
)
{
if
(
rowPair
==
null
)
{
return
Result
.
internalSolverError
;
return
Result
.
internalSolverError
;
}
}
Symbol
leaving
=
rowPair
.
first
;
_
Symbol
leaving
=
rowPair
.
first
;
row
=
rowPair
.
second
;
row
=
rowPair
.
second
;
var
removed
=
_rows
.
remove
(
rowPair
.
first
);
var
removed
=
_rows
.
remove
(
rowPair
.
first
);
assert
(
removed
!=
null
);
assert
(
removed
!=
null
);
...
@@ -102,7 +102,7 @@ class Solver {
...
@@ -102,7 +102,7 @@ class Solver {
return
Result
.
internalSolverError
;
return
Result
.
internalSolverError
;
}
}
EditInfo
info
=
new
EditInfo
();
_EditInfo
info
=
new
_
EditInfo
();
info
.
tag
=
_constraints
[
constraint
];
info
.
tag
=
_constraints
[
constraint
];
info
.
constraint
=
constraint
;
info
.
constraint
=
constraint
;
info
.
constant
=
0.0
;
info
.
constant
=
0.0
;
...
@@ -113,7 +113,7 @@ class Solver {
...
@@ -113,7 +113,7 @@ class Solver {
}
}
Result
removeEditVariable
(
Variable
variable
)
{
Result
removeEditVariable
(
Variable
variable
)
{
EditInfo
info
=
_edits
[
variable
];
_
EditInfo
info
=
_edits
[
variable
];
if
(
info
==
null
)
{
if
(
info
==
null
)
{
return
Result
.
unknownEditVariable
;
return
Result
.
unknownEditVariable
;
}
}
...
@@ -142,8 +142,8 @@ class Solver {
...
@@ -142,8 +142,8 @@ class Solver {
void
updateVariable
()
{
void
updateVariable
()
{
for
(
Variable
variable
in
_vars
.
keys
)
{
for
(
Variable
variable
in
_vars
.
keys
)
{
Symbol
symbol
=
_vars
[
variable
];
_
Symbol
symbol
=
_vars
[
variable
];
Row
row
=
_rows
[
symbol
];
_
Row
row
=
_rows
[
symbol
];
if
(
row
==
null
)
{
if
(
row
==
null
)
{
variable
.
value
=
0.0
;
variable
.
value
=
0.0
;
}
else
{
}
else
{
...
@@ -154,28 +154,28 @@ class Solver {
...
@@ -154,28 +154,28 @@ class Solver {
Solver
operator
<<(
Constraint
c
)
=>
this
..
addConstraint
(
c
);
Solver
operator
<<(
Constraint
c
)
=>
this
..
addConstraint
(
c
);
Symbol
_getSymbolForVariable
(
Variable
variable
)
{
_
Symbol
_getSymbolForVariable
(
Variable
variable
)
{
Symbol
symbol
=
_vars
[
variable
];
_
Symbol
symbol
=
_vars
[
variable
];
if
(
symbol
!=
null
)
{
if
(
symbol
!=
null
)
{
return
symbol
;
return
symbol
;
}
}
symbol
=
new
Symbol
(
SymbolType
.
external
,
tick
++);
symbol
=
new
_
Symbol
(
SymbolType
.
external
,
tick
++);
_vars
[
variable
]
=
symbol
;
_vars
[
variable
]
=
symbol
;
return
symbol
;
return
symbol
;
}
}
Row
_createRow
(
Constraint
constraint
,
Tag
tag
)
{
_Row
_createRow
(
Constraint
constraint
,
_
Tag
tag
)
{
Expression
expr
=
new
Expression
.
fromExpression
(
constraint
.
expression
);
Expression
expr
=
new
Expression
.
fromExpression
(
constraint
.
expression
);
Row
row
=
new
Row
(
expr
.
constant
);
_Row
row
=
new
_
Row
(
expr
.
constant
);
expr
.
terms
.
forEach
((
term
)
{
expr
.
terms
.
forEach
((
term
)
{
if
(!
_nearZero
(
term
.
coefficient
))
{
if
(!
_nearZero
(
term
.
coefficient
))
{
Symbol
symbol
=
_getSymbolForVariable
(
term
.
variable
);
_
Symbol
symbol
=
_getSymbolForVariable
(
term
.
variable
);
Row
foundRow
=
_rows
[
symbol
];
_
Row
foundRow
=
_rows
[
symbol
];
if
(
foundRow
!=
null
)
{
if
(
foundRow
!=
null
)
{
row
.
insertRow
(
foundRow
,
term
.
coefficient
);
row
.
insertRow
(
foundRow
,
term
.
coefficient
);
...
@@ -192,12 +192,12 @@ class Solver {
...
@@ -192,12 +192,12 @@ class Solver {
double
coefficient
=
double
coefficient
=
constraint
.
relation
==
Relation
.
lessThanOrEqualTo
?
1.0
:
-
1.0
;
constraint
.
relation
==
Relation
.
lessThanOrEqualTo
?
1.0
:
-
1.0
;
Symbol
slack
=
new
Symbol
(
SymbolType
.
slack
,
tick
++);
_Symbol
slack
=
new
_
Symbol
(
SymbolType
.
slack
,
tick
++);
tag
.
marker
=
slack
;
tag
.
marker
=
slack
;
row
.
insertSymbol
(
slack
,
coefficient
);
row
.
insertSymbol
(
slack
,
coefficient
);
if
(!
constraint
.
required
)
{
if
(!
constraint
.
required
)
{
Symbol
error
=
new
Symbol
(
SymbolType
.
error
,
tick
++);
_Symbol
error
=
new
_
Symbol
(
SymbolType
.
error
,
tick
++);
tag
.
other
=
error
;
tag
.
other
=
error
;
row
.
insertSymbol
(
error
,
-
coefficient
);
row
.
insertSymbol
(
error
,
-
coefficient
);
_objective
.
insertSymbol
(
error
,
constraint
.
priority
);
_objective
.
insertSymbol
(
error
,
constraint
.
priority
);
...
@@ -206,8 +206,8 @@ class Solver {
...
@@ -206,8 +206,8 @@ class Solver {
break
;
break
;
case
Relation
.
equalTo
:
case
Relation
.
equalTo
:
if
(!
constraint
.
required
)
{
if
(!
constraint
.
required
)
{
Symbol
errPlus
=
new
Symbol
(
SymbolType
.
error
,
tick
++);
_Symbol
errPlus
=
new
_
Symbol
(
SymbolType
.
error
,
tick
++);
Symbol
errMinus
=
new
Symbol
(
SymbolType
.
error
,
tick
++);
_Symbol
errMinus
=
new
_
Symbol
(
SymbolType
.
error
,
tick
++);
tag
.
marker
=
errPlus
;
tag
.
marker
=
errPlus
;
tag
.
other
=
errMinus
;
tag
.
other
=
errMinus
;
row
.
insertSymbol
(
errPlus
,
-
1.0
);
row
.
insertSymbol
(
errPlus
,
-
1.0
);
...
@@ -215,7 +215,7 @@ class Solver {
...
@@ -215,7 +215,7 @@ class Solver {
_objective
.
insertSymbol
(
errPlus
,
constraint
.
priority
);
_objective
.
insertSymbol
(
errPlus
,
constraint
.
priority
);
_objective
.
insertSymbol
(
errMinus
,
constraint
.
priority
);
_objective
.
insertSymbol
(
errMinus
,
constraint
.
priority
);
}
else
{
}
else
{
Symbol
dummy
=
new
Symbol
(
SymbolType
.
dummy
,
tick
++);
_Symbol
dummy
=
new
_
Symbol
(
SymbolType
.
dummy
,
tick
++);
tag
.
marker
=
dummy
;
tag
.
marker
=
dummy
;
row
.
insertSymbol
(
dummy
);
row
.
insertSymbol
(
dummy
);
}
}
...
@@ -229,8 +229,8 @@ class Solver {
...
@@ -229,8 +229,8 @@ class Solver {
return
row
;
return
row
;
}
}
Symbol
_chooseSubjectForRow
(
Row
row
,
Tag
tag
)
{
_Symbol
_chooseSubjectForRow
(
_Row
row
,
_
Tag
tag
)
{
for
(
Symbol
symbol
in
row
.
cells
.
keys
)
{
for
(
_
Symbol
symbol
in
row
.
cells
.
keys
)
{
if
(
symbol
.
type
==
SymbolType
.
external
)
{
if
(
symbol
.
type
==
SymbolType
.
external
)
{
return
symbol
;
return
symbol
;
}
}
...
@@ -250,11 +250,11 @@ class Solver {
...
@@ -250,11 +250,11 @@ class Solver {
}
}
}
}
return
new
Symbol
(
SymbolType
.
invalid
,
0
);
return
new
_
Symbol
(
SymbolType
.
invalid
,
0
);
}
}
bool
_allDummiesInRow
(
Row
row
)
{
bool
_allDummiesInRow
(
_
Row
row
)
{
for
(
Symbol
symbol
in
row
.
cells
.
keys
)
{
for
(
_
Symbol
symbol
in
row
.
cells
.
keys
)
{
if
(
symbol
.
type
!=
SymbolType
.
dummy
)
{
if
(
symbol
.
type
!=
SymbolType
.
dummy
)
{
return
false
;
return
false
;
}
}
...
@@ -262,10 +262,10 @@ class Solver {
...
@@ -262,10 +262,10 @@ class Solver {
return
true
;
return
true
;
}
}
bool
_addWithArtificialVariableOnRow
(
Row
row
)
{
bool
_addWithArtificialVariableOnRow
(
_
Row
row
)
{
Symbol
artificial
=
new
Symbol
(
SymbolType
.
slack
,
tick
++);
_Symbol
artificial
=
new
_
Symbol
(
SymbolType
.
slack
,
tick
++);
_rows
[
artificial
]
=
new
Row
.
fromRow
(
row
);
_rows
[
artificial
]
=
new
_
Row
.
fromRow
(
row
);
_artificial
=
new
Row
.
fromRow
(
row
);
_artificial
=
new
_
Row
.
fromRow
(
row
);
Result
result
=
_optimizeObjectiveRow
(
_artificial
);
Result
result
=
_optimizeObjectiveRow
(
_artificial
);
...
@@ -275,16 +275,16 @@ class Solver {
...
@@ -275,16 +275,16 @@ class Solver {
}
}
bool
success
=
_nearZero
(
_artificial
.
constant
);
bool
success
=
_nearZero
(
_artificial
.
constant
);
_artificial
=
new
Row
(
0.0
);
_artificial
=
new
_
Row
(
0.0
);
Row
foundRow
=
_rows
[
artificial
];
_
Row
foundRow
=
_rows
[
artificial
];
if
(
foundRow
!=
null
)
{
if
(
foundRow
!=
null
)
{
_rows
.
remove
(
artificial
);
_rows
.
remove
(
artificial
);
if
(
foundRow
.
cells
.
isEmpty
)
{
if
(
foundRow
.
cells
.
isEmpty
)
{
return
success
;
return
success
;
}
}
Symbol
entering
=
_anyPivotableSymbol
(
foundRow
);
_
Symbol
entering
=
_anyPivotableSymbol
(
foundRow
);
if
(
entering
.
type
==
SymbolType
.
invalid
)
{
if
(
entering
.
type
==
SymbolType
.
invalid
)
{
return
false
;
return
false
;
}
}
...
@@ -294,29 +294,29 @@ class Solver {
...
@@ -294,29 +294,29 @@ class Solver {
_rows
[
entering
]
=
foundRow
;
_rows
[
entering
]
=
foundRow
;
}
}
for
(
Row
row
in
_rows
.
values
)
{
for
(
_
Row
row
in
_rows
.
values
)
{
row
.
removeSymbol
(
artificial
);
row
.
removeSymbol
(
artificial
);
}
}
_objective
.
removeSymbol
(
artificial
);
_objective
.
removeSymbol
(
artificial
);
return
success
;
return
success
;
}
}
Result
_optimizeObjectiveRow
(
Row
objective
)
{
Result
_optimizeObjectiveRow
(
_
Row
objective
)
{
while
(
true
)
{
while
(
true
)
{
Symbol
entering
=
_getEnteringSymbolForObjectiveRow
(
objective
);
_
Symbol
entering
=
_getEnteringSymbolForObjectiveRow
(
objective
);
if
(
entering
.
type
==
SymbolType
.
invalid
)
{
if
(
entering
.
type
==
SymbolType
.
invalid
)
{
return
Result
.
success
;
return
Result
.
success
;
}
}
_Pair
<
Symbol
,
Row
>
leavingPair
=
_Pair
<
_Symbol
,
_
Row
>
leavingPair
=
_getLeavingRowForEnteringSymbol
(
entering
);
_getLeavingRowForEnteringSymbol
(
entering
);
if
(
leavingPair
==
null
)
{
if
(
leavingPair
==
null
)
{
return
Result
.
internalSolverError
;
return
Result
.
internalSolverError
;
}
}
Symbol
leaving
=
leavingPair
.
first
;
_
Symbol
leaving
=
leavingPair
.
first
;
Row
row
=
leavingPair
.
second
;
_
Row
row
=
leavingPair
.
second
;
_rows
.
remove
(
leavingPair
.
first
);
_rows
.
remove
(
leavingPair
.
first
);
row
.
solveForSymbols
(
leaving
,
entering
);
row
.
solveForSymbols
(
leaving
,
entering
);
_substitute
(
entering
,
row
);
_substitute
(
entering
,
row
);
...
@@ -324,21 +324,21 @@ class Solver {
...
@@ -324,21 +324,21 @@ class Solver {
}
}
}
}
Symbol
_getEnteringSymbolForObjectiveRow
(
Row
objective
)
{
_Symbol
_getEnteringSymbolForObjectiveRow
(
_
Row
objective
)
{
Map
<
Symbol
,
double
>
cells
=
objective
.
cells
;
Map
<
_
Symbol
,
double
>
cells
=
objective
.
cells
;
for
(
Symbol
symbol
in
cells
.
keys
)
{
for
(
_
Symbol
symbol
in
cells
.
keys
)
{
if
(
symbol
.
type
!=
SymbolType
.
dummy
&&
cells
[
symbol
]
<
0.0
)
{
if
(
symbol
.
type
!=
SymbolType
.
dummy
&&
cells
[
symbol
]
<
0.0
)
{
return
symbol
;
return
symbol
;
}
}
}
}
return
new
Symbol
(
SymbolType
.
invalid
,
0
);
return
new
_
Symbol
(
SymbolType
.
invalid
,
0
);
}
}
_Pair
<
Symbol
,
Row
>
_getLeavingRowForEnteringSymbol
(
Symbol
entering
)
{
_Pair
<
_Symbol
,
_Row
>
_getLeavingRowForEnteringSymbol
(
_
Symbol
entering
)
{
double
ratio
=
double
.
MAX_FINITE
;
double
ratio
=
double
.
MAX_FINITE
;
_Pair
<
Symbol
,
Row
>
result
=
new
_Pair
(
null
,
null
);
_Pair
<
_Symbol
,
_
Row
>
result
=
new
_Pair
(
null
,
null
);
_rows
.
forEach
((
symbol
,
row
)
{
_rows
.
forEach
((
symbol
,
row
)
{
if
(
symbol
.
type
!=
SymbolType
.
external
)
{
if
(
symbol
.
type
!=
SymbolType
.
external
)
{
...
@@ -363,7 +363,7 @@ class Solver {
...
@@ -363,7 +363,7 @@ class Solver {
return
result
;
return
result
;
}
}
void
_substitute
(
Symbol
symbol
,
Row
row
)
{
void
_substitute
(
_Symbol
symbol
,
_
Row
row
)
{
_rows
.
forEach
((
first
,
second
)
{
_rows
.
forEach
((
first
,
second
)
{
second
.
substitute
(
symbol
,
row
);
second
.
substitute
(
symbol
,
row
);
if
(
first
.
type
!=
SymbolType
.
external
&&
second
.
constant
<
0.0
)
{
if
(
first
.
type
!=
SymbolType
.
external
&&
second
.
constant
<
0.0
)
{
...
@@ -377,16 +377,16 @@ class Solver {
...
@@ -377,16 +377,16 @@ class Solver {
}
}
}
}
Symbol
_anyPivotableSymbol
(
Row
row
)
{
_Symbol
_anyPivotableSymbol
(
_
Row
row
)
{
for
(
Symbol
symbol
in
row
.
cells
.
keys
)
{
for
(
_
Symbol
symbol
in
row
.
cells
.
keys
)
{
if
(
symbol
.
type
==
SymbolType
.
slack
||
symbol
.
type
==
SymbolType
.
error
)
{
if
(
symbol
.
type
==
SymbolType
.
slack
||
symbol
.
type
==
SymbolType
.
error
)
{
return
symbol
;
return
symbol
;
}
}
}
}
return
new
Symbol
(
SymbolType
.
invalid
,
0
);
return
new
_
Symbol
(
SymbolType
.
invalid
,
0
);
}
}
void
_removeConstraintEffects
(
Constraint
cn
,
Tag
tag
)
{
void
_removeConstraintEffects
(
Constraint
cn
,
_
Tag
tag
)
{
if
(
tag
.
marker
.
type
==
SymbolType
.
error
)
{
if
(
tag
.
marker
.
type
==
SymbolType
.
error
)
{
_removeMarkerEffects
(
tag
.
marker
,
cn
.
priority
);
_removeMarkerEffects
(
tag
.
marker
,
cn
.
priority
);
}
}
...
@@ -395,8 +395,8 @@ class Solver {
...
@@ -395,8 +395,8 @@ class Solver {
}
}
}
}
void
_removeMarkerEffects
(
Symbol
marker
,
double
strength
)
{
void
_removeMarkerEffects
(
_
Symbol
marker
,
double
strength
)
{
Row
row
=
_rows
[
marker
];
_
Row
row
=
_rows
[
marker
];
if
(
row
!=
null
)
{
if
(
row
!=
null
)
{
_objective
.
insertRow
(
row
,
-
strength
);
_objective
.
insertRow
(
row
,
-
strength
);
}
else
{
}
else
{
...
@@ -404,11 +404,11 @@ class Solver {
...
@@ -404,11 +404,11 @@ class Solver {
}
}
}
}
_Pair
<
Symbol
,
Row
>
_getLeavingRowPairForMarkerSymbol
(
Symbol
marker
)
{
_Pair
<
_Symbol
,
_Row
>
_getLeavingRowPairForMarkerSymbol
(
_
Symbol
marker
)
{
double
r1
=
double
.
MAX_FINITE
;
double
r1
=
double
.
MAX_FINITE
;
double
r2
=
double
.
MAX_FINITE
;
double
r2
=
double
.
MAX_FINITE
;
_Pair
<
Symbol
,
Row
>
first
,
second
,
third
;
_Pair
<
_Symbol
,
_
Row
>
first
,
second
,
third
;
_rows
.
forEach
((
symbol
,
row
)
{
_rows
.
forEach
((
symbol
,
row
)
{
double
c
=
row
.
coefficientForSymbol
(
marker
);
double
c
=
row
.
coefficientForSymbol
(
marker
);
...
@@ -444,13 +444,13 @@ class Solver {
...
@@ -444,13 +444,13 @@ class Solver {
}
}
void
_suggestValueForEditInfoWithoutDualOptimization
(
void
_suggestValueForEditInfoWithoutDualOptimization
(
EditInfo
info
,
double
value
)
{
_
EditInfo
info
,
double
value
)
{
double
delta
=
value
-
info
.
constant
;
double
delta
=
value
-
info
.
constant
;
info
.
constant
=
value
;
info
.
constant
=
value
;
{
{
Symbol
symbol
=
info
.
tag
.
marker
;
_
Symbol
symbol
=
info
.
tag
.
marker
;
Row
row
=
_rows
[
info
.
tag
.
marker
];
_
Row
row
=
_rows
[
info
.
tag
.
marker
];
if
(
row
!=
null
)
{
if
(
row
!=
null
)
{
if
(
row
.
add
(-
delta
)
<
0.0
)
{
if
(
row
.
add
(-
delta
)
<
0.0
)
{
...
@@ -470,8 +470,8 @@ class Solver {
...
@@ -470,8 +470,8 @@ class Solver {
}
}
}
}
for
(
Symbol
symbol
in
_rows
.
keys
)
{
for
(
_
Symbol
symbol
in
_rows
.
keys
)
{
Row
row
=
_rows
[
symbol
];
_
Row
row
=
_rows
[
symbol
];
double
coeff
=
row
.
coefficientForSymbol
(
info
.
tag
.
marker
);
double
coeff
=
row
.
coefficientForSymbol
(
info
.
tag
.
marker
);
if
(
coeff
!=
0.0
&&
if
(
coeff
!=
0.0
&&
row
.
add
(
delta
*
coeff
)
<
0.0
&&
row
.
add
(
delta
*
coeff
)
<
0.0
&&
...
@@ -483,11 +483,11 @@ class Solver {
...
@@ -483,11 +483,11 @@ class Solver {
Result
_dualOptimize
()
{
Result
_dualOptimize
()
{
while
(
_infeasibleRows
.
length
!=
0
)
{
while
(
_infeasibleRows
.
length
!=
0
)
{
Symbol
leaving
=
_infeasibleRows
.
removeLast
();
_
Symbol
leaving
=
_infeasibleRows
.
removeLast
();
Row
row
=
_rows
[
leaving
];
_
Row
row
=
_rows
[
leaving
];
if
(
row
!=
null
&&
row
.
constant
<
0.0
)
{
if
(
row
!=
null
&&
row
.
constant
<
0.0
)
{
Symbol
entering
=
_getDualEnteringSymbolForRow
(
row
);
_
Symbol
entering
=
_getDualEnteringSymbolForRow
(
row
);
if
(
entering
.
type
==
SymbolType
.
invalid
)
{
if
(
entering
.
type
==
SymbolType
.
invalid
)
{
return
Result
.
internalSolverError
;
return
Result
.
internalSolverError
;
...
@@ -503,14 +503,14 @@ class Solver {
...
@@ -503,14 +503,14 @@ class Solver {
return
Result
.
success
;
return
Result
.
success
;
}
}
Symbol
_getDualEnteringSymbolForRow
(
Row
row
)
{
_Symbol
_getDualEnteringSymbolForRow
(
_
Row
row
)
{
Symbol
entering
;
_
Symbol
entering
;
double
ratio
=
double
.
MAX_FINITE
;
double
ratio
=
double
.
MAX_FINITE
;
Map
<
Symbol
,
double
>
rowCells
=
row
.
cells
;
Map
<
_
Symbol
,
double
>
rowCells
=
row
.
cells
;
for
(
Symbol
symbol
in
rowCells
.
keys
)
{
for
(
_
Symbol
symbol
in
rowCells
.
keys
)
{
double
value
=
rowCells
[
symbol
];
double
value
=
rowCells
[
symbol
];
if
(
value
>
0.0
&&
symbol
.
type
!=
SymbolType
.
dummy
)
{
if
(
value
>
0.0
&&
symbol
.
type
!=
SymbolType
.
dummy
)
{
...
@@ -523,22 +523,22 @@ class Solver {
...
@@ -523,22 +523,22 @@ class Solver {
}
}
}
}
return
_elvis
(
entering
,
new
Symbol
(
SymbolType
.
invalid
,
0
));
return
_elvis
(
entering
,
new
_
Symbol
(
SymbolType
.
invalid
,
0
));
}
}
}
}
class
Tag
{
class
_
Tag
{
Symbol
marker
;
_
Symbol
marker
;
Symbol
other
;
_
Symbol
other
;
Tag
(
this
.
marker
,
this
.
other
);
_
Tag
(
this
.
marker
,
this
.
other
);
Tag
.
fromTag
(
Tag
tag
)
_Tag
.
fromTag
(
_
Tag
tag
)
:
this
.
marker
=
tag
.
marker
,
:
this
.
marker
=
tag
.
marker
,
this
.
other
=
tag
.
other
;
this
.
other
=
tag
.
other
;
}
}
class
EditInfo
{
class
_
EditInfo
{
Tag
tag
;
_
Tag
tag
;
Constraint
constraint
;
Constraint
constraint
;
double
constant
;
double
constant
;
}
}
...
...
packages/cassowary/lib/symbol.dart
View file @
7dcd8115
...
@@ -6,9 +6,9 @@ part of cassowary;
...
@@ -6,9 +6,9 @@ part of cassowary;
enum
SymbolType
{
invalid
,
external
,
slack
,
error
,
dummy
,
}
enum
SymbolType
{
invalid
,
external
,
slack
,
error
,
dummy
,
}
class
Symbol
{
class
_
Symbol
{
final
SymbolType
type
;
final
SymbolType
type
;
int
tick
;
int
tick
;
Symbol
(
this
.
type
,
this
.
tick
);
_
Symbol
(
this
.
type
,
this
.
tick
);
}
}
packages/cassowary/lib/term.dart
View file @
7dcd8115
...
@@ -4,7 +4,7 @@
...
@@ -4,7 +4,7 @@
part of
cassowary
;
part of
cassowary
;
class
Term
extends
EquationMember
{
class
Term
extends
_
EquationMember
{
final
Variable
variable
;
final
Variable
variable
;
final
double
coefficient
;
final
double
coefficient
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment