def solve(self):
     """
     """
     # Turn off display and heuristics
     grb.setParam('OutputFlag', 0)
     grb.setParam('Heuristics', 0)
     
     # Open log file
     logfile = open(self.tmp+'gurobi.log', 'w')
     
     # Pass data into my callback function
     self.model._lastiter = -grb.GRB.INFINITY
     self.model._lastnode = -grb.GRB.INFINITY
     self.model._logfile = logfile
     self.model._vars = self.model.getVars()
     
     # Solve model and capture solution information
     self.model.optimize(mycallback)
     
     # Close log file
     logfile.close()
     print('')
     print('Optimization complete')
     if self.model.SolCount == 0:
         print('No solution found, optimization status = %d' % self.model.Status)
         sys.exit(0)
     else:
         print('Solution found, objective = %g' % self.model.ObjVal)
         x_optimal = self.x.getAttr("x").tolist()
         z_optimal = self.z.getAttr("x").tolist()
         self.optimal_edges = [e for i,e in enumerate(self.edges) if x_optimal[i]>0.8]
         self.roots = [self.nodes[ind] for i,ind in enumerate(self.rindex) if z_optimal[i]<0.2]
         return    
Пример #2
0
        def __init__(self,
                     mip=True,
                     msg=True,
                     timeLimit=None,
                     epgap=None,
                     **solverParams):
            """
            Initializes the Gurobi solver_type.

            @param mip: if False the solver_type will solve a MIP as an LP
            @param msg: displays information from the solver_type to stdout
            @param timeLimit: sets the maximum time for solution
            @param epgap: sets the integer bound gap
            """
            LpSolver.__init__(self, mip, msg)
            self.timeLimit = timeLimit
            self.epgap = epgap
            self.solveTime = 0.0

            # set the output of gurobi
            if not self.msg:
                gurobipy.setParam("OutputFlag", 0)

            # set the gurobi parameter values
            for key, value in solverParams.items():
                gurobipy.setParam(key, value)
Пример #3
0
def main():
    setParam("LogFile", "/tmp/gurobi.log")
    for g in gen_benchmark_digraphs():
        success, edgelist_per_cycle = get_all_cycles(g, cutoff=10000000)
        if not success:
            continue # too many simple cycles
        solve_cm(g, edgelist_per_cycle)
Пример #4
0
        def __init__(self,
                     mip=True,
                     msg=True,
                     timeLimit=None,
                     epgap=None,
                     **solverParams):
            """
            Initializes the Gurobi solver.

            @param mip: if False the solver will solve a MIP as an LP
            @param msg: displays information from the solver to stdout
            @param timeLimit: sets the maximum time for solution
            @param epgap: sets the integer bound gap
            """
            LpSolver.__init__(self,
                              mip,
                              msg,
                              timeLimit=timeLimit,
                              gapRel=epgap)
            #set the output of gurobi
            if not self.msg:
                gurobipy.setParam("OutputFlag", 0)

            # TODO: this does not follow the solvers interface
            #  it solverParams should go to LpSolver
            #  and have uniform naming
            #  we should still leave this as a possibility
            # set the gurobi parameter values
            for key, value in solverParams.items():
                gurobipy.setParam(key, value)
Пример #5
0
        def __init__(self,
                     mip=True, msg=True, timeLimit=None, epgap=None,
                     gapRel=None, warmStart=False, logPath=None,
                     **solverParams):
            """
            :param bool mip: if False, assume LP even if integer variables
            :param bool msg: if False, no log is shown
            :param float timeLimit: maximum time for solver (in seconds)
            :param float gapRel: relative gap tolerance for the solver to stop (in fraction)
            :param bool warmStart: if True, the solver will use the current value of variables as a start
            :param str logPath: path to the log file
            :param float epgap: deprecated for gapRel
            """
            if epgap is not None:
                warnings.warn("Parameter epgap is being depreciated for gapRel")
                if gapRel is not None:
                    warnings.warn("Parameter gapRel and epgap passed, using gapRel")
                else:
                    gapRel = epgap
            LpSolver.__init__(self, mip=mip, msg=msg, timeLimit=timeLimit, gapRel=gapRel,
                              logPath=logPath, warmStart=warmStart)
            #set the output of gurobi
            if not self.msg:
                gurobipy.setParam("OutputFlag", 0)

            # set the gurobi parameter values
            for key,value in solverParams.items():
                gurobipy.setParam(key, value)
Пример #6
0
def deduce_relevances(features, knowledgebase, regularization=1):
    gb.setParam('OutputFlag', 0)
    m = gb.Model('rar')

    solver_variables = {}
    for feature in features:
        solver_variables[feature] = m.addVar(
            name=feature, vtype=gb.GRB.CONTINUOUS, lb=0)

    vars_average = m.addVar(name='s', vtype=gb.GRB.CONTINUOUS)
    vars_sum = sum(solver_variables.values())

    m.setObjective(
        vars_sum + regularization * _squared_dist(
            solver_variables.values(), vars_average), gb.GRB.MINIMIZE)

    m.addConstr(vars_average == (vars_sum / len(features)))

    for subset in knowledgebase:
        objective_vars = [solver_variables[f] for f in subset["features"]]
        objective_sum = sum(objective_vars)
        m.addConstr(objective_sum >= subset["score"]["relevance"])

    m.optimize()
    max_x = max(1, max([v.x for v in m.getVars() if v.varName in features]))
    return {
        v.varName: v.x / max_x
        for v in m.getVars() if v.varName in features
    }
Пример #7
0
def run_IIS(g, loops, ub):
    from gurobipy import GRB, LinExpr, setParam
    from grb_set_cover import build_ilp
    #setParam("OutputFlag", 1)
    setParam("IISMethod", 0)
    #success, loops = get_all_cycles(g, cutoff=13747) # if all loops are needed
    #assert success, 'Too many cycles'
    loops = sorted(
        loops)  # loops needs to be a list, and sorting for stability
    m, vrs = build_ilp(g, loops)
    a, y = [], []
    for u, v, d in g.edges_iter(data=True):
        a.append(d['weight']), y.append(vrs[u, v])
    lhs = LinExpr(a, y)
    m.addConstr(lhs, GRB.LESS_EQUAL, ub - 1)
    #m.update()
    #m.write('JacobsenInfeas.lp')
    m.computeIIS()
    keep = [
        i for i, cn in enumerate(m.getConstrs()) if cn.getAttr('IISCONSTR')
    ]
    assert keep[-1] == len(loops)  # The last one is the dummy constraint
    keep.pop()
    #print(keep)
    cyc_subset = [loops[i] for i in keep]
    #m, _ = build_ilp(g, cyc_subset)
    #m.update()
    #m.write('JacobsenMinimal.lp')
    _, obj = solve_cm(g, cyc_subset)
    print('Obj:', obj)
Пример #8
0
    def __init__(self,
                 initialSolution,
                 constraints,
                 findConstraints,
                 numberOfCommodities,
                 verbose=False):
        '''
        create the necessary data structure to correctly handle the column generation procedure 
        
        Parameters
        ----------
        initialSolution : list
            list of initial solutions for each commodity (suppose that commodity k as solution initialSolution[k])

        constraints : list of sets

        findConstraints : dictionnary
            links each edge in the graph to the constraint it goes through  

        numberOfCommodities : int        
        '''
        if not verbose:
            gurobipy.setParam("LogToConsole", 0)

        self.model = gurobipy.Model("MasterProblem")
        self.constraints = constraints
        self.findConstraints_edges = findConstraints
        self.commodities = np.arange(numberOfCommodities)
        self.__setup(initialSolution)
        self.indexes = {x: 0 for x in self.commodities}
        self.stats = {"variablesAdded": [0 for x in self.commodities]}
