Skip to content

Commit

Permalink
Update lpgen.go
Browse files Browse the repository at this point in the history
  • Loading branch information
TimLai666 committed Oct 17, 2024
1 parent 33dfbac commit 87d4cce
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions lpgen/lpgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,39 +69,49 @@ func (lp *LPModel) AddIntegerVar(varName string) *LPModel {
func (lp *LPModel) GenerateLPFile(filename string) {
file, err := os.Create(filename)
if err != nil {
insyra.LogFatal("lpgen.GenerateLPFile: Failed to create LP file: %v", err)
insyra.LogWarning("lpgen.GenerateLPFile: Failed to create LP file: %v", err)
return
}
defer file.Close()

// 設定最大化或最小化
_, err = file.WriteString(lp.ObjectiveType + "\n")
if err != nil {
insyra.LogFatal("lpgen.GenerateLPFile: Failed to write objective type: %v", err)
insyra.LogWarning("lpgen.GenerateLPFile: Failed to write objective type: %v, returning", err)
return
}
_, err = file.WriteString(" " + "obj: " + lp.Objective + "\n")
if err != nil {
insyra.LogFatal("lpgen.GenerateLPFile: Failed to write objective: %v", err)
insyra.LogWarning("lpgen.GenerateLPFile: Failed to write objective: %v, returning", err)
return
}

// 添加約束條件
_, err = file.WriteString("Subject To\n")
if err != nil {
insyra.LogFatal("lpgen.GenerateLPFile: Failed to write subject to: %v", err)
insyra.LogWarning("lpgen.GenerateLPFile: Failed to write subject to: %v, returning", err)
return
}
for i, constr := range lp.Constraints {
_, err = file.WriteString(" " + fmt.Sprintf("c%d: %s\n", i+1, constr))
if err != nil {
insyra.LogFatal("lpgen.GenerateLPFile: Failed to write constraint: %v", err)
insyra.LogWarning("lpgen.GenerateLPFile: Failed to write constraint: %v, returning", err)
return
}
}

// 添加變數邊界
if len(lp.Bounds) > 0 {
file.WriteString("Bounds\n")
_, err = file.WriteString("Bounds\n")
if err != nil {
insyra.LogWarning("lpgen.GenerateLPFile: Failed to write bounds: %v, returning", err)
return
}
for _, bound := range lp.Bounds {
_, err = file.WriteString(" " + bound + "\n")
if err != nil {
insyra.LogFatal("lpgen.GenerateLPFile: Failed to write bound: %v", err)
insyra.LogWarning("lpgen.GenerateLPFile: Failed to write bound: %v, returning", err)
return
}
}
}
Expand All @@ -112,7 +122,8 @@ func (lp *LPModel) GenerateLPFile(filename string) {
for _, intVar := range lp.IntegerVars {
_, err = file.WriteString(" " + intVar + "\n")
if err != nil {
insyra.LogFatal("lpgen.GenerateLPFile: Failed to write integer var: %v", err)
insyra.LogWarning("lpgen.GenerateLPFile: Failed to write integer var: %v, returning", err)
return
}
}
}
Expand All @@ -123,14 +134,16 @@ func (lp *LPModel) GenerateLPFile(filename string) {
for _, binVar := range lp.BinaryVars {
_, err = file.WriteString(" " + binVar + "\n")
if err != nil {
insyra.LogFatal("lpgen.GenerateLPFile: Failed to write binary var: %v", err)
insyra.LogWarning("lpgen.GenerateLPFile: Failed to write binary var: %v, returning", err)
return
}
}
}

// LP 文件結尾
_, err = file.WriteString("End\n")
if err != nil {
insyra.LogFatal("lpgen.GenerateLPFile: Failed to write end: %v", err)
insyra.LogWarning("lpgen.GenerateLPFile: Failed to write end: %v, returning", err)
return
}
}

0 comments on commit 87d4cce

Please sign in to comment.