Python pulp.LpVariable() Examples

The following are 30 code examples of pulp.LpVariable(). 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: multiplier_model.py    From pyDEA with MIT License 7 votes vote down vote up
def get_equality_constraint(self, input_data, dmu_code, input_variables,
                                output_variables):
        ''' Generates equality constraint of input-oriented multiplier model.

            Args:
                input_data (InputData): object that stores input data.
                dmu_code (str): DMU code.
                input_variables (dict of str to pulp.LpVariable): dictionary
                    that maps variable name to pulp variable corresponding
                    to input categories.
                output_variables (dict of str to pulp.LpVariable): dictionary
                    that maps variable name to pulp variable corresponding
                    to output categories.

            Returns:
                pulp.LpConstraint: equality constraint.
        '''
        return pulp.lpSum([input_data.coefficients[dmu_code, category] *
                          input_variables[category]
                          for category in input_data.input_categories]) == 1 
Example #2
Source File: multiplier_model.py    From pyDEA with MIT License 6 votes vote down vote up
def update_objective(self, input_data, dmu_code, input_variables,
                         output_variables, lp_model):
        ''' Updates coefficients of the objective function of a given model.

            Args:
                input_data (InputData): object that stores input data.
                dmu_code (str): DMU code.
                input_variables (dict of str to pulp.LpVariable): dictionary
                    that maps variable name to pulp variable corresponding
                    to input categories.
                output_variables (dict of str to pulp.LpVariable): dictionary
                    that maps variable name to pulp variable corresponding
                    to output categories.
                lp_model (pulp.LpProblem): linear programming model.
        '''
        for category in input_data.output_categories:
            lp_model.objective[output_variables[category]] = input_data.coefficients[
                dmu_code, category] 
Example #3
Source File: multiplier_model.py    From pyDEA with MIT License 6 votes vote down vote up
def update_equality_constraint(self, input_data, dmu_code, input_variables,
                                   output_variables, lp_model):
        ''' Updates coefficients of the equality constraint of a given model.

            Args:
                input_data (InputData): object that stores input data.
                dmu_code (str): DMU code.
                input_variables (dict of str to pulp.LpVariable): dictionary
                    that maps variable name to pulp variable corresponding
                    to input categories.
                output_variables (dict of str to pulp.LpVariable): dictionary
                    that maps variable name to pulp variable corresponding
                    to output categories.
                lp_model (pulp.LpProblem): linear programming model.
        '''
        for category, var in input_variables.items():
            lp_model.constraints['equality_constraint'][var] = input_data.coefficients[
                dmu_code, category] 
Example #4
Source File: multiplier_model.py    From pyDEA with MIT License 6 votes vote down vote up
def get_objective_function(self, input_data, dmu_code, input_variables,
                               output_variables):
        ''' Generates objective function of output-oriented multiplier model.

            Args:
                input_data (InputData): object that stores input data.
                dmu_code (str): DMU code.
                input_variables (dict of str to pulp.LpVariable): dictionary
                    that maps variable name to pulp variable corresponding
                    to input categories.
                output_variables (dict of str to pulp.LpVariable): dictionary
                    that maps variable name to pulp variable corresponding
                    to output categories.

            Returns:
                pulp.LpSum: objective function.
        '''
        return pulp.lpSum([input_data.coefficients[dmu_code, category] *
                          input_variables[category]
                          for category in input_data.input_categories]) 
Example #5
Source File: multiplier_model.py    From pyDEA with MIT License 6 votes vote down vote up
def get_equality_constraint(self, input_data, dmu_code, input_variables,
                                output_variables):
        ''' Generates equality constraint of output-oriented multiplier model.

            Args:
                input_data (InputData): object that stores input data.
                dmu_code (str): DMU code.
                input_variables (dict of str to pulp.LpVariable): dictionary
                    that maps variable name to pulp variable corresponding
                    to input categories.
                output_variables (dict of str to pulp.LpVariable): dictionary
                    that maps variable name to pulp variable corresponding
                    to output categories.

            Returns:
                pulp.LpConstraint: equality constraint.
        '''
        return pulp.lpSum([input_data.coefficients[dmu_code, category] *
                          output_variables[category]
                          for category in input_data.output_categories]) == 1 