Пример #9
0
def GbpACLP2():
    # Cost Matrix
    Dij = np.random.randint(1, 20, 16)
    Dij = Dij.reshape(4, 4)
    # Minimum Separation Distance
    r = 10
    service_nodes = range(len(Dij))

    #     2. Create Model, Set MIP Focus, Add Variables, & Update Model
    m = gbp.Model(" -- ACLP2 -- ")
    # Set MIP Focus to 2 for optimality
    gbp.setParam('MIPFocus', 2)
    # Add Service Decision Variables
    serv_var = []
    for orig in service_nodes:
        serv_var.append(
            m.addVar(vtype=gbp.GRB.BINARY, ub=1, name='x' + str(orig + 1)))

    # Update Model Variables
    m.update()

    #     4. Set Objective Function --> Maximize sited facilities with no overlap (r)
    m.setObjective(gbp.quicksum(serv_var[dest] for dest in service_nodes),
                   gbp.GRB.MAXIMIZE)

    #    5. Add Constraints
    #Add Adjacency Pairwise Constraints
    for orig in service_nodes:
        for dest in service_nodes:
            if orig != dest and Dij[orig][dest] < r:
                m.addConstr((serv_var[orig] + serv_var[dest] <= 1))
            else:
                pass

    #     6. Optimize and Print Results
    try:
        m.optimize()
    except Exception as e:
        print '   ################################################################'
        print ' < ISSUE : ', e, ' >'
        print '   ################################################################'

    t2 = time.time() - t1
    print '**********************************************************************'
    selected = []
    for v in m.getVars():
        if v.x > 0:
            var = '%s' % v.VarName
            selected.append(v.x)
            print '    |                                                       ', var
    print '    | Selected Facility Locations ------------------------  ^^^^ '
    print '    | Minimum Distance (r) ------------------------------- ', r
    print '    | Service Nodes -------------------------------------- ', len(
        service_nodes)
    print '    | Candidate Facilities Sited without (r) overlap ----- ', len(
        selected)
    print '    | Real Time to Optimize (sec.) ----------------------- ', t2
    print '*****************************************************************************************'
    m.write("path.lp")
 def available(self):
     """True if the solver is available"""
     try:
         gurobipy.setParam("_test", 0)
     except gurobipy.GurobiError as e:
         warnings.warn('GUROBI error: {}.'.format(e))
         return False
     return True
Пример #11
0
def main():
    setParam('LogFile', '/tmp/gurobi.log')
    #setParam('OutputFlag', 0)
    setParam('LogToConsole', 0)
    #
    solve_digraphs_as_rectangular_bipartite()
    real_main(use_min_degree=False)
    real_main(use_min_degree=True)
Пример #12
0
def GbpPdefSum():
    # Distance Matrix --> 20x20
    Cij = np.random.randint(100, 1000, 400)
    Cij = Cij.reshape(20, 20)
    # Service Nodes
    service_nodes = range(len(Cij))

    #     2. Create Model, Set MIP Focus, Add Variables, & Update Model
    mPDPsum = gbp.Model(' -- p-Defense-Sum -- ')
    # Set MIP Focus to 2 for optimality
    gbp.setParam('MIPFocus', 2)

    #     3. Add Variables
    # Add Decision Variables
    # Service Facility IxJ
    serv_var = []
    for dest in service_nodes:
        serv_var.append(
            mPDPsum.addVar(vtype=gbp.GRB.BINARY,
                           ub=1,
                           name='y' + str(dest + 1)))

    # Update Model Variables
    mPDPsum.update()

    #     4. Set Objective --> Maximize Sum of Distance
    mPDPsum.setObjective(
        gbp.quicksum(Cij[orig][dest] * serv_var[orig] for orig in service_nodes
                     for dest in service_nodes), gbp.GRB.MAXIMIZE)

    #     5. Add Constriants
    # Add Facility Constraint --> [p=2]
    mPDPsum.addConstr(
        gbp.quicksum(serv_var[dest] for dest in service_nodes) == 2)

    #     6. Optimize and Print Results
    try:
        mPDPsum.optimize()
    except Exception as e:
        print '   ################################################################'
        print ' < ISSUE : ', e, ' >'
        print '   ################################################################'
    t2 = time.time() - t1
    mPDPsum.write('path.lp')
    print '\n*************************************************************************'
    selected = []
    for v in mPDPsum.getVars():
        if v.x > 0:
            var = '%s' % v.VarName
            selected.append(var)
            print '    |                                            ', var
    print '    | Selected Facility Locations --------------  ^^^^ '
    print '    | Candidate Facilities [p] ----------------- ', len(selected)
    val = mPDPsum.objVal
    print '    | Objective Value -------------------------- ', val
    print '    | Real Time to Optimize (sec.) ------------- ', t2
    print '*************************************************************************'
    print 'p-Defense-Sum Location Problem'
Пример #13
0
def main():
    setParam("LogFile", "/tmp/gurobi.log")
    for g in gen_benchmark_digraphs():
        #if g.graph['name'] == 'Problem 10 (opt=12)':
        #if g.graph['name'] != 'Problem 8 (opt=5)':
        #if g.graph['name'] != 'Subproblem 8 (opt=3)':
        #    continue
        STATISICS.clear()
        # Problem 10
        #-----------
        # Sparse cut
        #g.remove_edge(38, 36)
        #g.remove_edge(39, 34)
        #-----------
        # Peeling off the DAG of the bypasses
        #dag_connecting = [ (11,4), (17,16), (1,20), (26,25), (12,29), (35,34),
        #                   (21,38), (44,43), (30,48), (54,53), (39,57) ]
        #for e in dag_connecting:
        #    g.remove_edge(*e)
        #-----------
        # Breaking the small bypasses
        #bypass_breakers = [ (17,11), (26,20), (35,29), (44,38), (54,48) ] 
        #for e in bypass_breakers:
        #    g.remove_edge(*e)
        #-----------
        # Problem 8
        #g.remove_edge(4,6)
        #g.remove_edge(24,16)
        #g.remove_edge(17,22)
        #g.remove_edge(21,20)
        #g.remove_edge(2,6)
        #g.remove_edge(24,26)
        #iteratively_remove_runs_and_bypasses(g)
        #g.remove_edge(2,17)
        #g.remove_edge(8,27)
        #g.remove_edge(27,7)
        #g.remove_edge(8,14)  # These latter two are insane but still OK
        #g.remove_edge(18,21)
        #plot(g, prog='sfdp')
        #-----
        #from utils import deserialize
        #g = deserialize('data/JacobsenILOSimpBounds_as_DAG.pkl.gz')
        #orig_input = deserialize('data/JacobsenILOSimpBounds_as_DAG.pkl.gz')
        # And toggle comment on the # <- lines !!!
        #-----
        orig_input = deepcopy(g) # <-
        simplify(g)
        print('Info on the original input:')
        print(orig_input.graph['name']) # <-
        print('Nodes:', orig_input.number_of_nodes())
        print('Edges:', orig_input.number_of_edges())
        print('Loops:', len(list(nx.simple_cycles(orig_input)))) # <-
        if 'max cycles' in STATISICS:
            print('Loop budget:', STATISICS['max cycles'])
            print('Cutoff:', STATISICS['max cutoff'])
        plot(orig_input, prog='sfdp')
 def solve(self):
     """
     """
     grb.setParam('OutputFlag', 0)
     grb.setParam('Heuristics', 0)
     self.model.optimize()
     self.u_opt = np.array([int(self.u[i].getAttr("x")>0.5) \
                            for i in self.iter_EV])
     self.W_opt = np.zeros(shape=(len(self.s),self.T),dtype=int)
     self.P_opt = np.zeros(shape=(len(self.s),self.T))
     for i in self.iter_EV:
         for j in self.iter_trem:
             self.W_opt[i,j] = int(self.W[(i,j)].getAttr("x")>0.5)
             self.P_opt[i,j] = self.P[(i,j)].getAttr("x")
     return
Пример #15
0
    def __init__(self, mip_gap: float = 0.1, timeout: Optional[int] = None, enable_logging: bool = False) -> None:
        """ Initialize the solver

        Parameters
        ----------
        mip_gap:
            The gap used to determine when a solution to the mip has been found
        timeout:
            The time allowed for solving
        enable_logging:
            Whether gurobi logs should be shown
        """
        self.mip_gap = mip_gap
        self.timeout = timeout
        gurobipy.setParam("OutputFlag", enable_logging)
