from ortools.sat.python import cp_model
from orseries.array import CpArray
import numpy as np
import itertools

def main():
    # create empty cp model.
    model = cp_model.CpModel()
    # create five integer variables.
    vars = CpArray([
        model.NewIntVar(lb=-10, ub=10, name=f"test_{i}")
        for i in range(5)
    ])

    # all integers will be greater than zero.
    CpArray.AddToModel(model, vars > 0)
    # each variable take different value.
    CpArray.AddToModel(model, [
        x != y for [x,y] in itertools.combinations(vars,2)
    ])

    # define the objective variable.
    model.Minimize(np.sum(vars))

    # solve it
    solver = cp_model.CpSolver()
    status = solver.Solve(model)

    # print solution
    if status == cp_model.OPTIMAL or status == cp_model.FEASIBLE:
        results = CpArray.ToValues(solver, vars)
        print(f"objective value: {solver.ObjectiveValue()}")
        [print(f"variable {v.Name()} = {r}") for v,r in zip(vars, results)]
        return True

    else:
        print('No solution found.')
        return False

if __name__ == '__main__':
    main()

from ortools.sat.python import cp_model
from orseries.array import CpArray
import numpy as np
import itertools

def main():
    # create empty cp model.
    model = cp_model.CpModel()
    # create five integer variables.
    vars = CpArray([
        model.NewIntVar(lb=-10, ub=10, name=f"test_{i}")
        for i in range(5)
    ])

    # all integers will be greater than zero.
    CpArray.AddToModel(model, vars > 0)
    # each variable take different value.
    CpArray.AddToModel(model, [
        x != y for [x,y] in itertools.combinations(vars,2)
    ])

    # define the objective variable.
    model.Minimize(np.sum(vars))

    # solve it
    solver = cp_model.CpSolver()
    status = solver.Solve(model)

    # print solution
    if status == cp_model.OPTIMAL or status == cp_model.FEASIBLE:
        results = CpArray.ToValues(solver, vars)
        print(f"objective value: {solver.ObjectiveValue()}")
        [print(f"variable {v.Name()} = {r}") for v,r in zip(vars, results)]
        return True

    else:
        print('No solution found.')
        return False

if __name__ == '__main__':
    main()

# create empty cp model. # create five integer variables. # all integers will be greater than zero. # each variable take different value. # define the objective variable. # solve it # print solution