(Integer) Linear Programming in Python

Step one:

brew install glpk
pip install pulp

Step two:

from pulp import * 
 
prob = LpProblem("test1", LpMinimize) 
 
# Variables 
x = LpVariable("x", 0, 4, cat="Integer") 
y = LpVariable("y", -1, 1, cat="Integer") 
z = LpVariable("z", 0, cat="Integer") 
 
# Objective 
prob += x + 4*y + 9*z 
 
# Constraints 
prob += x+y <= 5 
prob += x+z >= 10 
prob += -y+z == 7 
 
GLPK().solve(prob) 
 
# Solution 
for v in prob.variables():
    print v.name, "=", v.varValue 
 
print "objective=", value(prob.objective)

In the documentation there are further examples, e.g. one to minimise the cost of producing cat food.

Things that are visible from space, the Garzweiler Surface Mine

I was looking at arial photos of north-western Europe in Google Maps when I noticed a big white dot on the map!

I thought, what the hell? To satisfy my curiosity I decided to zoom in for further investigation.

It turns out that the big white dot is a giant surface mine. The 48 km² mine is operated by RWE and used for mining lignite, also known as brown coal.

Fun fact: 50% of Greece’s power supply and 27% of Germany’s comes from burning lignite. Lignite also has innovative uses in farming and drilling.

Isn’t the geometric juxtaposition of farmland, urban area and surface mine quite enchanting? To get a sense of the scale, take a look at the size of cars next to the big heavy machine; then try to find the big heavy machine on the zoomed out image.

Here is a video that displays the grotesque beauty of the place…

Create a European city map with population density

Datasets:

– Urban morphological zones 2000 (EU): https://www.eea.europa.eu/data-and-maps/data/urban-morphological-zones-2000-2
– Population count (World): http://sedac.ciesin.columbia.edu/data/set/gpw-v4-population-count-rev10/
– Administrative regions (World): http://gadm.org/

The map is European since the “urban” data from the European Environmental Agency (EEA) only covers Europe.

Caveats

The UMZ data ended up in PostGIS with srid 900914. You can use prj2epsg.org to convert the contents of a .prj file to an estimated SRID code. In this case the UMZ .prj file as the contents:

PROJCS["ETRS89_LAEA_Europe",GEOGCS["GCS_ETRS_1989",DATUM["D_ETRS_1989",SPHEROID["GRS_1980",6378137,298.257222101]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]],PROJECTION["Lambert_Azimuthal_Equal_Area"],PARAMETER["latitude_of_origin",52],PARAMETER["central_meridian",10],PARAMETER["false_easting",4321000],PARAMETER["false_northing",3210000],UNIT["Meter",1]]

Which translates to 3035 - ETRS89_LAEA_Europe.

How to create a world-wide PostgreSQL database of administrative regions

The GADM database contains geographical data for administrative regions, e.g. countries, regions and municipalities. As always, once you have the data in the right format, it is easy to import it into a database. The data is available from GADM in several formats. All data has the coordinate reference system in longitude/latitude and theWGS84 datum.

Step-by-step:

  1. Download data for the whole world or by country. For a change, I will use the GeoPackage format.
  2. Create a PostgreSQL database (assumed to exist)
  3. Import the data with ogr2ogr (see instructions below)

Import data instructions

Download data (example for Denmark):

wget http://biogeo.ucdavis.edu/data/gadm2.8/gpkg/DNK_adm_gpkg.zip
unzip DNK_adm_gpkg.zip

Next, create a database called “gadm” on my local PostgreSQL server; of course you can use another name if you prefer. Install the PostGIS extension:

create extension postgis

Finally, use ogr2ogr with the GPKG (GeoPackage) driver to import the data:

ogr2ogr -f PostgreSQL "PG:dbname=gadm" DNK_adm.gpkg

Now the data is imported an ready to be queried.

As a test, we can query the adm2 table (municipalities) with a coordinate inside the municipality of Copenhagen, Denmark.

SELECT name_2, ST_AsText(wkb_geometry)
FROM dnk_adm2
WHERE ST_Intersects(ST_SetSRID(ST_Point(12.563585, 55.690628), 4326), wkb_geometry)
-- AND ST_Point(12.563585, 55.690628) && wkb_geometry

You can view the selected well-known string geometry (WKT) in an online viewer, such as openstreetmap-wkt-playground. Other viewers are listed on stackexchange.

Alternative sources

