import gurobipy as gu
import numpy as np


all_rezy = []

def odpad(plan_rezu, delka):
    return delka - np.sum(plan_rezu)


def posli(rez, useky, delka):
    """
    delka = 200
    useky = [80, 50, 30]
    """
    for usek in useky:
        if len(rez) == 0 or rez[-1] >= usek:
            kandidat = rez.copy()
            kandidat.append(usek)
            if odpad(kandidat, delka) >= 0:
                all_rezy.append(kandidat)
                # print(kandidat)
                posli(kandidat, useky, delka)


if __name__ == "__main__":

    delka = 200
    useky = [80, 50, 30]
    potrebujeme = [120, 310, 150]
    posli([], useky, delka)
    print("--------------")
    irange = range(len(useky))
    plan_ji = []
    odpad_j = []
    for rez in all_rezy:
        if np.sum(rez) + useky[-1] >= delka:
            column = [rez.count(useky[i]) for i in irange]
            odpad = delka - np.sum(rez)
            # if odpad <= 10:
            plan_ji.append(column)
            odpad_j.append(odpad)
                # print(rez, np.sum(rez), column, odpad)

    jrange = range(len(odpad_j))
    plan_ij = np.transpose(np.array(plan_ji))
    odpad_j = np.array(odpad_j)
    print(plan_ij)
    print(odpad_j)

    m = gu.Model()
    x_j = [m.addVar(vtype=gu.GRB.INTEGER, lb=0) for j in jrange]

    m.update()
    for i in irange:
        m.addConstr(gu.quicksum(plan_ij[i, j]*x_j[j] for j in jrange) >= potrebujeme[i])

    m.setObjective(gu.quicksum(x_j[j] for j in jrange))

    m.optimize()

    for j in jrange:
        if x_j[j].x >= 0.3:
            print(x_j[j].x, ": ", plan_ij[:, j])
