示例#1
0
func ExampleSolveInt() {
	dict := new(lp.Dict)
	dict.NonBasic = []int{0, 1}
	dict.Basic = []int{2, 3, 4}
	// max_{x, y >= 0} x + 2 y
	dict.C = []float64{1, 2}
	// subject to
	// -x + y <= 1,     x -  y +  1 >= 0
	// 3x + 2y <= 12, -3x - 2y + 12 >= 0
	// 2x + 3y <= 12, -2x - 3y + 12 >= 0
	dict.A = make([][]float64, 3)
	dict.B = make([]float64, 3)
	dict.A[0], dict.B[0] = []float64{1, -1}, 1
	dict.A[1], dict.B[1] = []float64{-3, -2}, 12
	dict.A[2], dict.B[2] = []float64{-2, -3}, 12

	dict, err := lp.SolveInt(dict)
	if err != nil {
		fmt.Print(err)
		return
	}
	fmt.Printf("%.6g at %.6g\n", dict.Obj(), dict.Soln()[:2])
	// Output:
	// 6 at [2 2]
}
示例#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")
		}
	}
}
示例#3
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")
		}
	}
}
示例#4
0
文件: main.go 项目: farmlab/golp
func solve(dict *lp.Dict, epsInt float64) Solution {
	if !dict.Feas() {
		log.Println("init. dict. not feasible: solve feas. problem")
		// Solve the feasibility problem.
		var infeas bool
		dict, infeas = lp.SolveFeas(dict)
		if infeas {
			// Continuous relaxation is infeasible,
			// therefore integer problem is infeasible.
			fmt.Println("infeasible")
			return Solution{Infeas: true}
		}
	} else {
		log.Println("init. dict. feasible")
	}

	log.Println("solve")
	var unbnd bool
	dict, unbnd = lp.PivotToFinal(dict)
	if unbnd {
		// Relaxation became unbounded.
		log.Println("primal is unbounded")
		return Solution{Unbnd: true}
	}

	for !dict.IsIntEps(epsInt) {
		fmt.Printf("primal objective coeffs: %.4g\n", dict.C)

		// Check if integer solution.
		log.Println("add cutting-plane constraints")
		dict = lp.CutPlane(dict)
		log.Println("feasible?", dict.Feas())

		log.Println("switch to dual")
		dict = dict.Dual()
		log.Println("feasible?", dict.Feas())

		log.Println("solve")
		var unbnd bool
		dict, unbnd = lp.PivotToFinal(dict)
		if unbnd {
			// Dual of relaxation became unbounded.
			log.Println("dual is unbounded (primal infeasible)")
			return Solution{Infeas: true}
		}

		log.Println("feasible?", dict.Feas())

		log.Println("switch to primal")
		dict = dict.Dual()
		log.Println("objective:", dict.Obj())

		//	// Find a feasible solution.
		//	var infeas bool
		//	log.Println("solve feasibility problem")
		//	dict, infeas = lp.SolveFeas(dict)
		//	if infeas {
		//		// Continuous relaxation is infeasible,
		//		// therefore integer problem is infeasible.
		//		log.Println("primal is infeasible")
		//		return Solution{Infeas: true}
		//	}

		//	var unbnd bool
		//	log.Println("solve problem")
		//	dict, unbnd = lp.Solve(dict)
		//	if unbnd {
		//		// Relaxation became unbounded.
		//		log.Println("primal is unbounded")
		//		return Solution{Unbnd: true}
		//	}
	}

	return Solution{Obj: dict.Obj()}
}