Пример #16
0
def main():

    if len(sys.argv) < 2:
        print('No instance specified!', file=sys.stderr)
        sys.exit(1)

    input_file = sys.argv[1]

    if len(sys.argv) < 3:
        configuration = 'ppa_star_hplus'
    else:
        configuration = sys.argv[2]

    constrained = True
    if len(sys.argv) < 4:
        total_amount_of_fluid = 10
    elif sys.argv[3] == 'unconstrained':
        constrained = False
        total_amount_of_fluid = 0
    else:
        total_amount_of_fluid = int(sys.argv[3])

    if configuration not in solver_support.configs:
        print('Specified configuration name {0} not recognized'.format(
            configuration))
        print('Available configurations are:')
        for cfg in configs:
            print('\t{0}'.format(cfg))
        sys.exit(1)
    print('Configuration {0} is active'.format(configuration))

    #if len(sys.argv) < 4 :
    #	output_redirect = sys.stdout
    #else :
    #	output_redirect = open( sys.argv[3], 'w' )

    # set Gurobi parameters
    setParam('LogFile', '')
    setParam('LogToConsole', 0)
    setParam('OutputFlag', 0)
    logging.basicConfig(
        level='INFO',
        #level='DEBUG',
        format='%(asctime)s %(levelname)-8s %(message)s',
        stream=sys.stdout)

    task = hbw3.create_prob_from_file(input_file, total_amount_of_fluid,
                                      constrained)
    task.s0.check_valid()
    logging.info('Task initial state valid? %s' % task.s0.valid)

    if len(sys.argv) > 4:
        task.validate(sys.argv[4:])
        sys.exit(0)

    solver_support.solve(configuration, task)
Пример #17
0
def main():

    if len(sys.argv) < 2:
        print('Missing argument: input problem!', file=sys.stderr)
        sys.exit(1)

    instance = sys.argv[1]

    if not os.path.exists(instance):
        print('Could not find {0}'.format(instance), file=sys.stderr)
        sys.exit(1)

    if len(sys.argv) < 3:
        configuration = 'ppa_star_hplus'
    else:
        configuration = sys.argv[2]

    if configuration not in solver_support.configs:
        print('Specified configuration name {0} not recognized'.format(
            configuration))
        print('Available configurations are:')
        for cfg in solver_support.configs:
            print('\t{0}'.format(cfg))
        sys.exit(1)
    print('Configuration {0} is active'.format(configuration))

    if len(sys.argv) < 4:
        output_redirect = sys.stdout
    else:
        output_redirect = open(sys.argv[3], 'w')

    # set Gurobi parameters
    setParam('LogFile', '')
    setParam('LogToConsole', 0)
    setParam('OutputFlag', 0)
    logging.basicConfig(
        level='INFO',
        #level='DEBUG',
        format='%(asctime)s %(levelname)-8s %(message)s',
        stream=output_redirect)

    task = parse_instance(instance)
    solver_support.solve(configuration, task)
def GuTransProb(Cij, Si, Dj):
    t1 = time.time()
    #  Data
    
    Cij = Cij.reshape(matrix_rows,matrix_cols)
    Si = Si.reshape(matrix_rows,1)
    Dj = Dj.reshape(matrix_cols,1)
    
    # Indices & Variable Names
    supply_nodes = len(Cij)
    demand_nodes = len(Cij[0])
    supply_nodes_range = range(len(Cij))
    demand_nodes_range = range(len(Cij[0]))
    all_nodes_len = len(Cij) * len(Cij[0])
    ALL_nodes_range = range(all_nodes_len)
    
    # Create Model, Set MIP Focus, Add Variables, & Update Model
    m = gbp.Model(' -- The Transportation Problem -- ')
    # Set MIP Focus to 2 for optimality
    gbp.setParam('MIPFocus', 2)
    descision_var = []
    for orig in supply_nodes_range:
        descision_var.append([])
        for dest in demand_nodes_range:
            descision_var[orig].append(m.addVar(vtype=gbp.GRB.CONTINUOUS, 
                                            obj=Cij[orig][dest], 
                                            name='x'+str(orig+10001)+'_'+str(dest+10001)))
    # Update Model Variables
    m.update()       
    
    #  Set Objective Function
    m.setObjective(gbp.quicksum(Cij[orig][dest]*descision_var[orig][dest] 
                            for orig in supply_nodes_range for dest in demand_nodes_range), 
                            gbp.GRB.MINIMIZE)
                            
    # Add Supply Constraints
    for orig in supply_nodes_range:
        m.addConstr(gbp.quicksum(descision_var[orig][dest] 
                            for dest in demand_nodes_range) - Si[orig] <= 0)
    # Add Demand Constraints
    for orig in demand_nodes_range:  
        m.addConstr(gbp.quicksum(descision_var[dest][orig] 
                            for dest in supply_nodes_range) - Dj[orig] >= 0)
    
    #  Optimize and Print Results
    m.optimize()
    m.write('path.lp')
    t2 = time.time()-t1
    print '******************************************************************************'
    print '| From SUPPLY Facility to DEMAND Facility x(Si)_(Dj) shipping # of units  '
    print '| ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓'
    print '|'
    selected = {}
    Closed = []
    for v in m.getVars():
        var = '%s' % v.VarName
        if v.x > 0:
            units = '%i' % v.x
            selected[var] = units
            print '|  Supply Facility #', var[1:6], 'is shipping', units, \
                                                'units to Demand Facility #', var[-5:]
        else:
            Closed.append([var[1:6], var[-5:]])
    print '******************************************************************************'
    print '    | Objective Value --------------------- ', int(m.objVal)
    print '    | Supply Facilities ------------------- ', len(Si)
    print '    | Total Supply Units ------------------ ', Si.sum()
    print '    | Demand Facilites -------------------- ', len(Dj)
    print '    | Total Demand Units ------------------ ', Dj.sum()
    print '    | Total Potential Combinations -------- ', len(Si)*len(Dj)
    print '    | Actual Combinations  ---------------- ', len(selected)
    print '    | Real Time to Optimize (sec.) -------- ', t2
    print '******************************************************************************'
    print '  --  The Transportation Simplex with Gurobi --'
    print '  --  James Gaboardi, 2016  --'
