Python pulp.LpMaximize() Examples

The following are 8 code examples of pulp.LpMaximize(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pulp , or try the search function .
Example #1
Source File: lp_solve.py    From RevPy with MIT License 6 votes vote down vote up
def define_lp(fares, product_names):
    """Set up LP.

    Parameters
    ----------
    fares: 2D np array
            contains fares for products, size n_classes*n_products
    products_names: list
            product names (typically relation/class combinations)

    Returns
    -------
    tuple of the form (LP problem, decision variables)
    """
    prob = pulp.LpProblem('network_RM', pulp.LpMaximize)

    # decision variables: available seats per product
    x = pulp.LpVariable.dicts('x', product_names, lowBound=0)

    # objective function
    revenue = pulp.lpSum([x[it]*fares.ravel()[i] for i, it in
                          enumerate(product_names)])
    prob += revenue

    return prob, x 
Example #2
Source File: pulp_solver.py    From pydfs-lineup-optimizer with MIT License 5 votes vote down vote up
def __init__(self):
        self.prob = LpProblem('Daily Fantasy Sports', LpMaximize) 
Example #3
Source File: multiplier_model.py    From pyDEA with MIT License 5 votes vote down vote up
def get_objective_type(self):
        ''' Returns pulp.LpMaximize - we maximize objective function in case
            of input-oriented multiplier model.

            Returns:
                pulp.LpMaximize.
        '''
        return pulp.LpMaximize 
Example #4
Source File: envelopment_model.py    From pyDEA with MIT License 5 votes vote down vote up
def get_objective_type(self):
        ''' Returns pulp.LpMinimize - we minimize objective function in case
            of input-oriented envelopment model.

            Returns:
                pulp.LpMaximize: type of objective function.
        '''
        return pulp.LpMinimize 
Example #5
Source File: envelopment_model.py    From pyDEA with MIT License 5 votes vote down vote up
def get_objective_type(self):
        ''' Returns pulp.LpMinimize - we maximize objective function in case
            of output-oriented envelopment model.

            Returns:
                pulp.LpMaximize: objective function type.
        '''
        return pulp.LpMaximize 
Example #6
Source File: maximize_slacks.py    From pyDEA with MIT License 5 votes vote down vote up
def _create_lp(self):
        ''' See base class.
        '''
        self.model._create_lp()
        self.lp_model_max_slack = self.model.lp_model.deepcopy()

        input_slack_vars = pulp.LpVariable.dicts(
            'input_slack', self.strongly_disposal_input_categories,
            0, None, pulp.LpContinuous)
        output_slack_vars = pulp.LpVariable.dicts(
            'output_slack', self.strongly_disposal_output_categories,
            0, None, pulp.LpContinuous)

        # change objective function
        self.lp_model_max_slack.sense = pulp.LpMaximize
        self.lp_model_max_slack.objective = (
            pulp.lpSum(list(input_slack_vars.values())) +
            pulp.lpSum(list(output_slack_vars.values())))

        # change constraints
        for input_category in self.strongly_disposal_input_categories:
            name = self.model._constraints[input_category]
            self.lp_model_max_slack.constraints[name].addterm(
                input_slack_vars[input_category], 1)
            self.lp_model_max_slack.constraints[name].sense = pulp.LpConstraintEQ

        for output_category in self.strongly_disposal_output_categories:
            name = self.model._constraints[output_category]
            self.lp_model_max_slack.constraints[name].addterm(
                output_slack_vars[output_category], -1)
            self.lp_model_max_slack.constraints[name].sense = pulp.LpConstraintEQ 
Example #7
Source File: upper_bound_ilp.py    From acl2017-interactive_summarizer with Apache License 2.0 5 votes vote down vote up
def solve_ilp(self, N):
        # build the A matrix: a_ij is 1 if j-th gram appears in the i-th sentence

        A = np.zeros((len(self.sentences_idx), len(self.ref_ngrams_idx)))
        for i in self.sentences_idx:
            sent = self.sentences[i].untokenized_form
            sngrams = list(extract_ngrams2([sent], self.stemmer, self.LANGUAGE, N))
            for j in self.ref_ngrams_idx:
                if self.ref_ngrams[j] in sngrams:
                    A[i][j] = 1

        # Define ILP variable, x_i is 1 if sentence i is selected, z_j is 1 if gram j appears in the created summary
        x = pulp.LpVariable.dicts('sentences', self.sentences_idx, lowBound=0, upBound=1, cat=pulp.LpInteger)
        z = pulp.LpVariable.dicts('grams', self.ref_ngrams_idx, lowBound=0, upBound=1, cat=pulp.LpInteger)

        # Define ILP problem, maximum coverage of grams from the reference summaries
        prob = pulp.LpProblem("ExtractiveUpperBound", pulp.LpMaximize)
        prob += pulp.lpSum(z[j] for j in self.ref_ngrams_idx)

        # Define ILP constraints, length constraint and consistency constraint (impose that z_j is 1 if j
        # appears in the created summary)
        prob += pulp.lpSum(x[i] * self.sentences[i].length for i in self.sentences_idx) <= self.sum_length

        for j in self.ref_ngrams_idx:
            prob += pulp.lpSum(A[i][j] * x[i] for i in self.sentences_idx) >= z[j]

        # Solve ILP problem and post-processing to get the summary
        try:
            print('Solving using CPLEX')
            prob.solve(pulp.CPLEX(msg=0))
        except:
            print('Fall back to GLPK')
            prob.solve(pulp.GLPK(msg=0))
                

        summary_idx = []
        for idx in self.sentences_idx:
            if x[idx].value() == 1.0:
                summary_idx.append(idx)

        return summary_idx 
Example #8
Source File: space.py    From qmpy with MIT License 4 votes vote down vote up
def get_reaction(self, var, facet=None):
        """
        For a given composition, what is the maximum delta_composition reaction
        on the given facet. If None, returns the whole reaction for the given
        PhaseSpace.

        Examples::

            >>> space = PhaseSpace('Fe2O3-Li2O')
            >>> equilibria = space.hull[0]
            >>> space.get_reaction('Li2O', facet=equilibria)

        """

        if isinstance(var, basestring):
            var = parse_comp(var)

        if facet:
            phases = facet
        else:
            phases = self.stable

        prob = pulp.LpProblem('BalanceReaction', pulp.LpMaximize)
        pvars = pulp.LpVariable.dicts('prod', phases, 0)
        rvars = pulp.LpVariable.dicts('react', phases, 0)
        prob += sum([ p.fraction(var)['var']*pvars[p] for p in phases ])-\
                sum([ p.fraction(var)['var']*rvars[p] for p in phases ]),\
                "Maximize delta comp"
        for celt in self.space:
            prob += sum([ p.fraction(var)[celt]*pvars[p] for p in phases ]) ==\
                    sum([ p.fraction(var)[celt]*rvars[p] for p in phases ]),\
                    'identical %s composition on both sides' % celt
        prob += sum([ rvars[p] for p in phases ]) == 1
        
        if pulp.GUROBI().available():
            prob.solve(pulp.GUROBI(msg=False))
        elif pulp.COIN_CMD().available():
            prob.solve(pulp.COIN_CMD())
        elif pulp.COINMP_DLL().available():
            prob.solve(pulp.COINMP_DLL())
        else:
            prob.solve()

        prods = defaultdict(float,[ (c, pvars[c].varValue) for c in phases
            if pvars[c].varValue > 1e-4 ])
        reacts = defaultdict(float,[ (c, rvars[c].varValue) for c in phases
            if rvars[c].varValue > 1e-4 ])
        n_elt = pulp.value(prob.objective)
        return reacts, prods, n_elt