Skip to content

Commit

Permalink
Update lingo.go
Browse files Browse the repository at this point in the history
  • Loading branch information
TimLai666 committed Nov 1, 2024
1 parent af7da51 commit 3c1ad93
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions lpgen/lingo.go
Original file line number Diff line number Diff line change
@@ -2,18 +2,21 @@ package lpgen

import (
"bufio"
"fmt"
"os"
"regexp"
"strings"

"github.com/HazelnutParadise/insyra"
)

// ParseLingoFile 解析 Lingo 檔案並返回 LPModel
func ParseLingoFile(filePath string) (*LPModel, error) {
// ParseLingoModel_txt parse lingo model from txt file.
// Go to `LINGO > Generate > Display Model` in LINGO to get the model.
func ParseLingoModel_txt(filePath string) *LPModel {
// 讀取文件
file, err := os.Open(filePath)
if err != nil {
return nil, fmt.Errorf("無法開啟檔案: %w", err)
insyra.LogWarning("lpgen.ParseLingoModel_txt: " + err.Error())
return nil
}
defer file.Close()

@@ -88,10 +91,10 @@ func ParseLingoFile(filePath string) (*LPModel, error) {
model.Objective = content
} else if strings.HasPrefix(strings.ToUpper(expr), "@BIN") {
// 處理 Binary 變數宣告
handleVariableDeclarations(expr, "@BIN", &model.BinaryVars)
lingo_handleVariableDeclarations(expr, "@BIN", &model.BinaryVars)
} else if strings.HasPrefix(strings.ToUpper(expr), "@INT") {
// 處理 Integer 變數宣告
handleVariableDeclarations(expr, "@INT", &model.IntegerVars)
lingo_handleVariableDeclarations(expr, "@INT", &model.IntegerVars)
} else if strings.ContainsAny(expr, "<=>=") {
// 處理 Bounds 和 Constraints
content := expr
@@ -104,14 +107,15 @@ func ParseLingoFile(filePath string) (*LPModel, error) {
}

if err := scanner.Err(); err != nil {
return nil, fmt.Errorf("讀取檔案時發生錯誤: %w", err)
insyra.LogWarning("lpgen.ParseLingoModel_txt: " + err.Error())
return nil
}

return model, nil
return model
}

// handleVariableDeclarations 處理變數宣告並將變數名稱添加到相應的列表中
func handleVariableDeclarations(expr string, declarationType string, targetList *[]string) {
func lingo_handleVariableDeclarations(expr string, declarationType string, targetList *[]string) {
// 切分宣告語句並處理每個宣告
declarations := strings.Split(expr, ";")
for _, declaration := range declarations {

0 comments on commit 3c1ad93

Please sign in to comment.