Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jpihl committed May 19, 2015
0 parents commit 5824628
Show file tree
Hide file tree
Showing 18 changed files with 638 additions and 0 deletions.
58 changes: 58 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*,cover

# Translations
*.mo
*.pot

# Django stuff:
*.log

# Sphinx documentation
docs/_build/

# PyBuilder
target/
27 changes: 27 additions & 0 deletions LICENSE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Copyright (c) 2015, Steinwurf ApS

All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of kodo-simulations-python nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
9 changes: 9 additions & 0 deletions NEWS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
News for kodo-simulations-python
================================

This file lists the major changes between versions. For a more detailed list of
every change, see the Git log.

Latest
------
* tbd
18 changes: 18 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
=======================
kodo-simulations-python
=======================

The purpose of this repository is to contain the building blocks for creating
simulations using kodo. Further more the examples directory contains a set of
simulations.
The simulations can be used for testing the properties of network
coding. They are also a great starting point for users wanting to create their
own simulations.

.. contents:: Table of Contents:
:local:

Installation
============

kodo-simulations-python requires a recent installation of kodo-python.
225 changes: 225 additions & 0 deletions examples/relay_simulations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
#! /usr/bin/env python
# encoding: utf-8

import sys
sys.path.append('..')

import argparse
import kodo
import simulator
import collections


def relay_simulation(symbols,
symbol_size,
error_source_sink,
error_source_relay,
error_relay_sink,
relay_count,
source_systematic,
relay_transmit_on_receive,
relay_recode):
"""
Simple relay simulation.
The simulation has the following topology:
.-----------------------------.
| |
| +-------+ |
+ | relay | v
+--------+ +-------+ +------+
| source | +--> +-> | sink |
+--------+ +-------+ +------+
| relay |
+-------+
.
.
.
+-------+
| relay |
+-------+
"""
nodes = []

# Create source
encoder_factory = kodo.FullVectorEncoderFactoryBinary(symbols, symbol_size)
encoder = encoder_factory.build()

if source_systematic:
encoder.set_systematic_on()
else:
encoder.set_systematic_off()

source = simulator.Source("source0", encoder)
nodes.append(source)

# Create sink
decoder_factory = kodo.FullVectorDecoderFactoryBinary(symbols, symbol_size)
sink = simulator.Sink("sink0", decoder_factory.build())
nodes.append(sink)

# Create channels
source_sink = simulator.Channel("channel0", error_source_sink)
nodes.append(source_sink)
source_relay = simulator.Channel("channel1", error_source_relay)
nodes.append(source_relay)
relay_sink = simulator.Channel("channel2", error_relay_sink)
nodes.append(relay_sink)

# Connect source and sink
source.receivers.append(source_sink)
source_sink.receivers.append(sink)

relays = []
for i in range(relay_count):
# Create relay
relay_id = "relay{}".format(i)
relay = simulator.Relay(relay_id, decoder_factory.build())
relay.transmit_on_receive = relay_transmit_on_receive
relay.recode = relay_recode

relays.append(relay)
nodes.append(relay)

# Connect source and relay
source.receivers.append(source_relay)
source_relay.receivers.append(relay)

# Connect source and relay
relay.receivers.append(relay_sink)
relay_sink.receivers.append(sink)

while not sink.decoder.is_complete():
source.tick()
for relay in relays:
relay.tick()

result = {}
for node in nodes:
result.update(node.counter)
return result


def print_column(key, results):
"""Print column of data nicely."""
sum_value = 0
count = 0
max_value = 0
min_value = 0

for result in results:
if key not in result:
continue
count += 1
r = result[key]
sum_value += r
max_value = r if max_value == 0 or max_value < r else max_value
min_value = r if min_value == 0 or min_value > r else min_value

average = float(sum_value) / count

print(
"[ RESULT ] {key}\n"
"[ ] Average: {average} packets\n"
"[ ] Max: {max} packets ({max_diff} packets)\n"
"[ ] Max: {min} packets ({min_diff} packets)\n"
"[ ]".format(
key=key,
average=average,
max=max_value,
max_diff=max_value - average,
min=min_value,
min_diff=min_value - average))


def present_results(results):
"""Print out results nicely."""
def get_keys(results):
keys = []
for key_list in [result.keys() for result in results]:
for key in key_list:
if key not in keys:
keys.append(key)
return keys

for key in sorted(get_keys(results)):
print_column(key, results)
print("[----------]\n"
"[ DONE ]\n"
"[----------]")


def main():
"""Handle options and run simulation."""
parser = argparse.ArgumentParser(description="relay line")
parser.add_argument(
"--symbols",
help="Set symbols",
type=int,
default=32)
parser.add_argument(
"--symbol-size",
help="Set symbols size",
type=int,
default=1400)
parser.add_argument(
"--error-source-sink",
help="Error source to sink",
type=float,
default=0.5)
parser.add_argument(
"--error-source-relay",
help="Error source to relay",
type=float,
default=0.5)
parser.add_argument(
"--error-relay-sink",
help="Error relay to sink",
type=float,
default=0.5)
parser.add_argument(
"--relay-count",
help="Number of relays",
type=int,
default=1)
parser.add_argument(
"--source-systematic",
help="Whether the source is systematic or not --systematic=1 turns on "
"systematic source. Systematic means that all packets in a "
"generation are sent first once without coding. After sending "
"everything once coding starts",
default=True)
parser.add_argument(
"--relay-transmit-on-receive",
help="Set true if the relay(s) should transmit in every tick or when "
"a packet is received from the source",
default=True)
parser.add_argument(
"--relay-recode",
help="Set true if the relay(s) should recode packets",
default=True)

args = parser.parse_args()

runs = 100
results = []
for run in range(runs):
result = relay_simulation(
args.symbols,
args.symbol_size,
args.error_source_sink,
args.error_source_relay,
args.error_relay_sink,
args.relay_count,
args.source_systematic,
args.relay_transmit_on_receive,
args.relay_recode)
results.append(result)

present_results(results)


if __name__ == '__main__':
main()
22 changes: 22 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#! /usr/bin/env python
# encoding: utf-8

try:
from setuptools import setup
except ImportError:
from distutils.core import setup

config = {
'description': 'Kodo Python Simulations',
'author': 'Steinwurf ApS',
'url': '',
'download_url': '',
'author_email': 'jpihl@steinwurf.com',
'version': '0.1',
'install_requires': ['nose', 'kodo'],
'packages': ['simulator'],
'scripts': [],
'name': 'kodo-simulations-python'
}

setup(**config)
8 changes: 8 additions & 0 deletions simulator/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#! /usr/bin/env python
# encoding: utf-8

from channel import *
from packet import *
from relay import *
from sink import *
from source import *
Loading

0 comments on commit 5824628

Please sign in to comment.