For this post I really wanted a dataset of populated/urban areas. However, the GADM data I downloaded only contains adm0-adm2, which is a tessellation of the land area, i.e. cannot be used to discriminate between urban and rural areas.

Other data sources are listed below:

– http://www.naturalearthdata.com/downloads/
– https://data.humdata.org
– https://freegisdata.rtwilson.com/

From the rtwilson list, here are some specific datasets that indicate population density and urbanism:

– http://sedac.ciesin.columbia.edu/data/collection/gpw-v4/sets/browse
– https://www.eea.europa.eu/data-and-maps/data/urban-morphological-zones-2000-2
– http://www.worldpop.org.uk/ (does not cover Europe and North America)
– https://nordpil.com/resources/world-database-of-large-cities/

How to assess computers on your local area network

I teach children how to programm and do other things with technology in an organisation called Coding Pirates in Denmark, which aims to be a kind of scout movement for geeks. A best seller among the kids is learning how to hack and I see this as a unique opportunity to convey some basic human values in relation to something that can be potentially harmful.

Yesterday, I and one of the kids played with nmap, the network surveying tool, to investigate our local area network. The aim was to find information about the computers that were attached, such as operating system, system owner’s first name (often part of the computer name) and whether any computer had open server ports (SSH, web etc.). We used nmap in combination with Wireshark.

  1. Tell another person about a fun website (any website will do)
  2. Use wireshark to detect the IP address (e.g. 192.168.85.116) of any computer that accesses that website
  3. Use nmap to scan the IP address we found: nmap -vS 192.168.85.116

We also learned how to detect that someone logs into your computer and e.g. kick the person (assume an Ubuntu host):

# Monitor login attempts
tail -f /var/log/auth.log
# See active sessions
who
# List remote sessions
ps fax | grep 'pts/'
# Kill sessions
kill -9 [pid of bash processes connected to session]

Other tricks

List all hosts (ping scan) on your local area network:

nmap -sP 192.168.1.*

Find computers on your local area network that run an SSH server:

nmap -p 22 --open -sV 192.168.1.*

Urban Mining – Platinum from Road-side Dust

These guys found that road-side dust contains 1000 DKK worth of platinum per 1 ton of dust.

Cody explains that this concentration of platinum equals the amount found in good-quality platinum ore.

Does the stretch of road matter?

In a word, yes. The user Jafromobile commented that a car is likely to eject platinum particulates into the exhaust stream when the catalytic converter is the hottest, when the exhaust system is under pressure, and both of these conditions are basically achieved at the same time. In other words, at full throttle.

Where do cars use full throttle? In an urban area it could be at traffic lights. Other sections of road might in a similar manner reveal consistently-high platinum deposits.

Urban Mining – Steel Scraps

While I intuitively like the concept of Urban Mining – we have all these precious raw materials within easy reach of robots – I know nothing about it. I want to remedy that with the best ignorance cure of all: YouTube! Please, see an exteneded list of videos at the bottom of this post.

The rest of the post will focus on steel recycling to understand the problems and potentials (with regards to AI and robotics) in this industry.

ELB in Duisburg:



Hammel metal separation:

The steel scraps produced in the recycling videos go to steel plants. To see how important this raw material is, check out Thyssen-Krupp‘s product website.

Important problems

Here is a list of problems that are specific to steel recycling — as shown in the videos from ELG.

Pollution

Perhaps the elephant in the room is that recycling can be a very toxic process. This gives an edge to countries with more relaxed regulation, e.g. certain Asian countries. For example, to extract gold from electronics you may use cyanide. If robotics are more pervasive in robotics, this could eliminate the edge of low-regulation countries as recycling can be done in pure machine environments.

Buying

The scrap buyer at the German company (ELG in Duisburg) must buy deliveries of scrap metal on a daily basis and at the right price. For example, nickel can be particularly expensive. She will sometimes go out of the office to buy even small quantities of steel from scrap dealers, and always carry a magnet in her pocket to test the quality.

The dealer has to sell her the steel at a loss because he is running low on space! However, since many competitors also wants to buy the scraps, the buyer has to offer fair prices and have the dealers interest in mind.

Analysis

The scrap company must examine the quality of a shipment (e.g. steel) before it can be sent to a plant. The process involves cutting out samples, melting the samples and finally determining the alloying elements (e.g. molybdenum) it contains. High-grade steel must have the right mix of non-iron elements. Furthermore, the scrap should not contain toxis elements, such as heavy metals.

Sorting!

