Giter VIP home page Giter VIP logo

optimizing-debts-repayment's Introduction

Optimizing Debts Repayment

Using Operations Research to minimize transactions in a debt network

Build codecov

Problem

Given a set of people who owe money to each other (graph), how to simplify the repayments so that the number of transactions is minimal.

Solution

Step 1 (According to your problem)

  • For each user (node) in the graph, sum the money they are owed (in-edges, positive value) and the money they owe (out-edges, negative value). This will mean you have a balance (positive/negative) for each person.
  • Separate those who owe/pay (pay) from those who are owed/ get money back (get).
  • Convert all negative debts to positive values in pay.
  • Assert that sum(pay) == sum(get).

Step 2 (Minimize transactions)

Feed the pay and get variables into the minimize_transactions(pay, get, decimal_places=2, verbose=False) function.

By default, it is assumed your values have two decimal places of precision, but that can be changed with the decimal_places parameter.

To see optimization execution statistics, set verbose=True.

The returned result is a list of (payer_index, getter_index, amount) for each transaction required to zero-out all debts.

Example

1st install ortools with pip install ortools or pip install -r requirements

from src.minimize_transactions import minimize_transactions as mt

# Owers: 11, 9.5
# Getters: 9.5, 7, 4
mt([11, 9.5], [9.5, 7, 4], verbose=True)

>>> Solve status: OPTIMAL
>>> Optimal objective value: 3
>>> Statistics
  - conflicts : 0
  - branches  : 13
  - wall time : 0.004604 s
  
>>> [(0, 1, 7.0), (0, 2, 4.0), (1, 0, 9.5)]

Disclaimer

This problem is actually an example of a large set of problems relating to network flow assignment, namely:

Another good reference to understanding this kind of problems is Settling Multiple Debts Efficiently: An Invitation to Computing Science.

So, the presented solution can be used for other problems too, you just need to be able to do that "conversion effort". This case was presented as an example on purpose, as I believe it is actually easier to learn the logic and port it with examples rather than abstractions.

optimizing-debts-repayment's People

Contributors

msramalho avatar

Stargazers

 avatar

Watchers

 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.