Example #6
Source File: multiplier_model.py    From pyDEA with MIT License 6 votes vote down vote up
def update_equality_constraint(self, input_data, dmu_code, input_variables,
                                   output_variables, lp_model):
        ''' Updates coefficients of the equality constraint of a given model.

            Args:
                input_data (InputData): object that stores input data.
                dmu_code (str): DMU code.
                input_variables (dict of str to pulp.LpVariable): dictionary
                    that maps variable name to pulp variable corresponding
                    to input categories.
                output_variables (dict of str to pulp.LpVariable): dictionary
                    that maps variable name to pulp variable corresponding
                    to output categories.
                lp_model (pulp.LpProblem): linear programming model.
        '''
        for category, var in output_variables.items():
            lp_model.constraints['equality_constraint'][var] = input_data.coefficients[
                dmu_code, category] 
Example #7
Source File: envelopment_model.py    From pyDEA with MIT License 6 votes vote down vote up
def update_output_category_coefficient(self, current_output, constraint,
                                           obj_var, output_category):
        ''' Updates coefficient of a given output category with a new
            value.

            Args:
                current_output (double): new value for the coefficient.
                constraint (pulp.LpConstraint): constraint whose coefficient
                    should be updated.
                obj_var (pulp.LpVariable): variable of the envelopment
                    model that is optimised in the objective function.
                output_category (str): output category name.
        '''
        if output_category in self.non_disc_outputs:
            constraint.changeRHS(current_output)
        else:
            constraint[obj_var] = -current_output 
Example #8
Source File: envelopment_model.py    From pyDEA with MIT License 6 votes vote down vote up
def get_input_variable_coefficient(self, obj_variable, input_category):
        ''' Returns proper coefficient depending on the fact if variable
            is discretionary or not.

            Args:
                obj_variable (pulp.LpVariable): pulp variable that corresponds
                    to input category of current DMU.
                input_category (str): input category for which current
                    constraint is being created.

            Returns:
                double or pulp.LpVariable: input variable coefficient.

        '''
        if input_category in self.non_disc_inputs:
            return 1
        return obj_variable 
Example #9
Source File: envelopment_model_base.py    From pyDEA with MIT License 5 votes vote down vote up
def _add_constraints_for_inputs(self, variables,
                                    dmu_code, obj_variable):
        ''' Adds constraints for inputs to LP.

            Args:
                variables (dict of {str: pulp.LpVariable}): a dictionary that
                    maps DMU codes to pulp.LpVariable, created with
                    pulp.LpVariable.dicts.
                dmu_code (str): DMU code for which LP is being created.
                obj_variable (pulp.LpVariable): LP variable that is optimised
                    (either efficiency score or inverse of efficiency score).
        '''
        for (count, input_category) in enumerate(
                self.input_data.input_categories):
            current_input = self.input_data.coefficients[(dmu_code,
                                                         input_category)]
            input_coeff = self._concrete_model.get_input_variable_coefficient(
                obj_variable, input_category)
            sum_all_inputs = pulp.lpSum([variables[dmu] *
                                        self.input_data.coefficients
                                        [(dmu, input_category)]
                                        for dmu in
                                        self.input_data.DMU_codes])
            name = 'constraint_input_{count}'.format(count=count)
            self.lp_model += (self._constraint_creator.create(
                              input_coeff * current_input
                              - sum_all_inputs, 0, input_category), name)
            self._constraints[input_category] = name 
Example #10
Source File: multiplier_model_decorators.py    From pyDEA with MIT License 5 votes vote down vote up
def _create_lp(self):
        ''' Creates initial LP.
        '''
        self._model_to_decorate._create_lp()
        self.lp_model = self._model_to_decorate.lp_model

        self._vrs_variable = pulp.LpVariable('VRS_variable', None, None,
                                             pulp.LpContinuous)
        self._model_to_decorate.lp_model.objective += self._vrs_variable

        for dmu_constraint in self._model_to_decorate._dmu_constraint_names.keys():
            self._model_to_decorate.lp_model.constraints[dmu_constraint] += (
                self.multiplier * self._vrs_variable) 
Example #11
Source File: multiplier_model_decorators.py    From pyDEA with MIT License 5 votes vote down vote up
def _change_lower_bound(self, variables):
        ''' Changes lower bound for variables corresponding to weakly
            disposable categories.

            Args:
                variables (dict of str to pulp.LpVariable): dictionary of
                    pulp variables than maps variable names to pulp variables.
        '''
        for category, var in variables.items():
            if category in self.weakly_disposable_categories:
                var.lowBound = None 
