2025-05-02 01:00:45 +00:00
|
|
|
/*
|
|
|
|
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"
|
|
|
|
"math"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"gonum.org/v1/gonum/graph"
|
|
|
|
"gonum.org/v1/gonum/graph/encoding"
|
2025-05-02 06:04:10 +00:00
|
|
|
"gonum.org/v1/gonum/graph/multi"
|
2025-05-02 01:00:45 +00:00
|
|
|
"gonum.org/v1/gonum/graph/simple"
|
2025-05-02 05:04:00 +00:00
|
|
|
"gonum.org/v1/gonum/mat"
|
2025-05-02 01:00:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type AbsorbingMarkovChain struct {
|
2025-05-02 06:04:10 +00:00
|
|
|
*multi.WeightedDirectedGraph
|
2025-05-02 01:00:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewAbsorbingMarkovChain() *AbsorbingMarkovChain {
|
2025-05-02 06:04:10 +00:00
|
|
|
return &AbsorbingMarkovChain{WeightedDirectedGraph: multi.NewWeightedDirectedGraph()}
|
2025-05-02 01:00:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *AbsorbingMarkovChain) IsValid() bool {
|
|
|
|
for nodes := g.Nodes(); nodes.Next(); {
|
|
|
|
u := nodes.Node().(*node)
|
2025-05-02 06:04:10 +00:00
|
|
|
if g.outWeightSum(u) != 1 {
|
2025-05-02 01:00:45 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2025-05-02 05:09:19 +00:00
|
|
|
func (g *AbsorbingMarkovChain) outWeightSum(u graph.Node) float64 {
|
2025-05-02 01:00:45 +00:00
|
|
|
sum := 0.0
|
|
|
|
for nodes := g.From(u.ID()); nodes.Next(); {
|
|
|
|
v := nodes.Node()
|
|
|
|
e := g.WeightedEdge(u.ID(), v.ID())
|
|
|
|
if e != nil {
|
2025-05-02 06:04:10 +00:00
|
|
|
sum += e.Weight()
|
2025-05-02 01:00:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sum
|
|
|
|
}
|
|
|
|
|
2025-05-02 05:04:00 +00:00
|
|
|
func (g *AbsorbingMarkovChain) AdjacencyMatrix() mat.Matrix {
|
2025-05-02 06:04:10 +00:00
|
|
|
adj := simple.NewDirectedMatrix(g.Nodes().Len(), 0, 0, 0)
|
2025-05-02 05:04:00 +00:00
|
|
|
for edges := g.WeightedEdges(); edges.Next(); {
|
2025-05-02 06:04:10 +00:00
|
|
|
e := edges.WeightedEdge()
|
|
|
|
if e.From() == e.To() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
adj.SetWeightedEdge(e)
|
2025-05-02 05:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
a := mat.DenseCopyOf(adj.Matrix())
|
|
|
|
|
|
|
|
nodes := adj.Nodes()
|
|
|
|
for i := 0; nodes.Next(); i++ {
|
|
|
|
id := nodes.Node().ID()
|
|
|
|
u := g.Node(id).(*node)
|
2025-05-02 06:04:10 +00:00
|
|
|
|
|
|
|
e := g.WeightedEdge(u.ID(), u.ID())
|
|
|
|
if e != nil {
|
|
|
|
a.Set(i, i, e.Weight())
|
|
|
|
}
|
2025-05-02 05:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2025-05-02 01:00:45 +00:00
|
|
|
func (g *AbsorbingMarkovChain) NewEdge(from, to graph.Node) graph.Edge {
|
2025-05-02 06:04:10 +00:00
|
|
|
e := g.WeightedDirectedGraph.NewWeightedLine(from, to, math.NaN()).(multi.WeightedLine)
|
|
|
|
return &weightedEdge{WeightedLine: e}
|
2025-05-02 01:00:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *AbsorbingMarkovChain) NewNode() graph.Node {
|
|
|
|
return &node{Node: g.WeightedDirectedGraph.NewNode()}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *AbsorbingMarkovChain) SetEdge(e graph.Edge) {
|
2025-05-02 06:04:10 +00:00
|
|
|
g.WeightedDirectedGraph.SetWeightedLine(e.(*weightedEdge))
|
2025-05-02 01:00:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type weightedEdge struct {
|
2025-05-02 06:04:10 +00:00
|
|
|
multi.WeightedLine
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *weightedEdge) ReversedEdge() graph.Edge {
|
|
|
|
revLine := multi.WeightedLine{F: e.T, T: e.F, W: e.W}
|
|
|
|
return &weightedEdge{WeightedLine: revLine}
|
2025-05-02 01:00:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
type node struct {
|
|
|
|
graph.Node
|
|
|
|
dotID string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *node) SetDOTID(id string) {
|
|
|
|
n.dotID = id
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *node) DOTID() string {
|
|
|
|
return n.dotID
|
|
|
|
}
|