GithubHelp home page GithubHelp logo

dpaleka / cross-entropy-for-combinatorics Goto Github PK

View Code? Open in Web Editor NEW

This project forked from zawagner22/cross-entropy-for-combinatorics

15.0 0.0 0.0 678 KB

Code accompanying the manuscript "Constructions in combinatorics via neural networks and LP solvers".

Python 100.00%

cross-entropy-for-combinatorics's Introduction

cross-entropy-for-combinatorics

Code accompanying the manuscript "Constructions in combinatorics via neural networks" by A Z Wagner.

The following code allows you to optimize any score function on the space of graphs with a fixed number of vertices.

Demo

On the left we see a terminal with the function values and adjacency values. On the right we see the best graph in each step. The algorithm learns to generate graphs with low proximity + distance eigenvalue, as in Conjecture 2.3. in the paper.

Installation instructions

Clone this repository as follows:

git clone https://github.com/dpaleka/cross-entropy-for-combinatorics
cd cross-entropy-for-combinatorics

Install conda on your system, and run

conda env create --file environment.yml 
conda activate cross-entropy-for-combinatorics

How to use this for a custom score function?

Replace the score_graph method in score.py with the function you want to maximize.

For example, to minimize the absolute ratio of the first and the last eigenvalue of the adjacency matrix of a connected graph, put this in your score.py file:

def score_graph(adjMatG, edgeListG, Gdeg):
    """
    Reward function. The arguments adjMatG, edgeListG, Gdeg are numpy arrays.
    """
    N = Gdeg.size
    INF = 100000

    _, conn = bfs(Gdeg,edgeListG)
    if not conn:
        return -INF
        
    lambdas = np.flip(np.sort(np.linalg.eigvals(adjMatG)))
    return -abs(lambdas[0]/lambdas[-1])

Check the parameters at the top of optimize.py, in particular the number of vertices N. Then run

python optimize.py

Of course, this will just generate connected bipartite graphs after a few iterations.

Improving performance

If the generated graphs are not improving with regards to your score function, there are some straightforward directions to try:

  1. Modify the parameters at the top of optimize.py. In particular, increasing the super_percentile parameter forces more exploration, and decreasing the LEARNING_RATE parameter can get you out of local optima.

  2. Use a more powerful model to parametrize the graph generation. See You et al., 2018 (it has code for the Graph RNN) or the permutation-equivariant Li et al., 2018.

  3. Change the loss function. There is a variety of policy gradient algorithms to choose from.

cross-entropy-for-combinatorics's People

Contributors

dpaleka avatar zawagner22 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.