:- nodbgcomp.

:- lib(eplex_cplex).

:- [proptest_common].


setup_and_solve(inc, AB, N, Vars, OrderedPairs) :-
	setup_bounds(Vars, N),
	Vars = [X0|_],
	OptG=min(X0),
	lp_setup([],OptG,[solution(no)],H),
	add_ordered(AB, OrderedPairs, H).
setup_and_solve(batch, AB, N, Vars, OrderedPairs) :-
	setup_bounds(Vars, N),
	Vars = [X0|_],
	OptG=min(X0),
	collect_ordered(AB, OrderedPairs, Cstrs),
	normalise_cstrs(Cstrs, NormCstrs, []),
	lp_setup(NormCstrs,OptG,[solution(no)],H),
	lp_solve(H,_C).


setup_bounds([], _N).
setup_bounds([X|Xs], N) :-
	X :: 0..N,
	setup_bounds(Xs, N).

collect_ordered(AB, OrderedPairs, Cstrs) :-
	(
	    foreach(Y1-Y2,OrderedPairs),
	    foreach(Cstr,Cstrs),
	    param(VarArr,AB)
	do
	    Cstr = (Y2>=Y1+AB)
	).

add_ordered(AB, OrderedPairs, Handle) :-
	(
	    foreach(Y1-Y2,OrderedPairs),
	    param(VarArr,AB,Handle)
	do
	    normalise_cstrs([Y2>=Y1+AB], NormCstrs, []),
	    lp_add(Handle, NormCstrs, []),
	    lp_solve(Handle, _Cost)
	).