Example #12
Source File: multiplier_model_decorators.py    From pyDEA with MIT License 5 votes vote down vote up
def _get_variables(self):
        ''' Returns proper variables.

            Returns:
                dict of str to pulp.LpVariable: dictionary of
                    pulp variables than maps variable names to pulp variables.

        '''
        raise NotImplementedError() 
Example #13
Source File: multiplier_model_decorators.py    From pyDEA with MIT License 5 votes vote down vote up
def _get_input_variables(self):
        ''' Returns pulp variables that correspond to input categories.

            Returns:
                dict of str to pulp.LpVariable: dictionary of
                    pulp variables than maps variable names to pulp variables.
        '''
        return self._model_to_decorate._input_variables 
Example #14
Source File: multiplier_model_decorators.py    From pyDEA with MIT License 5 votes vote down vote up
def _get_output_variables(self):
        ''' Returns pulp variables that correspond to output categories.

            Returns:
                dict of str to pulp.LpVariable: dictionary of
                    pulp variables than maps variable names to pulp variables.
        '''
        return self._model_to_decorate._output_variables 
Example #15
Source File: multiplier_model_decorators.py    From pyDEA with MIT License 5 votes vote down vote up
def _store_vars_ub(self, category, constraint_name, variable):
        ''' Stores a given variable in an internal data structure if needed.

            Args:
                category (str): category name.
                constraint_name (str): constraint name.
                variable (pulp.LpVariable): variable.
        '''
        pass 
Example #16
Source File: envelopment_model_base.py    From pyDEA with MIT License 5 votes vote down vote up
def _create_lp(self):
        ''' Creates initial linear program.
        '''
        assert len(self.input_data.DMU_codes) != 0
        self._variables.clear()
        self._constraints.clear()
        # create LP for the first DMU - it is the easiest way to
        # adapt current code
        for elem in self.input_data.DMU_codes:
            dmu_code = elem
            break

        orientation = self._concrete_model.get_orientation()
        obj_type = self._concrete_model.get_objective_type()
        self.lp_model = pulp.LpProblem('Envelopment model: {0}-'
                                       'oriented'.format(orientation),
                                       obj_type)
        ub_obj = self._concrete_model.get_upper_bound_for_objective_variable()
        obj_variable = pulp.LpVariable('Variable in objective function',
                                       self._concrete_model.
                                       get_lower_bound_for_objective_variable(),
                                       ub_obj,
                                       pulp.LpContinuous)
        self._variables['obj_var'] = obj_variable

        self.lp_model += (obj_variable,
                          'Efficiency score or inverse of efficiency score')

        lambda_variables = pulp.LpVariable.dicts('lambda',
                                                 self.input_data.DMU_codes,
                                                 0, None, pulp.LpContinuous)
        self._variables.update(lambda_variables)
        self._add_constraints_for_outputs(lambda_variables, dmu_code,
                                          obj_variable)
        self._add_constraints_for_inputs(lambda_variables, dmu_code,
                                         obj_variable) 
Example #17
Source File: envelopment_model_base.py    From pyDEA with MIT License 5 votes vote down vote up
def _add_constraints_for_outputs(self, variables, dmu_code,
                                     obj_variable):
        ''' Adds constraints for outputs to linear program.

            Args:
                variables (dict of str to pulp.LpVariable): a dictionary
                    that maps DMU codes to pulp.LpVariable, created with
                    pulp.LpVariable.dicts.
                dmu_code (str): DMU code for which LP is being created.
                obj_variable (pulp.LpVariable): LP variable that is optimised
                    (either efficiency score or inverse of efficiency score).
        '''
        for (count, output_category) in enumerate(
                self.input_data.output_categories):
            current_output = self.input_data.coefficients[(dmu_code,
                                                          output_category)]
            output_coeff = self._concrete_model.get_output_variable_coefficient(
                obj_variable, output_category)
            sum_all_outputs = pulp.lpSum([variables[dmu] *
                                         self.input_data.coefficients
                                         [(dmu, output_category)]
                                         for dmu in self.input_data.DMU_codes])
            name = 'constraint_output_{count}'.format(count=count)
            self.lp_model += (self._constraint_creator.create(
                              -output_coeff * current_output +
                              sum_all_outputs, 0, output_category), name)
            self._constraints[output_category] = name 
