示例#1
0
文件: main.go 项目: farmlab/golp
func main() {
	var (
		refFile string
		outFile string
	)
	flag.StringVar(&refFile, "ref", "", "File containing desired solution")
	flag.StringVar(&outFile, "out", "", "File to contain output")

	flag.Parse()
	if flag.NArg() != 1 {
		flag.Usage()
		os.Exit(1)
	}
	dictFile := flag.Arg(0)

	// Load input dictionary.
	dict := new(lp.Dict)
	err := Load(lp.ReadDictColoradoFrom, dictFile, dict)
	if err != nil {
		log.Fatal(err)
	}

	// Pivot all the way.
	var iter int
	var unbounded bool
	for {
		piv := lp.NextBland(dict)
		if piv.Unbounded {
			unbounded = true
			fmt.Println("unbounded")
			break
		}
		if piv.Final {
			fmt.Println("final")
			break
		}
		dict = dict.Pivot(piv.Enter, piv.Leave)
		iter++
		fmt.Printf("%4d  f:%10.3e\n", iter, dict.Obj())
	}

	var result Solution
	if unbounded {
		result = Solution{Unbounded: true}
	} else {
		result = Solution{Value: dict.Obj(), Steps: iter}
	}

	if refFile != "" {
		// Load reference output.
		var ref Solution
		if err := Load(ReadSolutionFrom, refFile, &ref); err != nil {
			log.Fatalln("could not load reference:", err)
		}

		// Compare.
		if err := check(result, ref); err != nil {
			log.Println("fail:", err)
		}
		log.Print("pass")
	}

	if outFile != "" {
		// Save dictionary out.
		if err := Save(WriteSolutionTo, outFile, result); err != nil {
			log.Fatalln("could not save solution")
		}
	}
}
示例#2
0
文件: main.go 项目: farmlab/golp
func main() {
	var (
		refFile string
		outFile string
	)
	flag.StringVar(&refFile, "ref", "", "File containing desired solution")
	flag.StringVar(&outFile, "out", "", "File to contain output")

	flag.Parse()
	if flag.NArg() != 1 {
		flag.Usage()
		os.Exit(1)
	}
	dictFile := flag.Arg(0)

	// Load input dictionary.
	dict := new(lp.Dict)
	err := Load(lp.ReadDictColoradoFrom, dictFile, dict)
	if err != nil {
		log.Fatal(err)
	}

	// Make the pivot.
	var soln Solution
	piv := lp.NextBland(dict)
	if piv.Unbounded {
		// The initial dictionary was unbounded.
		soln.Unbounded = true
	} else {
		if piv.Final {
			// The initial dictionary was final.
			soln.Final = true
		} else {
			// Possible to make a pivot.
			soln.Enter = dict.NonBasic[piv.Enter]
			soln.Leave = dict.Basic[piv.Leave]
			soln.Dict = dict.Pivot(piv.Enter, piv.Leave)
		}
	}

	if refFile != "" {
		// Load reference output.
		var ref Summary
		if err := Load(ReadSummaryFrom, refFile, &ref); err != nil {
			log.Fatalln("could not load reference summary:", err)
		}

		// Compare.
		ok := check(soln, ref)
		if !ok {
			log.Fatal("fail")
		}
		log.Print("pass")
	}

	if outFile != "" {
		// Save dictionary out.
		if err := Save(WriteSolutionTo, outFile, soln); err != nil {
			log.Fatalln("could not save solution")
		}
	}
}