add comments

This commit is contained in:
filifa 2024-05-25 23:32:29 -05:00
parent b1f0d60184
commit 82befabe64
1 changed files with 14 additions and 0 deletions

View File

@ -27,18 +27,22 @@ import (
"gonum.org/v1/gonum/graph/topo"
)
// projectNetwork is a directed graph of activity nodes
type projectNetwork struct {
*simple.DirectedGraph
}
// newProjectNetwork is a constructor for a projectNetwork
func newProjectNetwork() *projectNetwork {
return &projectNetwork{DirectedGraph: simple.NewDirectedGraph()}
}
// NewNode returns an activity node
func (g *projectNetwork) NewNode() graph.Node {
return &activity{Node: g.DirectedGraph.NewNode()}
}
// calculateTimes computes the times for each activity in g
func (g *projectNetwork) calculateTimes() error {
sorted, err := topo.Sort(g)
if err != nil {
@ -56,12 +60,15 @@ func (g *projectNetwork) calculateTimes() error {
return nil
}
// forwardPass computes the early times for each activity
func (g *projectNetwork) forwardPass(toposort []*activity) {
for _, a := range toposort {
g.setEarlyTimes(a)
}
}
// backwardPass computes the late times and slack for each activity using the
// early times
func (g *projectNetwork) backwardPass(toposort []*activity) {
n := len(toposort)
@ -75,6 +82,7 @@ func (g *projectNetwork) backwardPass(toposort []*activity) {
}
}
// setEarlyTimes sets the early times for an activity
func (g *projectNetwork) setEarlyTimes(a *activity) {
es := 0.0
predecessors := g.To(a.ID())
@ -86,6 +94,7 @@ func (g *projectNetwork) setEarlyTimes(a *activity) {
a.earlyFinish = a.earlyStart + a.duration
}
// setLateTimes sets the late times for an activity
func (g *projectNetwork) setLateTimes(a *activity) {
lf := math.Inf(1)
successors := g.From(a.ID())
@ -98,6 +107,7 @@ func (g *projectNetwork) setLateTimes(a *activity) {
a.slack = a.lateFinish - a.earlyFinish
}
// an activity represents a task in the project network
type activity struct {
graph.Node
dotID string
@ -109,6 +119,7 @@ type activity struct {
slack float64
}
// Attributes returns all the activity attributes to include in the DOT file
func (a *activity) Attributes() []encoding.Attribute {
return []encoding.Attribute{
{"DUR", strconv.FormatFloat(a.duration, 'f', 2, 64)},
@ -120,6 +131,7 @@ func (a *activity) Attributes() []encoding.Attribute {
}
}
// SetAttribute permits reading the duration from the DOT file
func (a *activity) SetAttribute(attr encoding.Attribute) error {
var err error
switch attr.Key {
@ -131,10 +143,12 @@ func (a *activity) SetAttribute(attr encoding.Attribute) error {
return err
}
// DOTID returns the DOTID of the activity
func (a *activity) DOTID() string {
return a.dotID
}
// SetDOTID sets the DOT ID of the activity
func (a *activity) SetDOTID(id string) {
a.dotID = id
}