Example #18
Source File: envelopment_model_decorators.py    From pyDEA with MIT License 5 votes vote down vote up
def _add_constraints_for_outputs(self, variables, dmu_code,
                                     obj_variable):
        ''' Adds constraints for outputs to linear program.

            Args:
                variables (dict of str to pulp.LpVariable): a dictionary
                    that maps DMU codes to pulp.LpVariable, created with
                    pulp.LpVariable.dicts.
                dmu_code (str): DMU code for which LP is being created.
                obj_variable (pulp.LpVariable): LP variable that is optimised
                    (either efficiency score or inverse of efficiency score).
        '''
        self._model_to_decorate._add_constraints_for_outputs(variables,
                                                             dmu_code,
                                                             obj_variable) 
Example #19
Source File: simplex_test.py    From GiMPy with Eclipse Public License 1.0 5 votes vote down vote up
def solve(g):
    el = g.get_edge_list()
    nl = g.get_node_list()
    p = LpProblem('min_cost', LpMinimize)
    capacity = {}
    cost = {}
    demand = {}
    x = {}
    for e in el:
        capacity[e] = g.get_edge_attr(e[0], e[1], 'capacity')
        cost[e] = g.get_edge_attr(e[0], e[1], 'cost')
    for i in nl:
        demand[i] = g.get_node_attr(i, 'demand')
    for e in el:
        x[e] = LpVariable("x"+str(e), 0, capacity[e])
    # add obj
    objective = lpSum (cost[e]*x[e] for e in el)
    p += objective
    # add constraints
    for i in nl:
        out_neig = g.get_out_neighbors(i)
        in_neig = g.get_in_neighbors(i)
        p += lpSum(x[(i,j)] for j in out_neig) -\
             lpSum(x[(j,i)] for j in in_neig)==demand[i]
    p.solve()
    return x, value(objective) 
Example #20
Source File: test_constraints.py    From ConferenceScheduler with MIT License 5 votes vote down vote up
def test_constraints(events, slots, X):
    beta = pulp.LpVariable("upper_bound")
    constraints = [
        c for c in lpc.all_constraints(
            events, slots, X, beta)]
    assert len(constraints) == 55 
Example #21
Source File: chemistry.py    From chempy with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def _solve_balancing_ilp_pulp(A):
    import pulp
    x = [pulp.LpVariable('x%d' % i, lowBound=1, cat='Integer') for i in range(A.shape[1])]
    prob = pulp.LpProblem("chempy balancing problem", pulp.LpMinimize)
    prob += reduce(add, x)
    for expr in [pulp.lpSum([x[i]*e for i, e in enumerate(row)]) for row in A.tolist()]:
        prob += expr == 0
    prob.solve()
    return [pulp.value(_) for _ in x] 
Example #22
Source File: process_path.py    From Cogent with BSD 3-Clause Clear License 5 votes vote down vote up
def make_into_lp_problem(good_for, N, add_noise=False):
    """
    Helper function for solve_with_lp_and_reduce()

    N --- number of isoform sequences
    good_for --- dict of <isoform_index> --> list of matched paths index
    """
    prob = LpProblem("The Whiskas Problem",LpMinimize)

    # each good_for is (isoform_index, [list of matched paths index])
    # ex: (0, [1,2,4])
    # ex: (3, [2,5])
    used_paths = []
    for t_i, p_i_s in good_for:
        used_paths += p_i_s
    used_paths = list(set(used_paths))

    variables = [LpVariable(str(i),0,1,LpInteger) for i in used_paths]
    #variables = [LpVariable(str(i),0,1,LpInteger) for i in xrange(N)]

    # objective is to minimize sum_{Xi}
    prob += sum(v for v in variables)

    already_seen = set()
    # constraints are for each isoform, expressed as c_i * x_i >= 1
    # where c_i = 1 if x_i is matched for the isoform
    # ex: (0, [1,2,4]) becomes t_0 = x_1 + x_2 + x_4 >= 1
    for t_i, p_i_s in good_for:
        #c_i_s = [1 if i in p_i_s else 0 for i in xrange(N)]
        #prob += sum(variables[i]*(1 if i in p_i_s else 0) for i in xrange(N)) >= 1
        p_i_s.sort()
        pattern = ",".join(map(str,p_i_s))
        #print >> sys.stderr, t_i, p_i_s, pattern
        if pattern not in already_seen:
            if add_noise:
                prob += sum(variables[i]*(1+random.random() if p in p_i_s else 0) for i,p in enumerate(used_paths)) >= 1
            else:
                prob += sum(variables[i]*(1 if p in p_i_s else 0) for i,p in enumerate(used_paths)) >= 1
        already_seen.add(pattern)
    prob.writeLP('cogent.lp')
    return prob 