Пример #19
0
class GUROBI(LpSolver):
    """
    The Gurobi LP/MIP solver (via its python interface)

    The Gurobi variables are available (after a solve) in var.solverVar
    Constraints in constraint.solverConstraint
    and the Model is in prob.solverModel
    """
    name = 'GUROBI'

    try:
        sys.path.append(gurobi_path)
        # to import the name into the module scope
        global gurobipy
        import gurobipy
        gurobipy.setParam("_test", 0)
    except:  # FIXME: Bug because gurobi returns
        #  a gurobi exception on failed imports
        def available(self):
            """True if the solver is available"""
            return False

        def actualSolve(self, lp, callback=None):
            """Solve a well formulated lp problem"""
            raise PulpSolverError("GUROBI: Not Available")
    else:

        def __init__(self,
                     mip=True,
                     msg=True,
                     timeLimit=None,
                     epgap=None,
                     **solverParams):
            """
            Initializes the Gurobi solver.

            @param mip: if False the solver will solve a MIP as an LP
            @param msg: displays information from the solver to stdout
            @param timeLimit: sets the maximum time for solution
            @param epgap: sets the integer bound gap
            """
            LpSolver.__init__(self,
                              mip,
                              msg,
                              timeLimit=timeLimit,
                              gapRel=epgap)
            #set the output of gurobi
            if not self.msg:
                gurobipy.setParam("OutputFlag", 0)

            # TODO: this does not follow the solvers interface
            #  it solverParams should go to LpSolver
            #  and have uniform naming
            #  we should still leave this as a possibility
            # set the gurobi parameter values
            for key, value in solverParams.items():
                gurobipy.setParam(key, value)

        def findSolutionValues(self, lp):
            model = lp.solverModel
            solutionStatus = model.Status
            GRB = gurobipy.GRB
            # TODO: check status for Integer Feasible
            gurobiLpStatus = {
                GRB.OPTIMAL: constants.LpStatusOptimal,
                GRB.INFEASIBLE: constants.LpStatusInfeasible,
                GRB.INF_OR_UNBD: constants.LpStatusInfeasible,
                GRB.UNBOUNDED: constants.LpStatusUnbounded,
                GRB.ITERATION_LIMIT: constants.LpStatusNotSolved,
                GRB.NODE_LIMIT: constants.LpStatusNotSolved,
                GRB.TIME_LIMIT: constants.LpStatusNotSolved,
                GRB.SOLUTION_LIMIT: constants.LpStatusNotSolved,
                GRB.INTERRUPTED: constants.LpStatusNotSolved,
                GRB.NUMERIC: constants.LpStatusNotSolved,
            }
            if self.msg:
                print("Gurobi status=", solutionStatus)
            lp.resolveOK = True
            for var in lp.variables():
                var.isModified = False
            status = gurobiLpStatus.get(solutionStatus,
                                        constants.LpStatusUndefined)
            lp.assignStatus(status)
            if status != constants.LpStatusOptimal:
                return status

            #populate pulp solution values
            for var, value in zip(lp.variables(),
                                  model.getAttr(GRB.Attr.X, model.getVars())):
                var.varValue = value

            # populate pulp constraints slack
            for constr, value in zip(
                    lp.constraints.values(),
                    model.getAttr(GRB.Attr.Slack, model.getConstrs())):
                constr.slack = value

            if not model.getAttr(GRB.Attr.IsMIP):
                for var, value in zip(
                        lp.variables(),
                        model.getAttr(GRB.Attr.RC, model.getVars())):
                    var.dj = value

                #put pi and slack variables against the constraints
                for constr, value in zip(
                        lp.constraints.values(),
                        model.getAttr(GRB.Attr.Pi, model.getConstrs())):
                    constr.pi = value

            return status

        def available(self):
            """True if the solver is available"""
            return True

        def callSolver(self, lp, callback=None):
            """Solves the problem with gurobi
            """
            #solve the problem
            self.solveTime = -clock()
            lp.solverModel.optimize(callback=callback)
            self.solveTime += clock()

        def buildSolverModel(self, lp):
            """
            Takes the pulp lp model and translates it into a gurobi model
            """
            log.debug("create the gurobi model")
            lp.solverModel = gurobipy.Model(lp.name)
            log.debug("set the sense of the problem")
            if lp.sense == constants.LpMaximize:
                lp.solverModel.setAttr("ModelSense", -1)
            if self.timeLimit:
                lp.solverModel.setParam("TimeLimit", self.timeLimit)
            gapRel = self.optionsDict.get('gapRel')
            logPath = self.optionsDict.get('logPath')
            if gapRel:
                lp.solverModel.setParam("MIPGap", gapRel)
            if logPath:
                lp.solverModel.setParam("LogFile", logPath)

            log.debug("add the variables to the problem")
            for var in lp.variables():
                lowBound = var.lowBound
                if lowBound is None:
                    lowBound = -gurobipy.GRB.INFINITY
                upBound = var.upBound
                if upBound is None:
                    upBound = gurobipy.GRB.INFINITY
                obj = lp.objective.get(var, 0.0)
                varType = gurobipy.GRB.CONTINUOUS
                if var.cat == constants.LpInteger and self.mip:
                    varType = gurobipy.GRB.INTEGER
                var.solverVar = lp.solverModel.addVar(lowBound,
                                                      upBound,
                                                      vtype=varType,
                                                      obj=obj,
                                                      name=var.name)
            lp.solverModel.update()
            log.debug("add the Constraints to the problem")
            for name, constraint in lp.constraints.items():
                #build the expression
                expr = gurobipy.LinExpr(
                    list(constraint.values()),
                    [v.solverVar for v in constraint.keys()])
                if constraint.sense == constants.LpConstraintLE:
                    relation = gurobipy.GRB.LESS_EQUAL
                elif constraint.sense == constants.LpConstraintGE:
                    relation = gurobipy.GRB.GREATER_EQUAL
                elif constraint.sense == constants.LpConstraintEQ:
                    relation = gurobipy.GRB.EQUAL
                else:
                    raise PulpSolverError(
                        'Detected an invalid constraint type')
                constraint.solverConstraint = lp.solverModel.addConstr(
                    expr, relation, -constraint.constant, name)
            lp.solverModel.update()

        def actualSolve(self, lp, callback=None):
            """
            Solve a well formulated lp problem

            creates a gurobi model, variables and constraints and attaches
            them to the lp model which it then solves
            """
            self.buildSolverModel(lp)
            #set the initial solution
            log.debug("Solve the Model using gurobi")
            self.callSolver(lp, callback=callback)
            #get the solution information
            solutionStatus = self.findSolutionValues(lp)
            for var in lp.variables():
                var.modified = False
            for constraint in lp.constraints.values():
                constraint.modified = False
            return solutionStatus

        def actualResolve(self, lp, callback=None):
            """
            Solve a well formulated lp problem

            uses the old solver and modifies the rhs of the modified constraints
            """
            log.debug("Resolve the Model using gurobi")
            for constraint in lp.constraints.values():
                if constraint.modified:
                    constraint.solverConstraint.setAttr(
                        gurobipy.GRB.Attr.RHS, -constraint.constant)
            lp.solverModel.update()
            self.callSolver(lp, callback=callback)
            #get the solution information
            solutionStatus = self.findSolutionValues(lp)
            for var in lp.variables():
                var.modified = False
            for constraint in lp.constraints.values():
                constraint.modified = False
            return solutionStatus
Пример #20
0
       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

