def test_int_bin(): A_iq = sp.coo_matrix(np.array([[1, 0], [0, 1], [-1, 0], [0, -1]])) b_iq = np.array([2.5, 2.5, 2.5, 2.5]) constr = Constraint(A_iq=A_iq, b_iq=b_iq) c = np.array([-1, -1]) if import_gurobi(): sol_int = solve_ilp(c, constr, [0, 1], [], solver="gurobi") np.testing.assert_equal(sol_int["x"], np.array([2, 2])) sol_bin = solve_ilp(c, constr, [], [0, 1], solver="gurobi") np.testing.assert_equal(sol_bin["x"], np.array([1, 1])) sol_mix = solve_ilp(c, constr, [0], [1], solver="gurobi") np.testing.assert_equal(sol_mix["x"], np.array([2, 1])) if import_mosek(): sol_int = solve_ilp(c, constr, [0, 1], [], solver="mosek") np.testing.assert_equal(sol_int["x"], np.array([2, 2])) sol_bin = solve_ilp(c, constr, [], [0, 1], solver="mosek") np.testing.assert_equal(sol_bin["x"], np.array([1, 1])) sol_mix = solve_ilp(c, constr, [0], [1], solver="mosek") np.testing.assert_equal(sol_mix["x"], np.array([2, 1]))
def _dynamic_constraint_48_m(problem): # Constructing A_eq and b_eq for equality (48) for master as sp.coo matrix A_iq_row = [] A_iq_col = [] A_iq_data = [] N = len(problem.graph.agents) constraint_idx = 0 for t, (v1, v2) in product(range(problem.T + 1), problem.graph.conn_edges()): A_iq_row.append(constraint_idx) A_iq_col.append(problem.get_mbar_idx(v1, v2, t)) A_iq_data.append(1) for r in problem.graph.agents: A_iq_row.append(constraint_idx) A_iq_col.append(problem.get_z_idx(r, v1, t)) A_iq_data.append(-N) constraint_idx += 1 for t, (v1, v2) in product(range(problem.T + 1), problem.graph.conn_edges()): A_iq_row.append(constraint_idx) A_iq_col.append(problem.get_mbar_idx(v1, v2, t)) A_iq_data.append(1) for r in problem.graph.agents: A_iq_row.append(constraint_idx) A_iq_col.append(problem.get_z_idx(r, v2, t)) A_iq_data.append(-N) constraint_idx += 1 A_iq_48 = sp.coo_matrix((A_iq_data, (A_iq_row, A_iq_col)), shape=(constraint_idx, problem.num_vars)) return Constraint(A_iq=A_iq_48, b_iq=np.zeros(constraint_idx))
def _dynamic_constraint_agent_avoidance(problem): # Constructing A_iq and b_iq for agent avoidance as sp.coo matrix A_row = [] A_col = [] A_data = [] constraint_idx = 0 # Constraint (56) for t, v, (r1, r2) in product(range(problem.T + 1), problem.graph.nodes, combinations(problem.big_agents, 2)): if problem.graph.nodes[v]["small"]: if not (problem.graph.agents[r1] == problem.graph.agents[r2] and problem.graph.agents[r1] == v): A_row.append(constraint_idx) A_col.append(problem.get_z_idx(r1, v, t)) A_data.append(1) A_row.append(constraint_idx) A_col.append(problem.get_z_idx(r2, v, t)) A_data.append(1) constraint_idx += 1 # Constraint (57) for t, (v1, v2), (r1, r2) in product( range(problem.T), problem.graph.tran_edges(), combinations(problem.big_agents, 2), ): if (problem.graph.nodes[v1]["small"]) or ( problem.graph.nodes[v2]["small"]): A_row.append(constraint_idx) A_col.append(problem.get_xf_idx(r1, v1, v2, t)) A_data.append(1) A_row.append(constraint_idx) A_col.append(problem.get_xf_idx(r2, v2, v1, t)) A_data.append(1) constraint_idx += 1 A_row.append(constraint_idx) A_col.append(problem.get_xf_idx(r1, v2, v1, t)) A_data.append(1) A_row.append(constraint_idx) A_col.append(problem.get_xf_idx(r2, v1, v2, t)) A_data.append(1) constraint_idx += 1 A = sp.coo_matrix((A_data, (A_row, A_col)), shape=(constraint_idx, problem.num_vars)) # .toarray( return Constraint(A_iq=A, b_iq=np.ones(constraint_idx))
def _dynamic_constraint_50(problem): """constraint on z, y""" A_iq_row = [] A_iq_col = [] A_iq_data = [] frontier_nodes = filter( lambda v: "frontiers" in problem.graph.nodes[v] and problem.graph. nodes[v]["frontiers"] != 0, problem.graph.nodes, ) constraint_idx = 0 for v, k in product(problem.graph.nodes, range(1, problem.num_r + 1)): A_iq_row.append(constraint_idx) A_iq_col.append(problem.get_y_idx(v, k)) A_iq_data.append(1) for r in problem.graph.agents: if v in frontier_nodes: if r in problem.eagents: A_iq_row.append(constraint_idx) A_iq_col.append(problem.get_z_idx(r, v, problem.T)) A_iq_data.append(-1 / k) else: A_iq_row.append(constraint_idx) A_iq_col.append(problem.get_z_idx(r, v, problem.T)) A_iq_data.append(-1 / k) constraint_idx += 1 A_iq_50 = sp.coo_matrix((A_iq_data, (A_iq_row, A_iq_col)), shape=(constraint_idx, problem.num_vars)) return Constraint(A_iq=A_iq_50, b_iq=np.zeros(constraint_idx))
def _dynamic_constraint_static(problem): # Constructing A_eq and b_eq for dynamic condition on static agents as sp.coo matrix A_stat_row = [] A_stat_col = [] A_stat_data = [] b_stat = [] constraint_idx = 0 # Enforce static agents to be static for t, r, v in product(range(problem.T + 1), problem.static_agents, problem.graph.nodes): A_stat_row.append(constraint_idx) A_stat_col.append(problem.get_z_idx(r, v, t)) A_stat_data.append(1) b_stat.append(1 if problem.graph.agents[r] == v else 0) constraint_idx += 1 if problem.final_position: # Enforce final position for agents for r, v in problem.final_position.items(): A_stat_row.append(constraint_idx) A_stat_col.append(problem.get_z_idx(r, v, problem.T)) A_stat_data.append(1) b_stat.append(1) constraint_idx += 1 A_stat = sp.coo_matrix( (A_stat_data, (A_stat_row, A_stat_col)), shape=(constraint_idx, problem.num_vars), ) # .toarray( return Constraint(A_eq=A_stat, b_eq=b_stat)
def _dynamic_constraint_52_53(problem): # Constructing A_eq and b_eq for equality (52,53) as sp.coo matrix A_eq_row = [] A_eq_col = [] A_eq_data = [] constraint_idx = 0 for t, v, (b, b_r) in product(range(problem.T + 1), problem.graph.nodes, enumerate(problem.min_src_snk)): if t > 0: for edge in problem.graph.tran_in_edges(v): A_eq_row.append(constraint_idx) A_eq_col.append(problem.get_f_idx(b, edge[0], edge[1], t - 1)) A_eq_data.append(1) for edge in problem.graph.conn_in_edges(v): A_eq_row.append(constraint_idx) A_eq_col.append(problem.get_fbar_idx(b, edge[0], edge[1], t)) A_eq_data.append(1) if t < problem.T: for edge in problem.graph.tran_out_edges(v): A_eq_row.append(constraint_idx) A_eq_col.append(problem.get_f_idx(b, edge[0], edge[1], t)) A_eq_data.append(-1) for edge in problem.graph.conn_out_edges(v): A_eq_row.append(constraint_idx) A_eq_col.append(problem.get_fbar_idx(b, edge[0], edge[1], t)) A_eq_data.append(-1) if problem.always_src or len(problem.src) <= len(problem.snk): # case (52) if t == 0: A_eq_row.append(constraint_idx) A_eq_col.append(problem.get_z_idx(b_r, v, t)) A_eq_data.append(len(problem.snk)) elif t == problem.T: for r in problem.snk: A_eq_row.append(constraint_idx) A_eq_col.append(problem.get_z_idx(r, v, t)) A_eq_data.append(-1) else: # case (53) if t == 0: for r in problem.src: A_eq_row.append(constraint_idx) A_eq_col.append(problem.get_z_idx(r, v, t)) A_eq_data.append(1) elif t == problem.T: A_eq_row.append(constraint_idx) A_eq_col.append(problem.get_z_idx(b_r, v, t)) A_eq_data.append(-len(problem.src)) constraint_idx += 1 A_eq_52 = sp.coo_matrix((A_eq_data, (A_eq_row, A_eq_col)), shape=(constraint_idx, problem.num_vars)) return Constraint(A_eq=A_eq_52, b_eq=np.zeros(constraint_idx))
def _dynamic_constraint_outflow_bound(problem): # Constructing A_iq and b_iq A_iq_row = [] A_iq_col = [] A_iq_data = [] N = len(problem.graph.agents) m_v0 = [problem.graph.agents[r] for r in problem.master] constraint_idx = 0 for r, (b, _), t in product(problem.graph.agents, enumerate(problem.min_src_snk), range(problem.T + 1)): v0 = problem.graph.agents[r] if v0 not in m_v0: for tau in range(t + 1): if tau > 0: for edge in problem.graph.tran_in_edges(v0): A_iq_row.append(constraint_idx) A_iq_col.append( problem.get_m_idx(edge[0], edge[1], tau - 1)) A_iq_data.append(-N) for edge in problem.graph.conn_in_edges(v0): A_iq_row.append(constraint_idx) A_iq_col.append(problem.get_mbar_idx( edge[0], edge[1], tau)) A_iq_data.append(-N) if tau < problem.T: for edge in problem.graph.tran_out_edges(v0): A_iq_row.append(constraint_idx) A_iq_col.append( problem.get_m_idx(edge[0], edge[1], tau)) A_iq_data.append(N) for edge in problem.graph.conn_out_edges(v0): A_iq_row.append(constraint_idx) A_iq_col.append(problem.get_mbar_idx( edge[0], edge[1], tau)) A_iq_data.append(N) for edge in problem.graph.conn_out_edges(v0): A_iq_row.append(constraint_idx) A_iq_col.append(problem.get_fbar_idx(b, edge[0], edge[1], t)) A_iq_data.append(1) constraint_idx += 1 A_iq = sp.coo_matrix((A_iq_data, (A_iq_row, A_iq_col)), shape=(constraint_idx, problem.num_vars)) return Constraint(A_iq=A_iq, b_iq=np.zeros(constraint_idx))
def _dynamic_constraint_55(problem): # Constructing A_eq and b_eq for equality (55) as sp.coo matrix A_iq_row = [] A_iq_col = [] A_iq_data = [] b_iq_55 = [] constraint_idx = 0 m_v0 = [problem.graph.agents[r] for r in problem.master] for t, r in product(range(problem.T + 1), problem.graph.agents): v0 = problem.graph.agents[r] if r not in problem.master and v0 not in m_v0: A_iq_row.append(constraint_idx) A_iq_col.append(problem.get_z_idx(r, v0, t)) A_iq_data.append(-1) for tau in range(t): # z_t is locked unless info arrived at some point before t-1 if tau > 0: for edge in problem.graph.tran_in_edges(v0): A_iq_row.append(constraint_idx) A_iq_col.append( problem.get_m_idx(edge[0], edge[1], tau - 1)) A_iq_data.append(-1) for edge in problem.graph.conn_in_edges(v0): A_iq_row.append(constraint_idx) A_iq_col.append(problem.get_mbar_idx( edge[0], edge[1], tau)) A_iq_data.append(-1) if tau < problem.T: for edge in problem.graph.tran_out_edges(v0): A_iq_row.append(constraint_idx) A_iq_col.append( problem.get_m_idx(edge[0], edge[1], tau)) A_iq_data.append(1) for edge in problem.graph.conn_out_edges(v0): A_iq_row.append(constraint_idx) A_iq_col.append(problem.get_mbar_idx( edge[0], edge[1], tau)) A_iq_data.append(1) b_iq_55.append(-1) constraint_idx += 1 A_iq_55 = sp.coo_matrix((A_iq_data, (A_iq_row, A_iq_col)), shape=(constraint_idx, problem.num_vars)) return Constraint(A_iq=A_iq_55, b_iq=b_iq_55)
def _dynamic_constraint_58(problem): # Constructing A_iq and b_iq for equality (58) as sp.coo matrix A_iq_row = [] A_iq_col = [] A_iq_data = [] m_v0 = [problem.graph.agents[r] for r in problem.master] constraint_idx = 0 for v, k in product(problem.graph.nodes, range(1, problem.num_r + 1)): if v in m_v0: continue A_iq_row.append(constraint_idx) A_iq_col.append(problem.get_y_idx(v, k)) A_iq_data.append(1) for tau in range(problem.T + 1): if tau > 0: for edge in problem.graph.tran_in_edges(v): A_iq_row.append(constraint_idx) A_iq_col.append( problem.get_m_idx(edge[0], edge[1], tau - 1)) A_iq_data.append(-1) for edge in problem.graph.conn_in_edges(v): A_iq_row.append(constraint_idx) A_iq_col.append(problem.get_mbar_idx(edge[0], edge[1], tau)) A_iq_data.append(-1) if tau < problem.T: for edge in problem.graph.tran_out_edges(v): A_iq_row.append(constraint_idx) A_iq_col.append(problem.get_m_idx(edge[0], edge[1], tau)) A_iq_data.append(1) for edge in problem.graph.conn_out_edges(v): A_iq_row.append(constraint_idx) A_iq_col.append(problem.get_mbar_idx(edge[0], edge[1], tau)) A_iq_data.append(1) constraint_idx += 1 A_iq_58 = sp.coo_matrix((A_iq_data, (A_iq_row, A_iq_col)), shape=(constraint_idx, problem.num_vars)) return Constraint(A_iq=A_iq_58, b_iq=np.zeros(constraint_idx))
def _dynamic_constraint_27(problem): # Constructing A_eq and b_eq for equality for agent existence as sp.coo matrix A_eq_row = [] A_eq_col = [] A_eq_data = [] constraint_idx = 0 for t, r in product(range(problem.T + 1), problem.graph.agents): for v in problem.graph.nodes: A_eq_row.append(constraint_idx) A_eq_col.append(problem.get_z_idx(r, v, t)) A_eq_data.append(1) constraint_idx += 1 A_eq_27 = sp.coo_matrix((A_eq_data, (A_eq_row, A_eq_col)), shape=(constraint_idx, problem.num_vars)) return Constraint(A_eq=A_eq_27, b_eq=np.ones(constraint_idx))
def _dynamic_constraint_54(problem): # Constructing A_eq and b_eq for equality (55) as sp.coo matrix A_iq_row = [] A_iq_col = [] A_iq_data = [] b_iq_54 = [] v0 = [problem.graph.agents[r] for r in problem.master] constraint_idx = 0 for t, v in product(range(problem.T + 1), problem.graph.nodes): if t > 0: for edge in problem.graph.tran_in_edges(v): A_iq_row.append(constraint_idx) A_iq_col.append(problem.get_m_idx(edge[0], edge[1], t - 1)) A_iq_data.append(-1) for edge in problem.graph.conn_in_edges(v): A_iq_row.append(constraint_idx) A_iq_col.append(problem.get_mbar_idx(edge[0], edge[1], t)) A_iq_data.append(-1) if t < problem.T: for edge in problem.graph.tran_out_edges(v): A_iq_row.append(constraint_idx) A_iq_col.append(problem.get_m_idx(edge[0], edge[1], t)) A_iq_data.append(1) for edge in problem.graph.conn_out_edges(v): A_iq_row.append(constraint_idx) A_iq_col.append(problem.get_mbar_idx(edge[0], edge[1], t)) A_iq_data.append(1) if t == 0 and v in v0: b_iq_54.append(len(problem.graph)) else: b_iq_54.append(0) constraint_idx += 1 A_iq_54 = sp.coo_matrix((A_iq_data, (A_iq_row, A_iq_col)), shape=(constraint_idx, problem.num_vars)) return Constraint(A_iq=A_iq_54, b_iq=b_iq_54)
def generate_initial_constraints(problem): A_init_row = [] A_init_col = [] A_init_data = [] b_init = [] constraint_idx = 0 for r, v in product(problem.graph.agents, problem.graph.nodes): A_init_row.append(constraint_idx) A_init_col.append(problem.get_z_idx(r, v, 0)) A_init_data.append(1) b_init.append(1 if problem.graph.agents[r] == v else 0) constraint_idx += 1 A_init = sp.coo_matrix( (A_init_data, (A_init_row, A_init_col)), shape=(constraint_idx, problem.num_vars), ) return Constraint(A_eq=A_init, b_eq=b_init)
def _dynamic_constraint_47(problem): A_eq_row = [] A_eq_col = [] A_eq_data = [] constraint_idx = 0 for t, v, r in product(range(problem.T), problem.graph.nodes, problem.graph.agents): A_eq_row.append(constraint_idx) A_eq_col.append(problem.get_z_idx(r, v, t)) A_eq_data.append(1) for edge in problem.graph.tran_out_edges(v): A_eq_row.append(constraint_idx) A_eq_col.append(problem.get_xf_idx(r, edge[0], edge[1], t)) A_eq_data.append(-1) constraint_idx += 1 A_eq_47 = sp.coo_matrix((A_eq_data, (A_eq_row, A_eq_col)), shape=(constraint_idx, problem.num_vars)) return Constraint(A_eq=A_eq_47, b_eq=np.zeros(constraint_idx))
def constraint_static_master(problem, master): # Constructing A_iq and b_iq for equality (59) as sp.coo matrix A_iq_row = [] A_iq_col = [] A_iq_data = [] b_iq = [] constraint_idx = 0 if master in problem.static_agents: master_node = problem.graph.agents[master] # find nodes connected to master through static agents connected_nodes = [] active = [master_node] while len(active) > 0: v = active.pop(0) connected_nodes.append(v) for _, v1 in problem.graph.conn_out_edges(v): if v1 not in connected_nodes: connected_nodes.append(v1) # check if any static agent in nbr for r in problem.static_agents: if problem.graph.agents[r] == v1: active.append(v1) dynamic_agents = [ r for r in problem.graph.agents if r not in problem.static_agents ] for v, r in product(connected_nodes, dynamic_agents): A_iq_row.append(constraint_idx) A_iq_col.append(problem.get_z_idx(r, v, problem.T)) A_iq_data.append(-1) b_iq.append(-1) constraint_idx += 1 A_iq = sp.coo_matrix((A_iq_data, (A_iq_row, A_iq_col)), shape=(constraint_idx, problem.num_vars)) return Constraint(A_iq=A_iq, b_iq=b_iq)
def _dynamic_constraint_30(problem): # Constructing A_eq and b_eq for identity dynamics as sp.coo matrix A_iq_row = [] A_iq_col = [] A_iq_data = [] constraint_idx = 0 for t, v, r in product(range(problem.T), problem.graph.nodes, problem.graph.agents): A_iq_row.append(constraint_idx) A_iq_col.append(problem.get_z_idx(r, v, t)) A_iq_data.append(1) for edge in problem.graph.tran_out_edges(v): A_iq_row.append(constraint_idx) A_iq_col.append(problem.get_z_idx(r, edge[1], t + 1)) A_iq_data.append(-1) constraint_idx += 1 A_iq_46 = sp.coo_matrix((A_iq_data, (A_iq_row, A_iq_col)), shape=(constraint_idx, problem.num_vars)) return Constraint(A_iq=A_iq_46, b_iq=np.zeros(constraint_idx))
def prepare_problem(self): super(ConnectivityProblem, self).prepare_problem() # reset contraints constraint = Constraint() if self.T is None: raise Exception("Can not solve problem without horizon 'T'") if self.src is None: # all-to-sinks connectivity if sources undefined self.src = list(self.graph.agents.keys()) if self.snk is None: # sources-to-all connectivity if sinks undefined self.snk = list(self.graph.agents.keys()) if not set(self.src) <= set(self.graph.agents.keys()): raise Exception("Invalid sources") if not set(self.snk) <= set(self.graph.agents.keys()): raise Exception("Invalid sinks") if not set(self.graph.agents.values()) <= set(self.graph.nodes()): raise Exception("Invalid initial positions") # redefine master as a list to enable multiple masters if type(self.master) is not list: self.master = [self.master] # Create dictionaries for (i,j)->k mapping for edges self.dict_tran = {(i, j): k for k, (i, j) in enumerate(self.graph.tran_edges())} self.dict_conn = {(i, j): k for k, (i, j) in enumerate(self.graph.conn_edges())} # Create dictionary for v -> k mapping for nodes self.dict_node = {v: k for k, v in enumerate(self.graph.nodes)} # Create agent dictionary self.dict_agent = {r: k for k, r in enumerate(self.graph.agents)}
def _dynamic_constraint_49(problem): # Constructing A_eq and b_eq for equality (49) as sp.coo matrix A_iq_row = [] A_iq_col = [] A_iq_data = [] N = len(problem.graph.agents) constraint_idx = 0 for t, b, (v1, v2) in product(range(problem.T), range(problem.num_min_src_snk), problem.graph.tran_edges()): A_iq_row.append(constraint_idx) A_iq_col.append(problem.get_f_idx(b, v1, v2, t)) A_iq_data.append(1) for r in problem.graph.agents: A_iq_row.append(constraint_idx) A_iq_col.append(problem.get_xf_idx(r, v1, v2, t)) A_iq_data.append(-N) constraint_idx += 1 A_iq_49 = sp.coo_matrix((A_iq_data, (A_iq_row, A_iq_col)), shape=(constraint_idx, problem.num_vars)) return Constraint(A_iq=A_iq_49, b_iq=np.zeros(constraint_idx))