split nodes and edges into separate file

This commit is contained in:
filifa 2025-05-04 13:02:57 -04:00
parent c4e90a1170
commit 692b8a581b
2 changed files with 74 additions and 51 deletions

View File

@ -17,12 +17,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package markov package markov
import ( import (
"errors"
"math" "math"
"strconv"
"gonum.org/v1/gonum/graph" "gonum.org/v1/gonum/graph"
"gonum.org/v1/gonum/graph/encoding"
"gonum.org/v1/gonum/graph/multi" "gonum.org/v1/gonum/graph/multi"
"gonum.org/v1/gonum/graph/simple" "gonum.org/v1/gonum/graph/simple"
"gonum.org/v1/gonum/graph/topo" "gonum.org/v1/gonum/graph/topo"
@ -158,51 +155,3 @@ func (g *AbsorbingMarkovChain) NewNode() graph.Node {
func (g *AbsorbingMarkovChain) SetEdge(e graph.Edge) { func (g *AbsorbingMarkovChain) SetEdge(e graph.Edge) {
g.WeightedDirectedGraph.SetWeightedLine(e.(*weightedEdge)) g.WeightedDirectedGraph.SetWeightedLine(e.(*weightedEdge))
} }
// weightedEdge is a DOT-aware multi.WeightedLine. By being a
// multi.WeightedLine, it allows for self-loops, which are important for
// absorbing Markov chains.
// TODO: this is a little confusing, maybe just have checks in the code that
// there's only one line in each edge?
type weightedEdge struct {
multi.WeightedLine
}
// ReversedEdge returns a new weightedEdge with the same weight, but the
// direction reversed. It exists mainly to satisfy the graph.Edge interface.
func (e *weightedEdge) ReversedEdge() graph.Edge {
revLine := multi.WeightedLine{F: e.T, T: e.F, W: e.W}
return &weightedEdge{WeightedLine: revLine}
}
// SetAttribute enables storing the weight read from a DOT file. It errors if
// an attribute is read that can't be stored in a weightedEdge.
func (e *weightedEdge) SetAttribute(attr encoding.Attribute) error {
var err error
switch attr.Key {
case "weight":
e.W, err = strconv.ParseFloat(attr.Value, 64)
default:
err = errors.New("unknown key:" + attr.Key)
}
return err
}
// Node is a DOT-aware graph.Node.
type Node struct {
graph.Node
dotID string
}
// SetDOTID sets the node's DOT ID. It enables storing the node name read from
// a DOT file.
func (n *Node) SetDOTID(id string) {
n.dotID = id
}
// DOTID returns the node's DOT ID.
func (n *Node) DOTID() string {
return n.dotID
}

View File

@ -0,0 +1,74 @@
/*
Copyright © 2025 filifa
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package markov
import (
"errors"
"strconv"
"gonum.org/v1/gonum/graph"
"gonum.org/v1/gonum/graph/encoding"
"gonum.org/v1/gonum/graph/multi"
)
// Node is a DOT-aware graph.Node.
type Node struct {
graph.Node
dotID string
}
// SetDOTID sets the node's DOT ID. It enables storing the node name read from
// a DOT file.
func (n *Node) SetDOTID(id string) {
n.dotID = id
}
// DOTID returns the node's DOT ID.
func (n *Node) DOTID() string {
return n.dotID
}
// weightedEdge is a DOT-aware multi.WeightedLine. By being a
// multi.WeightedLine, it allows for self-loops, which are important for
// absorbing Markov chains.
// TODO: this is a little confusing, maybe just have checks in the code that
// there's only one line in each edge?
type weightedEdge struct {
multi.WeightedLine
}
// ReversedEdge returns a new weightedEdge with the same weight, but the
// direction reversed. It exists mainly to satisfy the graph.Edge interface.
func (e *weightedEdge) ReversedEdge() graph.Edge {
revLine := multi.WeightedLine{F: e.T, T: e.F, W: e.W}
return &weightedEdge{WeightedLine: revLine}
}
// SetAttribute enables storing the weight read from a DOT file. It errors if
// an attribute is read that can't be stored in a weightedEdge.
func (e *weightedEdge) SetAttribute(attr encoding.Attribute) error {
var err error
switch attr.Key {
case "weight":
e.W, err = strconv.ParseFloat(attr.Value, 64)
default:
err = errors.New("unknown key:" + attr.Key)
}
return err
}