示例#1
0
class SolverGurobi(Solver):
    def __init__(self):
        Solver.__init__(self, GRB.BINARY, GRB.CONTINUOUS, GRB.INTEGER, GRB.OPTIMAL, GRB.INFEASIBLE, GRB.UNBOUNDED, GRB.INTERRUPTED, GRB.SUBOPTIMAL, GRB.EQUAL, GRB.LESS_EQUAL, GRB.GREATER_EQUAL, GRB.MAXIMIZE, GRB.MINIMIZE, GRB.INFINITY)
        setParam('OutputFlag', 0)
        setParam('IntFeasTol', 1e-9)
        self.problem = Model('gurobi_problem')

    def _add_variable(self, var):
        if var.var_type == self.BINARY:
            self.problem.addVar(name=var.name, vtype=var.var_type)
        else:
            self.problem.addVar(name=var.name, lb=var.lower_bound, ub=var.upper_bound, vtype=var.var_type)

    def _del_variable(self, name):
        self.problem.remove(self.problem.getVarByName(name))

    def _add_constraint(self, const):
        lhs = LinExpr()

        for elem in const.lhs:
            var = self.problem.getVarByName(elem.name)
            lhs.add(var, elem.coefficient)

        self.problem.addConstr(lhs, const.sense, const.rhs, const.name)

    def _del_constraint(self, name):
        self.problem.remove(self.problem.getConstrByName(name))

    def update(self):
        self.problem.update()

    def _set_objective(self, objective):
        expr = LinExpr()

        for elem in objective.expr:
            var = self.problem.getVarByName(elem.name)
            expr.add(var, elem.coefficient)

        self.problem.setObjective(expr, objective.sense)

    def get_variable_value(self, name):
        return self.problem.getVarByName(name).x

    def get_objective_value(self):
        return self.problem.objVal

    def get_solution_status(self):
        return self.problem.status

    def _optimize(self):
        self.problem.optimize()
示例#2
0
for v in substrates.difference(kinases):
    lp.addVar(vtype=GRB.BINARY, name=v)

# Initialise edges variables
for edge in in_edges:
    lp.addVar(vtype=GRB.BINARY, name=edge)

for edge in out_edges:
    lp.addVar(vtype=GRB.BINARY, name=edge)

# Update variables
lp.update()

# Initialise constraints
for site in sites:
    lhs = LinExpr([(1, lp.getVarByName(edge)) for edge in in_edges if edge.endswith(site)])
    lp.addConstr(lhs, GRB.EQUAL, lp.getVarByName(site))

for site in sites:
    for out_edge in out_edges:
        if out_edge.startswith(site):
            lp.addConstr(lp.getVarByName(out_edge), GRB.EQUAL, lp.getVarByName(site))

for kinase in kinases:
    for in_edge in in_edges:
        if in_edge.startswith(kinase):
            lp.addConstr(lp.getVarByName(kinase), GRB.GREATER_EQUAL, lp.getVarByName(in_edge))

for substrate in substrates:
    for out_edge in out_edges:
        if out_edge.endswith(substrate):
示例#3
0
class PCST:
    def __init__(self, network):
        self.lp = Model('PCST')
        self.network = network

    def initialise(self):
        # Initialise variables
        for n in self.network.nodes.values():
            n.var = self.lp.addVar(vtype=GRB.BINARY, name=n.id)

        for e in self.network.edges.values():
            e.var = self.lp.addVar(vtype=GRB.BINARY, name=e.id)

        # Update variables
        self.lp.update()

        # Root restriction
        lhs = LinExpr()
        for root_out in self.network.out_edges(self.network.root.id):
            lhs.addTerms(1, root_out.var)

        self.lp.addConstr(lhs, GRB.EQUAL, 1)

        # Income constraints
        for n in self.network.nodes.values():
            lhs = LinExpr()
            for e in self.network.in_edges(n.id):
                lhs.addTerms(1, e.var)

            self.lp.addConstr(lhs, GRB.EQUAL, LinExpr(n.var))

        # Reversibility constraint
        for e in self.network.edges.values():
            if e.inverse is not None:
                lhs = LinExpr([1, 1], [e.var, self.network.get_edge(e.inverse).var])

                self.lp.addConstr(lhs, GRB.LESS_EQUAL, self.network.get_node(e.source).var)
                self.lp.addConstr(lhs, GRB.LESS_EQUAL, self.network.get_node(e.target).var)

        for n in self.network.nodes.values():
            if n.type == Type.LEAF or n.type == Type.LEAF_TERMINAL:
                for in_edge in self.network.in_edges(n.id):
                    source = self.network.get_node(in_edge.source)
                    if source.type != Type.ROOT:
                        print n.id, in_edge.source
                        self.lp.addConstr(source.var, GRB.LESS_EQUAL, n.var)

    def objective_function(self, beta=1):
        # Set objective function
        obj = LinExpr()

        for e in self.network.edges.values():
            obj.addTerms(e.value, e.var)

        for n in self.network.nodes.values():
            if n.type == Type.NORMAL:
                obj.addTerms(-n.value * beta, n.var)

        self.lp.setObjective(obj, GRB.MINIMIZE)

    def optimise(self):
        self.lp.optimize()
        return self.get_solution()

    def edge_sensitivity_analysis(self, solution):
        scores = {}
        for e in solution.edges.values():
            edge_constraint = self.lp.addConstr(self.network.get_edge(e.id).var, GRB.EQUAL, 0)

            scores[e.id] = self.optimise()

            self.lp.remove(edge_constraint)

        print '\n[INFO] Edge sensivity analysis done! ' + str(len(solution.edges)) + ' edges analysed.'

        return scores

    def get_solution(self):
        edges = dict((e.id, Edge(e.source, e.target, self.lp.getVarByName(e.id).x)) for e in self.network.edges.values() if self.lp.getVarByName(e.id).x > 0.9999)
        obj_val = self.lp.objVal
        return Solution(edges, obj_val)

    def get_obj_value(self):
        return self.lp.objVal

    def print_solution(self):
        for v in self.lp.getVars():
            print v.varName, v.x

        print 'Obj:', self.lp.objVal