Exemplo n.º 1
0
Arquivo: main.go Projeto: 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()}
}
Exemplo n.º 2
0
Arquivo: main.go Projeto: 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)
	}

	dict.Fprint(os.Stdout)
	fmt.Println("feas?", dict.Feas())
	fmt.Println()

	dict = lp.ToFeasDict(dict)
	dict.Fprint(os.Stdout)
	fmt.Println("feas?", dict.Feas())
	fmt.Println()

	// Pivot all the way.
	var iter int
	for {
		piv := lp.NextFeasBland(dict)
		if piv.Unbounded {
			panic("unbounded")
		}
		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())
	}

	result := Solution{dict.Obj()}

	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.Fatalln("fail:", err)
		}
		log.Print("pass")
	}

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