A shipment of steel scraps may contain copper or zink parts. These metals would contaminate the steel melt later on, so they have to be picked out of the pile before shipping to the steel plant. Workers at a German plant manually sort through the pile and test the metal with a magnet!

Cutting!

Big pieces of scrap larger than 500 kg have to be cut into smaller pieces, otherwise they won’t fit in the furnace of the customer. ELG uses a big crane fitted with a giant claw to cut big pieces of sheet metal (e.g. from dismantled factories) into smaller pieces.

Sometimes ELG uses external companies to reduce the piece size for them.

Transportation

Another problem that is exposed in the video is that of transportation. A delayed shipment of scrap metal to a steel plant can cause critical downstream delays in the supply chain. In the video, the recycling plant receives too few rail cars into their on-sight train yard (how cool is that!). They have to act fast in order to get the shipment out the door, which means they have to get more train cars fast. Perhaps an alternative would be to ship the metal using self-driving train cars that can be summoned on demand or some other sci-fi solution.

Making new steel

The scrap is melted into new steel in an electric arc furnace at the plant. The furnace uses a lot of electricity! At the plant, the composition of the scraps is analysed again using a randomly selected sample of the shipment. The balance of chromium, nickel and phosphorus to iron has to be within certain bounds.

Robotic potential

How could robots solve the problems outlined above?

Additional video material that relates to Urban Mining

Some of these videos are about extracting gold from electronics.







How to do backpropagation in Numpy

I have adapted an example neural net written in Python to illustrate how the back-propagation algorithm works on a small toy example.

My modifications include printing, a learning rate and using the leaky ReLU activation function instead of sigmoid.

import numpy as np
 
# seed random numbers to make calculation
# deterministic (just a good practice)
np.random.seed(1)
# make printed output easier to read
# fewer decimals and no scientific notation
np.set_printoptions(precision=3, suppress=True)
 
# learning rate
lr = 1e-2
 
# sigmoid function
def sigmoid(x,deriv=False):
    if deriv:
        result = x*(1-x)
    else:
        result = 1/(1+np.exp(-x))
    return result
 
# leaky ReLU function
def prelu(x, deriv=False):
    c = np.zeros_like(x)
    slope = 1e-1
    if deriv:
        c[x<=0] = slope
        c[x>0] = 1
    else:
        c[x>0] = x[x>0]
        c[x<=0] = slope*x[x<=0]
    return c
 
# non-linearity (activation function)
nonlin = prelu # instead of sigmoid
 
# initialize weights randomly with mean 0
W = 2*np.random.random((3,1)) - 1
 
# input dataset
X = np.array([  [0,0,1],
                [0,1,1],
                [1,0,1],
                [1,1,1] ])
# output dataset            
y = np.array([[0,0,1,1]]).T
 
print('X:\n', X)
print('Y:\n', y)
print()
 
for iter in range(1000):
 
    # forward propagation
    l0 = X
    l1 = nonlin(np.dot(l0,W))
 
    # how much did we miss?
    l1_error = y - l1
 
    # compute gradient (slope of activation function at the values in l1)
    l1_gradient = nonlin(l1, True)    
    # set delta to product of error, gradient and learning rate
    l1_delta = l1_error * l1_gradient * lr
 
    # update weights
    W += np.dot(l0.T,l1_delta)
 
    if iter % 100 == 0:
        print('pred:', l1.squeeze(), 'mse:', (l1_error**2).mean())
 
print ("Output After Training:")
print ('l1:', np.around(l1))

My Dad’s Early Life Story

In this post I will give a brief overview of my father’s (Georgios Kefaloukos) many activities in the early part of his life. After dinner today I decided to ask him about his life story, and took as many notes as I could. My writing may seem like a glorified enumeration of his many occupations with only sparse attention payed to artistic style.

A young carpenter and waiter on Kalymnos
When my father was 13 years old, he lived alone with my grandmother in Pothia on the Greek Island of Kalymnos. My grandfather had left the family a few years earlier for reasons I don’t really know. As a young teenager my father worked as a carpenter in the summer. He also served coffee and cake to customers in the cafe that was located in the hotel in the harbour where my grandmother worked as a cleaning lady. Needless to say, they did not have a lot of money.

From petroleum lamps to electric light
As a 14-year-old teenager my father worked as an electrician apprentice in Pothia. The island had just started transitioning from petroleum lamps to electrical lighting and many houses needed electrical installations. This meant that it was easy to get work.

