import random
from amplpy import AMPL


def make_data(n, prob):
    V = list(range(1, n + 1))
    E = [(i, j) for i in V for j in V if i < j and random.random() < prob]
    return V, E


def load(modfile, V, E, K=None):
    ampl = AMPL()
    ampl.read(modfile)
    ampl.set["V"] = V
    ampl.set["E"] = E
    if K is not None:
        ampl.param["K"] = K
    ampl.option["solver"] = "highs"
    return ampl


random.seed(1)
V, E = make_data(30, .5)
a = load("gpp.mod", V, E); a.solve(verbose=False)
print("gpp   :", a.solve_result, a.get_objective("CutEdges").value())

random.seed(1)
V, E = make_data(40, .5)
a = load("ssp.mod", V, E); a.solve(verbose=False)
print("ssp   :", a.solve_result, a.get_objective("Size").value())

random.seed(1)
V, E = make_data(20, .5)
a = load("gcp.mod", V, E, 10); a.solve(verbose=False)
print("gcp   :", a.solve_result, a.get_objective("NumColors").value())

a = load("gcp_fixed_k.mod", V, E, 7); a.solve(verbose=False)
print("gcp_k :", a.solve_result, a.get_objective("BadEdges").value())