Example #23
Source File: envelopment_model_decorators.py    From pyDEA with MIT License 5 votes vote down vote up
def _add_constraints_for_inputs(self, variables,
                                    dmu_code, obj_variable):
        ''' Adds constraints for inputs to LP.

            Args:
                variables (dict of {str: pulp.LpVariable}): a dictionary that
                    maps DMU codes to pulp.LpVariable, created with
                    pulp.LpVariable.dicts.
                dmu_code (str): DMU code for which LP is being created.
                obj_variable (pulp.LpVariable): LP variable that is optimised
                    (either efficiency score or inverse of efficiency score).
        '''
        self._model_to_decorate._add_constraints_for_inputs(variables,
                                                            dmu_code,
                                                            obj_variable) 
Example #24
Source File: pulp_solver.py    From pydfs-lineup-optimizer with MIT License 5 votes vote down vote up
def add_variable(self, name):
        return LpVariable(name, cat=LpBinary) 
Example #25
Source File: envelopment_model.py    From pyDEA with MIT License 5 votes vote down vote up
def get_output_variable_coefficient(self, obj_variable, output_category):
        ''' Returns a proper coefficient depending on the fact if the variable
            is discretionary or not.

            Args:
                obj_variable (pulp.LpVariable): pulp variable that corresponds
                    to output category of current DMU.
                input_category (str): output category for which current
                    constraint is being created.
            Returns:
                double or pulp.LpVariable: output variable coefficient.
        '''
        if output_category in self.non_disc_outputs:
            return 1
        return obj_variable 
Example #26
Source File: envelopment_model.py    From pyDEA with MIT License 5 votes vote down vote up
def update_input_category_coefficient(self, current_input, constraint,
                                          obj_var, input_category):
        ''' Updates coefficient of a given input category with a new
            value.

            Args:
                current_output (double): new value for the coefficient.
                constraint (pulp.LpConstraint): constraint whose coefficient
                    should be updated.
                obj_var (pulp.LpVariable): variable of the envelopment
                    model that is optimised in the objective function.
                output_category (str): input category name.
        '''
        constraint.changeRHS(-current_input) 
Example #27
Source File: envelopment_model.py    From pyDEA with MIT License 5 votes vote down vote up
def update_output_category_coefficient(self, current_output, constraint,
                                           obj_var, output_category):
        ''' Updates coefficient of a given output category with a new
            value.

            Args:
                current_output (double): new value for the coefficient.
                constraint (pulp.LpConstraint): constraint whose coefficient
                    should be updated.
                obj_var (pulp.LpVariable): variable of the envelopment
                    model that is optimised in the objective function.
                output_category (str): output category name.
        '''
        constraint[obj_var] = -current_output 
Example #28
Source File: envelopment_model.py    From pyDEA with MIT License 5 votes vote down vote up
def get_input_variable_coefficient(self, obj_variable, input_category):
        ''' Returns 1, since in output-oriented model we do not multiply
            current input by anything.

            Args:
                obj_variable (pulp.LpVariable): pulp variable that corresponds
                    to input category of current DMU.
                input_category (str): input category for which current
                    constraint is being created.

            Returns:
                double: input variable coefficient.
        '''
        return 1 
Example #29
Source File: envelopment_model.py    From pyDEA with MIT License 5 votes vote down vote up
def get_output_variable_coefficient(self, obj_variable, output_category):
        ''' Returns obj_variable, since in output-oriented model we multiply
            current output by inverse efficiency spyDEA.core.

            Args:
                obj_variable (pulp.LpVariable): pulp variable that corresponds
                    to output category of current DMU.
                output_category (str): output category for which current
                    constraint is being created.

            Returns:
                pulp.LpVariable: output variable coefficient.
        '''
        return obj_variable 
Example #30
Source File: envelopment_model.py    From pyDEA with MIT License 5 votes vote down vote up
def update_output_category_coefficient(self, current_output, constraint,
                                           obj_var, output_category):
        ''' Updates coefficient of a given output category with a new
            value.

            Args:
                current_output (double): new value for the coefficient.
                constraint (pulp.LpConstraint): constraint whose coefficient
                    should be updated.
                obj_var (pulp.LpVariable): variable of the envelopment
                    model that is optimised in the objective function.
                output_category (str): output category name.
        '''
        constraint.changeRHS(current_output)