Skip to main content

Newman iteration foamalation

Answered

Comments

2 comments

  • Official comment
    Simranjit Kaur
    • Gurobi Staff
    This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum, or try Gurobot, our chatbot interface offering instant, expert-level support.
  • Maliheh Aramon
    • Gurobi Staff

    Hi Kim,

    Your code is a bit complicated and I cannot read through it easily. As far as I understand, you are trying to implement the following mathematical model:

    • Notation
      • \(G = (V, E)\): A graph with vertex set \(V\) and edge set \(E\)
      • \(k_i\): degree of vertex \(i\)
      • \(M\): \(2|E|\)
      • \(A_{ij}\): 1 if edge \((i,j) \in E\)
    • Decision variable:
      • \(s_i\): 1 if vertex \(i\) belongs to group 1 and -1 if it belongs to group 2
    • Objective function:
      • \(\max Q = \frac{1}{2} \sum_{ij} (A_{ij} - \frac{k_ik_j}{M})(s_is_j+1)\)

    A simple implementation is given below:

    import gurobipy as gp
    from gurobipy import GRB

    import networkx as nx

    if __name__ == "__main__":

    n = 20
    G = nx.erdos_renyi_graph(n, 0.5)
    A = nx.adjacency_matrix(G).toarray()
    k = [G.degree(i) foriinrange(n)]
    M = 2 * len(G.edges)

    model = gp.Model("modularity")
    x = model.addVars(n, vtype=GRB.BINARY, name="x")
    s = model.addVars(n, lb=-1, ub=1, vtype=GRB.INTEGER, name="s")

    # Define s_i=2x_i -1 such that if x_i=1, then s_i=1 and
    # if x_i=0, then s_i=-1
    model.addConstrs(
    (s[i] == 2 * x[i] - 1 for i in range(n)), name="connect_x_s_constr"
    )

    # Define the objective function
    Q = gp.QuadExpr()
    for i in range(n):
    for j in range(n):
    Q += (A[i, j] - k[i] * k[j] / M) * (s[i] * s[j] + 1)
    Q *= 0.5
    model.setObjective(Q, sense=GRB.MAXIMIZE)

    model.optimize()

    Best regards,

    Maliheh

    1

Post is closed for comments.