'''

import tempfile
from collections import OrderedDict
from .solver import Solver, Solution, Status, VarType
from gurobipy import setParam, Model as GurobiModel, GRB, quicksum, read

setParam("OutputFlag", 0)

status_mapping = {GRB.OPTIMAL: Status.OPTIMAL,
                  GRB.UNBOUNDED: Status.UNBOUNDED,
                  GRB.INFEASIBLE: Status.INFEASIBLE}


class GurobiSolver(Solver):
    """ Implements the solver interface using gurobipy. """

    def __init__(self):
        Solver.__init__(self)
        self.problem = GurobiModel()
        

    def __getstate__(self):
import gurobipy as gp
from gurobipy import GRB
from math import ceil

debug = False
# debug = True

try:

    # Create a new model
    m = gp.Model("level2")

    # This is required to allow for multiple train type.  If the default value (-1) is used, we get error:
    # Error code 10020: Objective Q not PSD (diagonal adjustment of 2.3e+02 would be required). Set NonConvex parameter to 2 to solve model.
    # This is not in the base level 2 model.
    gp.setParam("NonConvex", 2)

    #### Initialize Data: ####
    '''
    ---- Init data for locomotives ----
    loco_types: The type of locomotive.  Corresponds to a train type
    loco_Cfix: Fixed cost of a loco. i.e. cost to purchase one loco
    loco_Ckm: Cost of loco travelling 1km
    loco_speed: Average speed of locomotive

    e.g. locomotive 'a' costs 1000 to purchase and 2 per km to run, avg speed of 30km/h
    Note: I have set the fixed cost to zero since the locos are already owned, and this does not affect daily
    operation as much.  The millions of dollars in fixed cost overshadows the cost of operating.
    '''
    loco_types, loco_Cfix, loco_Ckm, loco_speed = gp.multidict({
        'a': [1000, 2, 40],
Пример #22
0
def find_path_aux(g, start, end, edge_budget, node_budget, time_limit,
                  verbose=True):
    # Problem parameters
    N = g.number_of_nodes()
    S = start
    T = end
    EB = edge_budget
    NB = node_budget

    grb.setParam('OutputFlag', verbose)

    if verbose:
        print '(N, S, T, EB, NB) =', ','.join(map(str, [N, S, T, EB, NB]))
    # Model init
    model = grb.Model("Orienteering")
    # Weights and variables
    w = dict()
    t = dict()
    m = dict()
    x = dict()
    u = dict()
    # Read node weights
    for i, d in g.nodes(data=True):
        u[i] = model.addVar(vtype=grb.GRB.INTEGER, name='u_'+str(i))
        try:
            w[i] = d['w']
        except KeyError:
            w[i] = 0
        try:
            m[i] = d['m']
        except KeyError:
            m[i] = 0
    # Read edge weights
    if g.is_directed():
        dg = g
    else:
        dg = g.to_directed()
    for i, j, d in dg.edges(data=True):
        try:
            t[i,j] = d['t']
        except KeyError:
            t[i,j] = 0
        obj = w[j]
        if i == S:
            obj = w[i] + w[j]
        name = 'x_' + str(i) + '_' + str(j)
        x[i,j] = model.addVar(obj=obj, vtype=grb.GRB.BINARY, name=name)

    model.modelSense = grb.GRB.MAXIMIZE
    model.update()

    RN = range(1, N+1)

    # Path starts at S and ends at T
    model.addConstr(grb.quicksum(x[S,j] for j in RN if (S, j) in x) == 1)
    model.addConstr(grb.quicksum(x[i,T] for i in RN if (i, T) in x) == 1)
    # No edges "entering" S or "leaving" T
    for i in RN:
        if (i, S) in x:
            model.addConstr(x[i,S] == 0)
        if (T, i) in x:
            model.addConstr(x[T,i] == 0)
    # "Flow conservation" constraints
    for k in RN:
        if k != S and k != T:
            model.addConstr(
                grb.quicksum(x[i,k] for i in RN if (i, k) in x) <= 1)
            model.addConstr(
                grb.quicksum(x[k,j] for j in RN if (k, j) in x) <= 1)
            model.addConstr(
                grb.quicksum(x[i,k] for i in RN if i != T and (i, k) in x) +
                grb.quicksum(-x[k,j] for j in RN if j != S and (k, j) in x)
                == 0)
    # Edge budget constraint
    model.addConstr(grb.quicksum(t[i,j]*x[i,j] for (i, j) in x) <= EB)
    # Node budget constraint
    model.addConstr(grb.quicksum(m[j]*x[i,j] for (i, j) in x) <= NB)
    # Subtour elimination constraints (1)
    for k in RN:
        if k != S:
            model.addConstr(u[k] >= 2)
            model.addConstr(u[k] <= N)
    model.addConstr(u[S] == 1)
    # Subtour elimination constraints (2)
    for (i, j) in x:
        if i != S and j != S:
            model.addConstr(u[i] - u[j] + (N-1)*x[i,j] <= N-2)

    model.update()

    # Solve
    model.setParam('TimeLimit', time_limit)
    model.optimize()

    # Decode solution
    if (model.status != grb.GRB.status.OPTIMAL and
        model.status != grb.GRB.status.TIME_LIMIT and
        model.status != grb.GRB.status.INTERRUPTED):
        obj = None
        path = None
    else:
        obj = model.objVal
        nxt = dict()
        for i in RN:
            for j in RN:
                if (i, j) in x and x[i,j].x == 1:
                    nxt[i] = j
        path = [S]
        v = S
        while v != T:
            v = nxt[v]
            path.append(v)
    return (model.status, obj, path)
Пример #23
0
def GbpStPrimLP():
    #  Constants
    Aij = np.random.randint(5, 50, 25)
    Aij = Aij.reshape(5, 5)
    AijSum = np.sum(Aij)
    Cj = np.random.randint(10, 20, 5)
    CjSum = np.sum(Cj)
    Bi = np.random.randint(10, 20, 5)
    BiSum = np.sum(Bi)

    # Matrix Shape
    rows = range(len(Aij))
    cols = range(len(Aij[0]))

    # Instantiate Model
    mPrimal_Standard_GUROBI = gbp.Model(" -- Standard Primal Linear Programming Problem -- ")

    # Set Focus to Optimality
    gbp.setParam("MIPFocus", 2)

    # Decision Variables
    desc_var = []
    for dest in cols:
        desc_var.append([])
        desc_var[dest].append(mPrimal_Standard_GUROBI.addVar(vtype=gbp.GRB.CONTINUOUS, name="y" + str(dest + 1)))

    # Surplus Variables
    surp_var = []
    for orig in rows:
        surp_var.append([])
        surp_var[orig].append(mPrimal_Standard_GUROBI.addVar(vtype=gbp.GRB.CONTINUOUS, name="s" + str(orig + 1)))

    # Update Model
    mPrimal_Standard_GUROBI.update()

    # Objective Function
    mPrimal_Standard_GUROBI.setObjective(gbp.quicksum(Cj[dest] * desc_var[dest][0] for dest in cols), gbp.GRB.MINIMIZE)

    # Constraints
    for orig in rows:
        mPrimal_Standard_GUROBI.addConstr(
            gbp.quicksum(Aij[orig][dest] * desc_var[dest][0] for dest in cols) - surp_var[orig][0] - Bi[orig] == 0
        )

    # Optimize
    try:
        mPrimal_Standard_GUROBI.optimize()
    except Exception as e:
        print "   ################################################################"
        print " < ISSUE : ", e, " >"
        print "   ################################################################"

    # Write LP file
    mPrimal_Standard_GUROBI.write("LP.lp")
    print "\n*************************************************************************"
    print "    |   Decision Variables"
    for v in mPrimal_Standard_GUROBI.getVars():
        print "    |  ", v.VarName, "=", v.x
    print "*************************************************************************"
    val = mPrimal_Standard_GUROBI.objVal
    print "    |   Objective Value ------------------ ", val
    print "    |   Aij Sum -------------------------- ", AijSum
    print "    |   Cj Sum --------------------------- ", CjSum
    print "    |   Bi Sum --------------------------- ", BiSum
    print "    |   Matrix Dimensions ---------------- ", Aij.shape
    print "    |   Date/Time ------------------------ ", dt.datetime.now()
    print "*************************************************************************"
    print "-- Gurobi Standard Primal Linear Programming Problem --"
Пример #24
0
import os

import gurobipy as grb
from gurobipy import GRB

grb.setParam('OutputFlag', 0)
grb.setParam('LogFile', os.getenv("HOME") + '/gurobi.log')
try:
    os.unlink('gurobi.log')
except:
    pass


def generate_basic_lp(model, add_to_existing=False):
    """docstring for generate_basic_lp"""
    # we first initialise the linear program
    if hasattr(model, 'lp') and add_to_existing:
        pass
    else:
        model.lp = grb.Model('LP_' + model.name)

    model.lp.modelSense = GRB.MAXIMIZE

    # reaction fluxes are the variables in the linear model
    # we take the objective coefficient from the corresponding reaction property,
    # which generally be non-zero for only the biomass reaction
    for reaction in model.reactions():

        if reaction.reversible:
            var = model.lp.addVar(reaction.lower_bound,
        cmpl = model.cbGet(GRB.Callback.BARRIER_COMPL)
        print('%d %g %g %g %g %g' %
              (itcnt, primobj, dualobj, priminf, dualinf, cmpl))
    elif where == GRB.Callback.MESSAGE:
        # Message callback
        msg = model.cbGet(GRB.Callback.MSG_STRING)
        model._logfile.write(msg)


if len(sys.argv) < 2:
    print('Usage: callback.py filename')
    quit()

# Turn off display and heuristics

gp.setParam('OutputFlag', 0)
gp.setParam('Heuristics', 0)

# Read model from file

model = gp.read(sys.argv[1])

# Open log file

logfile = open('cb.log', 'w')

# Pass data into my callback function

model._lastiter = -GRB.INFINITY
model._lastnode = -GRB.INFINITY
model._logfile = logfile
Пример #26
0
class RelevanceOptimizer:

    gb.setParam('OutputFlag', 0)

    def _calculate_single_feature_relevance(self,
                                            columns,
                                            relevances,
                                            cost_matrix=None):
        # New approach, keep class relevances separate
        if cost_matrix is not None:
            for index, df in relevances.items():
                df.index = [index]
            dataframe = pd.concat(relevances.values)
            classes = {col: dataframe[col] for col in dataframe.columns}
        else:
            classes = {'-1': relevances}

        single_relevances = defaultdict(int)
        for class_col, class_scores in classes.items():
            # print(class_col)
            m = gb.Model('rar')
            n = len(columns)
            max_score = max(class_scores)

            solver_variables = {}
            for col in columns:
                solver_variables[col] = m.addVar(name='x_' + col,
                                                 vtype=gb.GRB.CONTINUOUS,
                                                 lb=0,
                                                 ub=max_score)

            vars_average = m.addVar(name='s', vtype=gb.GRB.CONTINUOUS)
            vars_sum = sum(solver_variables.values())

            m.addConstr(vars_average == (vars_sum / n))
            m.setObjective(
                vars_sum +
                self.__squared_dist(solver_variables.values(), vars_average),
                gb.GRB.MINIMIZE)
            m.update()

            for (subset, score) in class_scores.iteritems():
                objective_vars = map(lambda col: solver_variables[col], subset)
                objective_sum = sum(objective_vars)
                m.addConstr(objective_sum >= score)

            m.optimize()

            var_max = max(map(lambda v: v.x, solver_variables.values()))
            class_result = {
                k: (v.x / var_max)
                for k, v in solver_variables.items()
            }
            for k, v in solver_variables.items():
                single_relevances[k] += (v.x / var_max) * (
                    1 if cost_matrix is None else cost_matrix[class_col][0])
                # print(str(v.x / var_max) + ' weighted to ' +
                # str((v.x / var_max) * (1 if cost_matrix is None else cost_matrix[class_col][0])))

        for k in single_relevances.keys():
            single_relevances[k] /= (1 if cost_matrix is None else
                                     cost_matrix.iloc[0].sum())

        return single_relevances

    def __squared_dist(self, variables, mean):
        return sum(map(lambda v: (v - mean) * (v - mean), variables))
def GbpApC():
    #           1. Create Data
    # Distance Matrix
    Dij = np.random.randint(100, 1000, 25)
    Dij = Dij.reshape(5, 5)
    rows, cols = Dij.shape
    client_nodes = range(rows)
    service_nodes = range(cols)

    #     2. Create Model, Set MIP Focus, Add Variables, & Update Model
    mPACP = gbp.Model(" -- P-Anti-Center -- ")
    # Set MIP Focus to 2 for optimality
    gbp.setParam('MIPFocus', 2)
    # Add Client Decision Variables
    client_var = []
    for orig in client_nodes:
        client_var.append([])
        for dest in service_nodes:
            client_var[orig].append(
                mPACP.addVar(vtype=gbp.GRB.BINARY,
                             obj=Dij[orig][dest],
                             name='x' + str(orig + 1) + '_' + str(dest + 1)))
    # Add Service Decision Variables
    serv_var = []
    for dest in service_nodes:
        serv_var.append([])
        serv_var[dest].append(
            mPACP.addVar(vtype=gbp.GRB.BINARY, name='y' + str(dest + 1)))
    # Add Minimized Maximum Average Variable
    W = mPACP.addVar(vtype=gbp.GRB.CONTINUOUS, name='W')
    # Update Model Variables
    mPACP.update()

    #     3. Set Objective Function
    mPACP.setObjective(W, gbp.GRB.MAXIMIZE)

    #     4. Add Constraints
    #Add Assignment Constraints
    for orig in client_nodes:
        mPACP.addConstr(
            gbp.quicksum(client_var[orig][dest]
                         for dest in service_nodes) == 1)
    # Add Opening Constraints
    for dest in service_nodes:
        for orig in client_nodes:
            mPACP.addConstr((serv_var[dest] - client_var[orig][dest] >= 0))
    # Add Facility Constraint -->  [p=2]
    mPACP.addConstr(
        gbp.quicksum(serv_var[dest][0] for dest in service_nodes) == 2)
    # Add Maximized Maximum Time Constraint
    for orig in client_nodes:
        mPACP.addConstr(
            gbp.quicksum(Dij[orig][dest] * client_var[orig][dest]
                         for dest in service_nodes) - W >= 0)

    #      5. Optimize and Print Results
    try:
        mPACP.optimize()
    except Exception as e:
        print '   ################################################################'
        print ' < ISSUE : ', e, ' >'
        print '   ################################################################'
    mPACP.write('path.lp')
    print '\n*************************************************************************'
    selected = []
    for v in mPACP.getVars():
        if 'x' in v.VarName:
            pass
        elif 'W' in v.VarName:
            pass
        elif v.x > 0:
            var = '%s' % v.VarName
            selected.append(var)
            print '    |                                            ', var
    print '    | Selected Facility Locations --------------  ^^^^ '
    print '    | Candidate Facilities [p] ----------------- ', len(selected)
    val = mPACP.objVal
    print '    | Objective Value(miles) ------------------- ', val
    print '*************************************************************************'
Пример #28
0
def algorithm_route_vi(data_with_route_variable, bool_display=True, bool_display_iteration=True):
    gp.setParam('OutputFlag', 0)

    link_node_pair = data_with_route_variable['link_node_pair']
    link_capacity = data_with_route_variable['link_capacity']
    link_free = data_with_route_variable['link_free_flow_time']
    od_node_pair = data_with_route_variable['od_node_pair']
    od_demand = data_with_route_variable['od_demand']
    od_number = data_with_route_variable['od_number']
    link_number = data_with_route_variable['link_number']
    node_number = data_with_route_variable['node_number']

    od_route_incidence = data_with_route_variable['od_route_incidence']
    route_link_incidence = data_with_route_variable['route_link_incidence']
    route_number = data_with_route_variable['route_number']

    parameter = set_parameter()
    para_a = parameter['a']
    para_b = parameter['b']

    def cal_objective_value_from_route_flow(route_flow):
        link_flow = route_flow @ route_link_incidence
        objective_value = (link_free * link_flow + link_free * para_a / (para_b + 1) * link_capacity * (
                (link_flow / link_capacity) ** (para_b + 1))).sum()
        return objective_value

    def cal_route_time(cf):
        rf = cf / 1
        lf = rf @ route_link_incidence
        lt = link_free * (1 + para_a * (lf ** para_b) / (link_capacity ** para_b))
        rt = route_link_incidence @ lt
        ct = rt + 0
        return ct

    od_route_number = od_route_incidence.sum(axis=1)
    initial_route_flow = (od_demand / od_route_number) @ od_route_incidence
    optimal_value = cal_objective_value_from_route_flow(initial_route_flow)

    current_route_flow = initial_route_flow

    tolerance = 1e-15
    iteration_number = 5000
    termination_bool = False
    optimum_bool = False
    iteration_index = 0
    while not (termination_bool or optimum_bool):
        current_route_cost = cal_route_time(current_route_flow)

        matrix_h = np.eye(route_number)
        rho = 0.1
        matrix_quadratic = matrix_h / 2
        vector_quadratic = rho * current_route_cost.reshape([1, route_number]) - matrix_h @ current_route_flow

        m = gp.Model()
        var = m.addMVar(shape=route_number, lb=0.0)
        m.addConstr(od_route_incidence @ var == od_demand)
        m.setObjective(var @ matrix_quadratic @ var + vector_quadratic @ var, GRB.MINIMIZE)
        m.optimize()
        new_route_flow = var.x

        temp_value = cal_objective_value_from_route_flow(new_route_flow)

        if iteration_index != 0:
            gap = np.abs((temp_value - optimal_value) / optimal_value)
        else:
            gap = 1
        if gap < tolerance:
            optimum_bool = True
        elif iteration_index == iteration_number:
            termination_bool = True

        optimal_value = temp_value
        current_route_flow = new_route_flow
        iteration_index += 1
        if bool_display_iteration:
            print('----iteration_of_algorithm_route_vi----')
            print(f'iteration_index:{iteration_index}')
            print(f'gap:{gap}')
            print(f'optimal_value:{optimal_value}')
            print('--------')

    if bool_display:
        print('----algorithm_route_vi----')
        print(f'iteration_index:{iteration_index}')
        print(f'optimum_bool:{optimum_bool}')
        print(f'gap:{gap}')
        print(f'current_route_flow:{current_route_flow}')
        print(f'cost:{cal_route_time(current_route_flow)}')
        print(f'optimal_value:{optimal_value}')
        print('--------')

    return current_route_flow, optimal_value
def GbpPrimCanCOMPLEX():

    t1 = time.time()

    #  Constants
    Aij = np.random.randint(5, 50, 250000)
    Aij = Aij.reshape(500, 500)
    AijSum = np.sum(Aij)
    Oij = np.random.randint(5, 10, 250000)
    Oij = Oij.reshape(500, 500)
    OijSum = np.sum(Oij)
    Cj = np.random.randint(10, 20, 500)
    CjSum = np.sum(Cj)
    Ej = np.random.randint(10, 20, 500)
    EjSum = np.sum(Ej)
    Gj = np.random.randint(10, 20, 500)
    GjSum = np.sum(Gj)
    Bi = np.random.randint(10, 20, 500)
    BiSum = np.sum(Bi)

    # Matrix Shape
    rows = range(len(Aij))
    cols = range(len(Aij[0]))

    # Instantiate Model
    mPrimal_Canonical_GUROBI = gbp.Model(" -- Canonical Primal Linear Programming Problem -- ")

    # Set Focus to Optimality
    gbp.setParam("MIPFocus", 2)

    # Decision Variables
    desc_var = []
    for dest in cols:
        desc_var.append([])
        desc_var[dest].append(mPrimal_Canonical_GUROBI.addVar(vtype=gbp.GRB.CONTINUOUS, name="y" + str(dest + 1)))
    o_var = []
    for dest in cols:
        o_var.append([])
        o_var[dest].append(mPrimal_Canonical_GUROBI.addVar(vtype=gbp.GRB.CONTINUOUS, name="o" + str(dest + 1)))
    p_var = []
    for dest in cols:
        p_var.append([])
        p_var[dest].append(mPrimal_Canonical_GUROBI.addVar(vtype=gbp.GRB.CONTINUOUS, name="p" + str(dest + 1)))

    # Update Model
    mPrimal_Canonical_GUROBI.update()

    # Objective Function
    mPrimal_Canonical_GUROBI.setObjective(
        gbp.quicksum(Cj[dest] * desc_var[dest][0] for dest in cols)
        + gbp.quicksum(Ej[dest] * o_var[dest][0] for dest in cols)
        + gbp.quicksum(Gj[dest] * p_var[dest][0] for dest in cols),
        gbp.GRB.MINIMIZE,
    )

    # Constraints
    for orig in rows:
        mPrimal_Canonical_GUROBI.addConstr(
            gbp.quicksum(Aij[orig][dest] * desc_var[dest][0] for dest in cols)
            + gbp.quicksum(Oij[orig][dest] * o_var[dest][0] for dest in cols)
            - Bi[orig]
            >= 2.5
        )
    for orig in rows:
        mPrimal_Canonical_GUROBI.addConstr(
            gbp.quicksum(Aij[orig][dest] * desc_var[dest][0] for dest in cols)
            + gbp.quicksum(Oij[orig][dest] * o_var[dest][0] for dest in cols)
            - Bi[orig]
            <= 150.006
        )
    for orig in rows:
        mPrimal_Canonical_GUROBI.addConstr(
            gbp.quicksum(desc_var[dest][0] for dest in cols) + gbp.quicksum(o_var[dest][0] for dest in cols) >= 7.25
        )
    for orig in rows:
        mPrimal_Canonical_GUROBI.addConstr(
            gbp.quicksum(desc_var[dest][0] for dest in cols) + gbp.quicksum(o_var[dest][0] for dest in cols) <= 47.29
        )
    for orig in rows:
        mPrimal_Canonical_GUROBI.addConstr(
            gbp.quicksum(Aij[orig][dest] * p_var[dest][0] for dest in cols)
            + gbp.quicksum(o_var[dest][0] for dest in cols) / Ej[orig]
            + Bi[orig]
            >= 1.7
        )

    # Optimize
    try:
        mPrimal_Canonical_GUROBI.optimize()
    except Exception as e:
        print "   ################################################################"
        print " < ISSUE : ", e, " >"
        print "   ################################################################"

    t2 = time.time() - t1
    # Write LP file
    mPrimal_Canonical_GUROBI.write("LP.lp")
    print "\n*************************************************************************"
    print "    |   Decision Variables"
    for v in mPrimal_Canonical_GUROBI.getVars():
        print "    |  ", v.VarName, "=", v.x
    print "*************************************************************************"
    val = mPrimal_Canonical_GUROBI.objVal
    print "    |   Objective Value ------------------ ", val
    print "    |   Aij Sum -------------------------- ", AijSum
    print "    |   Oij Sum -------------------------- ", OijSum
    print "    |   Cj Sum --------------------------- ", CjSum
    print "    |   Ej Sum --------------------------- ", EjSum
    print "    |   Bi Sum --------------------------- ", BiSum
    print "    |   Matrix Dimensions ---------------- ", Aij.shape
    print "    |   Build & Solve(sec.) -------------- ", t2
    print "    |   Date/Time ------------------------ ", dt.datetime.now()
    print "*************************************************************************"
    print "-- Gurobi Canonical Primal Linear Programming Problem --"
Пример #30
0
# later version.
#
# qpsolvers is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with qpsolvers. If not, see <http://www.gnu.org/licenses/>.

from numpy import empty
from gurobipy import Model, QuadExpr, GRB, GurobiError, quicksum, setParam


try:
    setParam('OutputFlag', 0)
except GurobiError as e:
    print("GurobiError: {}".format(e))


def get_nonzero_rows(M):
    nonzero_rows = {}
    rows, cols = M.nonzero()
    for ij in zip(rows, cols):
        i, j = ij
        if i not in nonzero_rows:
            nonzero_rows[i] = []
        nonzero_rows[i].append(j)
    return nonzero_rows

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 28 14:24:01 2018

@author: chrisr
"""
import numpy as np
from types import SimpleNamespace
import sklearn.linear_model as skl
import gurobipy as gb
gb.setParam('OutputFlag', 0)

class linear_explanation:
    def __init__(self):
        return None

    def encode_pandas(self, inputs):
        """Build a training set of dummy encoded variables from existing input
        data"""
        self.cox=np.empty(inputs.columns.size,dtype=np.object)
        self.constraints=np.empty_like(self.cox)
        for i in range(inputs.columns.shape[0]):
            col=inputs[inputs.columns[i]]
            vals=np.minimum(col,0)
            normal=col[(col>=0)]
            scale=normal.max()
            if scale==0 or np.isnan(scale):
                scale=1
            normal/=scale
            un=np.unique(vals)
Пример #32
0
def GbpPrimCan():
    #  Constants
    Aij = np.random.randint(5, 50, 25)
    Aij = Aij.reshape(5,5)
    AijSum = np.sum(Aij)
    Cj = np.random.randint(10, 20, 5)
    CjSum = np.sum(Cj)
    Bi = np.random.randint(10, 20, 5)
    BiSum = np.sum(Bi)
    
    # Matrix Shape
    rows = range(len(Aij))
    cols = range(len(Aij[0]))
    
    # Instantiate Model
    mPrimal_Canonical_GUROBI = gbp.Model(' -- Canonical Primal Linear Programming Problem -- ')
    
    # Set Focus to Optimality
    gbp.setParam('MIPFocus', 2)
    
    # Decision Variables
    desc_var = []
    for dest in cols:
        desc_var.append([])
        desc_var[dest].append(mPrimal_Canonical_GUROBI.addVar(
                                        vtype=gbp.GRB.CONTINUOUS, 
                                        name='y'+str(dest+1)))
    # Update Model
    mPrimal_Canonical_GUROBI.update()
    
    #Objective Function
    mPrimal_Canonical_GUROBI.setObjective(gbp.quicksum(
                            Cj[dest]*desc_var[dest][0] 
                            for dest in cols), 
                            gbp.GRB.MINIMIZE)
    # Constraints
    for orig in rows:
        mPrimal_Canonical_GUROBI.addConstr(gbp.quicksum(
                            Aij[orig][dest]*desc_var[dest][0] 
                            for dest in cols) - Bi[orig] >= 0)
    # Optimize
    try:
        mPrimal_Canonical_GUROBI.optimize()
    except Exception as e:
        print '   ################################################################'
        print ' < ISSUE : ', e, ' >'
        print '   ################################################################'
    
    # Write LP file
    mPrimal_Canonical_GUROBI.write('LP.lp')
    print '\n*************************************************************************'
    print '    |   Decision Variables'
    for v in mPrimal_Canonical_GUROBI.getVars():
        print '    |  ', v.VarName, '=', v.x
    print '*************************************************************************'
    val = mPrimal_Canonical_GUROBI.objVal
    print '    |   Objective Value ------------------ ', val
    print '    |   Aij Sum -------------------------- ', AijSum
    print '    |   Cj Sum --------------------------- ', CjSum
    print '    |   Bi Sum --------------------------- ', BiSum
    print '    |   Matrix Dimensions ---------------- ', Aij.shape
    print '    |   Date/Time ------------------------ ', dt.datetime.now()
    print '*************************************************************************'
    print '-- Gurobi Canonical Primal Linear Programming Problem --'
Пример #33
0
# later version.
#
# qpsolvers is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with qpsolvers. If not, see <http://www.gnu.org/licenses/>.

from numpy import empty
from gurobipy import Model, QuadExpr, GRB, GurobiError, quicksum, setParam


try:
    setParam('OutputFlag', 0)
except GurobiError as e:
    print("GurobiError: {}".format(e))


def get_nonzero_rows(M):
    nonzero_rows = {}
    rows, cols = M.nonzero()
    for ij in zip(rows, cols):
        i, j = ij
        if i not in nonzero_rows:
            nonzero_rows[i] = []
        nonzero_rows[i].append(j)
    return nonzero_rows

def GuTSP(nodes, min_cost, max_cost):
    #     1. Read In (or Create)Data
    # CREATE
    # Cost Matrix
    Cij = np.random.randint(min_cost, max_cost, nodes**2)
    Cij = Cij.reshape(nodes,nodes)
    # Cost Sum
    CijSum = np.sum(Cij)
    
    tour_nodes = range(len(Cij[0]))
    
    #       2. Create Model, Set MIP Focus, Add Variables, & Update Model
    m = gbp.Model(' -- Branch and Bound Directed Traveling Salesperson Problem -- ')
    # Set MIP Focus to 2 for optimality
    gbp.setParam('MIPFocus', 2)
    # Add Decision Variables
    desc_var = []
    for orig in tour_nodes:
        desc_var.append([])
        for dest in tour_nodes:
            desc_var[orig].append(m.addVar(vtype=gbp.GRB.BINARY, 
                                                obj=Cij[orig][dest], 
                                                name='x'+str(orig+1)+'_'+str(dest+1)))
    # Update Model Variables
    m.update()       
    
    #       3. Set Objective Function
    m.setObjective(gbp.quicksum(Cij[orig][dest]*desc_var[orig][dest] 
                            for orig in tour_nodes for dest in tour_nodes), 
                            gbp.GRB.MINIMIZE)
    
    #       4. Add Constraints
    #Add Assignment Constraints by summed rows x_ij
    for orig in tour_nodes:
        m.addConstr(gbp.quicksum(desc_var[orig][dest] 
                            for dest in tour_nodes) == 1)
    #Add Assignment Constraints by summed columns x_ji                        
    for orig in tour_nodes:
        m.addConstr(gbp.quicksum(desc_var[dest][orig] 
                            for dest in tour_nodes) == 1)
    
    #       5. Optimize and Print Results
    try:
        m.optimize()
    except Exception as e:
        print e
        
    t2 = time.time()-t1
    print '**********************************************************************'
    print '    | Nodes on Path ---------------------------- ', len(Cij)
    val = m.objVal
    print '    | Objective Value -------------------------- ', val
    avg = float(m.objVal)/float(len(Cij))
    print '    | Avg. Value / Arc ------------------------- ', avg
    print '    | Total Network Travel Cost ---------------- ', CijSum
    print '    | Real Time to Optimize (sec.) ------------- ', t2
    print '    | '
    print '    | Cij ', len(Cij)*' ↓' , '        Tour by Node Order'
    NoP = []
    for v in m.getVars():
        if v.x > 0:
            var = '%s' % v.VarName
            NoP.append(var)
    
    for i in range(len(NoP)):
        print '    |  -- ', Cij[i], ' --    ', NoP[i]
    print '    |                               ^^^^ Tour by Node Order'
    print '**********************************************************************'
    print ' -- Branch and Bound Directed Traveling Salesperson Problem -- '
    m.write('path_Gurobi_TSP.lp')
Пример #35
0
'''
# Cost Matrix
Cij = np.fromfile('path.txt', dtype=float, sep='\n')
Cij = Cij.reshape(#,#)
'''
# Cost Matrix
Cij = np.random.randint(100, 1000, 25)
Cij = Cij.reshape(5,5)

client_nodes = range(len(Cij[0]))
service_nodes = range(len(Cij))

#           2. Create Model, Set MIP Focus, Add Variables, & Update Model
m = gbp.Model(' -- P-Center -- ')
# Set MIP Focus to 2 for optimality
gbp.setParam('MIPFocus', 2)
# Add Client Decision Variables
client_var = []
for orig in service_nodes:
    client_var.append([])
    for dest in client_nodes:
        client_var[orig].append(m.addVar(vtype=gbp.GRB.BINARY, 
                                            obj=Cij[orig][dest], 
                                            name='x'+str(orig+1)+'_'+str(dest+1)))
# Add Service Decision Variables
serv_var = []
for dest in service_nodes:
    serv_var.append([])
    serv_var[dest].append(m.addVar(vtype=gbp.GRB.BINARY, 
                                    name='y'+str(dest+1)))
# Add Minimized Maximum Average Variable
Пример #36
0
            g = Graph()
            g.add_edges_from(e for e in izip(edgelist[::2], edgelist[1::2]))
            g.graph['name'] = str(i)
            _, _, _, tear_set, _ = solve_problem(g, set(irange(size)))
            assert opt == len(tear_set)
        print('Done with size', size)
        print_timestamp()
        print()


################################################################################

if __name__ == '__main__':

    from gurobipy import setParam
    setParam('LogFile', '/tmp/gurobi.log')
    setParam('LogToConsole', 0)

    #naughty_brute_force()
    #quit()

    #solve_difficult_for_ilp(solve_problem, log)
    solve_test_matrices(solve_problem, log)
    #quit()

    print('Started generative testing...')

    import os
    os.environ['HYPOTHESIS_STORAGE_DIRECTORY'] = '/tmp/ht'
    from hypothesis import given, Settings
    from hypothesis.strategies import integers
Пример #37
0
from typing import List, Tuple, Union, Dict

from gurobipy import LinExpr, Var, Model, Env, GRB, setParam

from RNN.MarabouRNNMultiDim import alpha_to_equation
from maraboupy import MarabouCore
from RNN.Bound import Bound

random.seed(0)
sign = lambda x: 1 if x >= 0 else -1

SMALL = 10 ** -4
LARGE = 10 ** 5
PRINT_GUROBI = False

setParam('Threads', 1)
setParam('NodefileStart', 0.5)
STAT_TOTAL_COUNTER = 'total_gurobi_steps'
STAT_FAIL_COUNTER = 'fail_gurobi_steps'
STAT_TOTAL_TIME = 'time_gurobi_steps'
STAT_IMPROVE_DECISION_TIME = 'time_gurobi_imporve_decision'
STAT_CONTINUOUS_COUNTER_EXAMPLE = 'continuous_counter_examples'



class GurobiSingleLayer:
    def __init__(self, rnnModel, xlim, polyhedron_max_dim, use_relu=True, use_counter_example=False,
                 add_alpha_constraint=False, max_steps=5,
                 layer_idx=0, debug=False, stats={}, **kwargs):
        '''
Пример #38
0
def gurobi_solve_qp(P,
                    q,
                    G=None,
                    h=None,
                    A=None,
                    b=None,
                    initvals=None,
                    verbose=False):
    """
    Solve a Quadratic Program defined as:

    .. math::

        \\begin{split}\\begin{array}{ll}
        \\mbox{minimize} &
            \\frac{1}{2} x^T P x + q^T x \\\\
        \\mbox{subject to}
            & G x \\leq h                \\\\
            & A x = h
        \\end{array}\\end{split}

    using `Gurobi <http://www.gurobi.com/>`_.

    Parameters
    ----------
    P : array, shape=(n, n)
        Primal quadratic cost matrix.
    q : array, shape=(n,)
        Primal quadratic cost vector.
    G : array, shape=(m, n)
        Linear inequality constraint matrix.
    h : array, shape=(m,)
        Linear inequality constraint vector.
    A : array, shape=(meq, n), optional
        Linear equality constraint matrix.
    b : array, shape=(meq,), optional
        Linear equality constraint vector.
    initvals : array, shape=(n,), optional
        Warm-start guess vector (not used).
    verbose : bool, optional
        Set to `True` to print out extra information.

    Returns
    -------
    x : array, shape=(n,)
        Solution to the QP, if found, otherwise ``None``.
    """
    setParam('OutputFlag', 1 if verbose else 0)
    if initvals is not None:
        print("Gurobi: note that warm-start values are ignored by wrapper")
    n = P.shape[1]
    model = Model()
    x = {
        i: model.addVar(vtype=GRB.CONTINUOUS,
                        name='x_%d' % i,
                        lb=-GRB.INFINITY,
                        ub=+GRB.INFINITY)
        for i in range(n)
    }
    model.update()  # integrate new variables

    # minimize
    #     1/2 x.T * P * x + q * x
    obj = QuadExpr()
    rows, cols = P.nonzero()
    for i, j in zip(rows, cols):
        obj += 0.5 * x[i] * P[i, j] * x[j]
    for i in range(n):
        obj += q[i] * x[i]
    model.setObjective(obj, GRB.MINIMIZE)

    # subject to
    #     G * x <= h
    if G is not None:
        G_nonzero_rows = get_nonzero_rows(G)
        for i, row in G_nonzero_rows.items():
            model.addConstr(quicksum(G[i, j] * x[j] for j in row) <= h[i])

    # subject to
    #     A * x == b
    if A is not None:
        A_nonzero_rows = get_nonzero_rows(A)
        for i, row in A_nonzero_rows.items():
            model.addConstr(quicksum(A[i, j] * x[j] for j in row) == b[i])

    model.optimize()

    a = empty(n)
    for i in range(n):
        a[i] = model.getVarByName('x_%d' % i).x
    return a