[go: up one dir, main page]

Skip to content

Commit

Permalink
solver, error: remove dependency on 'fmt', and expose error constants
Browse files Browse the repository at this point in the history
Fixes #1.
  • Loading branch information
lithdew committed May 31, 2020
1 parent 1f9f4b9 commit 827f689
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
11 changes: 11 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package casso

import "errors"

var (
ErrBadPriority = errors.New("priority must be non-negative and not required for edit variable")
ErrNotEditVariable = errors.New("symbol is not yet registered as an edit variable")
ErrBadTermInConstraint = errors.New("one of the terms in the constraint references a nil symbol")
ErrBadConstraintMarker = errors.New("symbol is not registered to refer to a constraint")
ErrBadDummyVariable = errors.New("constraint is unsatisfiable: non-zero dummy variable")
)
11 changes: 5 additions & 6 deletions solver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package casso

import (
"errors"
"fmt"
"math"
)

Expand Down Expand Up @@ -63,7 +62,7 @@ func (s *Solver) AddConstraintWithPriority(priority Priority, cell Constraint) (
continue
}
if term.id.Zero() {
return zero, errors.New("symbol referenced in term is nil")
return zero, ErrBadTermInConstraint
}
resolved, exists := s.tabs[term.id]
if !exists {
Expand Down Expand Up @@ -140,7 +139,7 @@ func (s *Solver) AddConstraintWithPriority(priority Priority, cell Constraint) (
func (s *Solver) RemoveConstraint(marker Symbol) error {
tag, exists := s.tags[marker]
if !exists {
return errors.New("tag is unregistered")
return ErrBadConstraintMarker
}

delete(s.tags, tag.marker)
Expand Down Expand Up @@ -223,7 +222,7 @@ func (s *Solver) RemoveConstraint(marker Symbol) error {

func (s *Solver) Edit(id Symbol, priority Priority) error {
if priority < 0 || priority >= Required {
return errors.New("priority must be non-negative and not required for edit variables")
return ErrBadPriority
}
if _, exists := s.edits[id]; exists {
return nil
Expand All @@ -240,7 +239,7 @@ func (s *Solver) Edit(id Symbol, priority Priority) error {
func (s *Solver) Suggest(id Symbol, val float64) error {
edit, ok := s.edits[id]
if !ok {
return fmt.Errorf("symbol id %d is not registered as editable", id)
return ErrNotEditVariable
}

defer s.optimizeDualObjective()
Expand Down Expand Up @@ -332,7 +331,7 @@ func (s *Solver) findSubject(cell Constraint, tag Tag) (Symbol, error) {
}

if !eqz(cell.expr.constant) {
return zero, errors.New("non-zero dummy variable: constraint is unsatisfiable")
return zero, ErrBadDummyVariable
}

return tag.marker, nil
Expand Down

0 comments on commit 827f689

Please sign in to comment.