Blasting mountain roads with dynamite
As a 16 year old, my father helped build the winding road that runs along the sea from Pothia to Vathi. The work partly consisted of blasting the mountain away with dynamite. Before the road was built, you had to travel from Pothia to Vathi by ship or walk over the mountain. In the old days, horse carts would carry passengers into the valley where fruits and olives are grown.

With the money that my father earned from the roadworks, he purchased a “naftologio” for perhaps 1,000 drachmer (amount from memory), which gave him the right to work as a sailor in the commercial navy of Greece. In Greece, it was, and partially still is, required to pay for an expensive licence in order to many types of work, including being a cab driver or sell cigarettes on the street. I have heard that such licences can even be passed down from one generation to the next.

Ahoy, sailor!
During the winter of my father’s 16th year, he worked as a cook for a couple of months on board the ship Manos. The ship was a small freight carrier that transported goods between the Greek Islands and also the Greek mainland. While he sailed on this ship, my grandmother and grandfather moved back together on Rhodes, after many years of separation. They lived in Ippodamou 44 in the old town of Mandraki. Because he dismantled the ship so soon, the captain was not very pleased with him.

In the summer before he turned 17, he worked on the cruise ship Ellas that sailed in the Eastern Mediterranean. The ship visited ports in Beirut (Lebanon), Limasol (Cyprus), Alexandria (Egypt), Rhodos (Greece), Piraeus (Greece), Corfu (Greece) and Venice (Italy), were the wealthy tourists disembarked to explore the historical cities.

Hatha Yoga and spirituality on Rhodos
After his first stint as a sailor, my father became hippie and settled for a while on Rhodos where he studied yogawhile living at home. Perhaps the Yoga and his search for an identity prompted his idea to become a monk.

Monk on Athos
At the age of 17, my father met Nektarios, a monk-priest from Kalymnos, in Athens. He told him that he would like to become a monk and they agreed to live together in Agios Minas one of the most settlements on Athos – situated at he very tip of the peninsula. My father lied to my grandmother and told her that he would sail with the ship Anna-Maria, named after the Danish princess, but instead he secretly travelled to Athos to meet with Nektarios. Upon arrival he sent my grandmother a letter from Athos and told her the truth. He lived for about 1 year in Agios Minas. Later he would return many times to Athos and learn the art of icon painting.

In his later days, he has travelled frequently to Athos and toured with several Danish celebrities and TV-presenters, including the cook and entrepreneur Klaus Meyer and the journalist Hans Bischoff.

Electrician again
After his first stay as a monk (novice) on Athos my father would return to work as an electrician on Rhodos, mostly in hotels. At this time he was 18 and 19 years old. Soon he would join the navy as part of his military duty.

From special forces to ordinary marine
As a 20-year-old my father spent 28 months serving his military service in the Navy. The military junta ruled Greece in those years. Initially, he trained as a frogman, but soon had to transfer to other duties for political reasons. My grandfather was a communist at the time, and the junta didn’t allow sons of black-listed people to serve in the special forces. He transfered to the destroyer Aetos (Αντιτορπιλικο Αετός), which is featured in the Greek movie Alice in the Navy. The movie features the actress Aliki Vougiouklaki, with whom the Greek Prince Konstantinos allegedly had a brief love affair. During the final months, my father served onboard a mine sweeping vessel.

Archaeologist assistant
???

Sailor again on the seven seas
At the age of 22, my father returned to Rhodes. After working once again as an electrician, he would take work aboard the freight ship Atlantic Star. To rendevouz with the ship he flew from Athens to Tokyo, Japan and waited a couple of weeks for the ship to arrive. To get to the right port, he would travel through Japan on board the high-speed trains from Yokohama, Osaka to Nagasaki. On-board the Atlantic Star he sailed from Japan to Perth, Australia and from Goa to Cape Town.

On other ships my father would sail to Sierra Leone, New York, Boston, Murmansk, Northern Norway, Montpellier, Casablanca, and Liverpool. During a trip from Murmansk to Norway, a welder from Rhodos had accidentally opened a big hole in the hull of the ship, which took in a lot of water and listed dangerously in the freezing artic waters.

Drummer in Greek night clubs
At the age of 24, he returned to Rhodes and played drums, guitar and bass at the nightclubs there. It was during this time that he met my mother, who by the way is from Finland.

Black belt in Denmark
As a 28-year-old my father moved to Denmark with my mother. They lived at Saxhøjvej in Valby, Pension Norden in Nørrebro, Rosenørns Allé and Danas Plads on Frederiksberg and finally Lupinvej in Vanløse. In those early years in Denmark he trained Shotokan karate on Amager where he got the black belt.