The simplest problems we would like to solve require matrices that can easily grow larger than a few 100GB. But most values that are stored in these matrices are zeros, which makes the matrices sparse in data. If we instead use MATLAB’s sparse matrix implementation to take advantage of this sparsity, then the memory requirements drastically reduce! The function one should use to initialise such a sparse matrix is:

A = shallow(m, n, nz);

Here, one can define the matrix’s height, `m`

, its width, `n`

and the number of non-zero elements, `nz`

, can also be set. Now, instead of requiring e.g. 5083.5GB to store a single matrix, our matrix grows up to a manageable 75MB.

Nonetheless, we need to populate this matrix with data – otherwise what’s the point of having the matrix anyway? For instance, for the code above we append rows to the bottom of our matrix until all constraints have been added. Sometimes the constraints are symmetric and we can append multiple rows that contain combinations of scaled identity matrices (`eye()`

), upper triangular matrices (`triu()`

) or lower triangular matrices (`tril()`

).

It is this population and appending however, that requires **a lot of time**!

To investigate how much time is spent on preparing and actually solving our minimisation problem, I adjusted my profiling code and re-ran the MILP (Mixed Integer Linear Programming) solver in MATLAB. Results look as follows:

One can see, how the MATLAB overhead increases with the actual solving time, since this is what one might expect. After all, the more variables we need to deal with, the more time it takes to populate (i.e. *overhead*) and the more time is required to solve (i.e. *solving*). So I checked if the ratio between time spent populating and time spent solving changes for different number of variables.

Results show that the average time spent *solving* our problem highly outweighs the average *overhead* time. One might therefore believe that MATLAB is pretty good at performing elementary tasks. After all, problems that took two hours to solve required less than a second to be set up. But as you can see, MATLAB is pretty slow, which is why (at the time of ~~writing~~ updating this post) I only had 50 repetitions of up to ~~11~~ 15 days evaluated. CPLEX on the other hand was much quicker, so I started profiling its BILP (Binary Integer Linear Programming) solver and after a few days gathered the results.

The difference is astonishing! Both the *overhead* time as well as the *solving* time increase exponentially as the number of input parameters goes up. Unlike the pure MATLAB implementation however, for long datasets, the CPLEX solver is actually quicker at finding a solution than MATLAB is at setting up the problem. Now that’s what I call optimisation: well done IBM! But how is the average share of time allocated you may ask?

Here you go. When using CPLEX, the percentage of running time used to solve our minimisation problem rapidly decreases with increasing problem size. Isn’t that beautiful? But also frustrating since there is no apparent way to speed up MATLAB’s matrix manipulation mechanism. After all, I used as much of my MATLAB knowledge to avoid loops and use native functions wherever possible. Yet it is MATLAB, not the solver, that has become our bottleneck.

Nonetheless, I am complaining from a very high standard of “underperformance”. The solutions we seek are returned within 15 minutes, and that includes both *solving* time and *overhead* time. Therefore, I apologise to you for having made you read through this pointless mini-rant, and can only hope that you at least enjoyed my beautiful results that were in no way, shape or form advertisement for IBM’s awesomeness

**Updated: 27th of July 2017 – More MILP timing data**

`MATLAB`

and `Python`

. They both are easy to lean and use, and they both have powerful toolboxes and modules that extend the basic functions of `MATLAB`

and `Python`

, respectively. Nonetheless, for most initial implementations we frequently choose `MATLAB`

over `Python`

since it provides a (in my view) cleaner way to do large matrix manipulation and complex optimisation calls. Once we solved the problem on a theoretical level, we then proceed to stage two, where `Python`

accelerates the computation (especially when we deploy it on our compute cluster However, with the most recent problem we had to tackle, transition from step one to step two was not straight forward. We had to assure all components operate correctly, and that our solver could find a solution. Yet with an increasing number of input parameters, the developed procedure took more and more time to solve. In fact, the increase in time was exponential as the number of parameters increased – **not good**. So what could we do now?

Deadlines were coming up and we needed to solve problems lasting several weeks, however a solution may only be returned after several days of computation. At this hopeless moment in time, one of my colleagues suggested an amazing tool called `CPLEX`

by IBM; she really saved us, because it’s free for academics and works so much quicker than `MATLAB`

(get it here). So, if you are interested in the mathematical problem we wanted to solve, why it is difficult to solve it, how `CPLEX`

performed in comparison to native `MATLAB`

solvers, and how to acquire `CIPLEX`

and use it within your `MATLAB`

code, then you have come to the right post.

In the world of resource management for electricity and energy grids, generators need to plan ahead so that they can operate their generators when needed. Usually this is done using a forecast, yet these forecasts are inaccurate to some degree. Therefore, generators do not state how much energy they will provide, but whether their units will be active or “committed” during a certain time. Whenever units are committed, these they can provide any amount of energy within their operational limits, and the operators can plan for these periods of operation by assuring enough resources are made available. For example, if a hydro-electric plant was committed for a few hours in the future, then the operator could make sure that enough water is in its water reservoir, i.e. the resource is sufficient.

With multiple generators operating at different costs and emission levels, a solver had to be used in order to address this complex “Unit Commitment” (UC) problem. We decided to choose a “Mixed Integer Linear Programming” (MILP) approach that has a simple (yet powerful) function in `MATLAB`

called `intlinprog()`

:

[x, fval, exitflag, output] = intlinprog(f, intvars, A, b, Aeq, beq, lb, ub);

Here, we only use the following variables:

`x`

which is the output to the UC problem (math: )`intvars`

defining which values are integers (here, all values – math: )`f`

which are the costs associated to generator commitment (math: )`A`

and`b`

which define the inequality constraints (math: and )`lb`

and`ub`

which limit the solution space (to make it binary – math: and )

The mathematical formula (including the unused linear constraints and ), explaining what needs to be minimised would look as follows:

This looks rather neat and simple, **but**… The inequality constraints and for instance can become insanely large. The current average sizes are:

If these matrices were stored as simple arrays of 8bit integers, more than 600GB of storage would be needed. Luckily, `MATLAB`

‘s space matrices can handle these matrices within relative ease and eventually solve the MILP problem; but at what cost?

Solving our UC problem for a single day or two reduces the size of the arrays quite significantly. Therefore, a solution is achieved within a minute. Yet if we increase the number of days, this is the trend we observe:

Running time vs number of days (average time in red)

Note that the Y-axis has a logarithmic scale. I ran 50 MILP solves for different consecutive combinations of days and noted the time it took to solve. When reaching 16 days solving took so long that it was not feasible to continue. It looks like the time it takes to solve increases exponentially with number of days, and the spread in computation time becomes wider, too. If this trend continues (and we suspect it will) then solving for 365 days will take something between 0.63 days and 2800 years (depending on what polynomial order we extrapolate).

So we installed IBM’s `CPLEX`

solver and ran its corresponding “Binary Integer Linear Programming” (BILP) solver. Its function call uses the exact same underlying maths and is therefore surprisingly similar to `MATLAB`

‘s function call:

[x, fval, exitflag, output] = cplexbilp(f, A, b, Aeq, beq, x0, options);

Since the output has to be a binary vector, we need not restrict `x`

using `intvars`

, `lb`

and `ub`

. However, you need to specify some starting conditions in `x0`

, but that can just be a simple vector of zeros. Solving is now significantly accelerated:

Running time vs number of days (average time in red)

I only ran multiple simulations up to half a year, yet you can already see how much quicker and reliable IBM’s solver operates. In fact, running a single year took less than 12 minutes (on a virtual machine on my MacBook Pro). `CPLEX`

is therefore an amazing, “plug and play” like tool that just works; and it works really well!

As you probably know by now, `CPLEX`

is amazing. But how do you get it, and use it in `MATLAB`

? Simple… follow the link on IBM’s website:

https://www.ibm.com/developerworks/community/blogs/jfp/entry/CPLEX_Is_Free_For_Students

Then register with your student email, which must end in `@xxx.edu`

or `@xxx.ac.uk`

to assure you are an enrolled student or member of staff at university. After confirming your email address, you can download the installer for “IBM ILOG CPLEX Optimization Studio 12.7.1 – Student (CJ1HQML)” for free (12.7.1 may change based on the latest version available at the time you read this). Run the setup and you are done with the acquisition of `CPLEX`

.

In `MATLAB`

all you need to do is add the `CPLEX`

toolbox to the search path. This can be done by adding this simple line of code:

Keep in mind, that this path may be different for different versions of `CPLEX`

. To find the correct path to the `CPLEX`

toolbox, navigate to the folder into which you previously installed `CPLEX Studio`

. Then look for the `matlab`

directory, and inside it, and choose the folder of matching system architecture (in my case it was `x64_win64`

). Once you assured that the `addpath();`

function uses the correct path to this folder, you are done and good to go.

Further information can be found on IBM’s **CPLEX for MATLAB** page (for version 12.7.1: link). Now however, my break for lunch (during which I wrote this post) is definitely over, and I shall resume my work. I wish you much fun solving problems, “gerfficiently”

Imagine you have the following function call in MATLAB®:

You expect arg1 to be a string, and `arg2`

an integer to indicate how often the string in `arg1`

is to be displayed. This code is valid code and if run like so:

print_string('Hello World!', 4);

would print:

Hello World!

Hello World!

Hello World!

Hello World!

Hello World!

Hello World!

Hello World!

But what if the second argument is missing? Or what if the first one is not a string? I will show you how you can use the `exist()`

and `assert()`

function to make default values and throw an error if something went wrong.

Calling the function with an argument missing is perfectly valid in MATLAB®:

print_string('Hello World!');

Yet MATLAB® will complain, that the variable `arg2`

has not been defined. You can test for this case using the `exist()`

function and (in case the variable does not exist) assign a default value, e.g. 1:

There, now the code will run and print a single line of “Hello World!”.

Luckily MATLAB® can display a variety of datatypes using the `disp()`

function. Yet if you want to make sure that only strings (or any specific datatype for that matter) are passed, then it may be worth raising an error and stopping the code execution. The function `assert()`

is perfect for this case! It takes up to three inputs, where:

Specifies the all-clear condition

Is an error identifier

Is an error message (shown to the user)

So if we wanted to make sure, that only strings are passed as `arg1`

, then we can write:

Now when calling the function and passing e.g. `5.0`

or `pi`

for the 1st argument, then the user will receive a nice error message stating, that the 1st argument is not a string, and the execution stops. It is common convention, to use a function hierarchy specific error ID, so that it is unique and can be traced back. It is worth mentioning, that the “all-clear” condition is the test for when everything works as it should, i.e. when no error should be thrown. Before that I used to throw exceptions inside an if-statement, making the code do the exact opposite. Doing it with `assert()`

on the other hand is much cleaner.

With these two features, I could rewrite my entire PhD’s code in a very quick manner and make it much more robust in comparison to previous iterations. Now, my code is also more flexible and much neater than before. I hope this hint will serve you as well as it did serve me.

]]>2.71828182845904523536028747135266249775724709369995…

More commonly it is known as , Euler’s number. You may wonder, where does this number come from and why is it so important? Well… If you are like me that is: waiting for simulations to finish, having 20 minutes of spare time on your hands, and having a tenacious drive to understand things. So come along as I explain two different ways that result explain where this beautiful number comes from.

Rather than diving into derivatives and integrals, let me begin with an explanation that may be more comprehensible and easily relates to reality: compound interest. Interest is a percentage by which the amount of your savings in a bank increase. Imagine you are lucky and receive an interest offer of 100% per annum. This effectively means that your savings will double after every year.

Now, what if the bank offers you 50%, but after 6 months. Would you take the deal? Quick napkin-maths will show that you should definitely take that deal. In fact, you would not only double your savings, but receive 1.25 times what you already have.

Since no bank in the world would offer such extreme interest rates, let’s carry on with this slightly ludicrous thought experiment, Let’s decrease the interest rate to , which is paid every month. The formula would now become quite long, yet the result is an increase by 1.613. Rather than writing out the full chain of products I will simply raise the brackets to the power of 12:

As you can see, despite reducing the interest rate, if the payout frequency is inversely proportional, you will always receive a tiny bit more. So you might think: so why not pay out every day?

Or why not every second of the year?

Hmmm… You may think that the multiplication is stagnating, and you are right: it is approaching $:

In fact, this method is how to calculate , assuming we increase the exponent (or in our example, the interest rate) to infinity:

Now, let’s delve into the mathematical derivation of and appreciate its awesome properties. To begin, let us consider any exponential function, i.e. . If we substitute a number for , that is greater than 1, then we obtain a constantly increasing value for any given . In fact, if we were to try and plot the value of a the gradient for at any point, we would find that it looks awfully similar to the original function:

In the figure above, six exponential functions are plotted on the left, and their derivatives are sketched on the right. As you can see, they do look awfully similar, right? But let’s remind ourselves, how these derivates, or gradients, are estimated. This is done by the difference quotient, i.e. the gradient is equals to the ration of the change in over the change in :

The change in can be approximated by evaluating at two locations, as shown in the figure below.

Here,

Where,

As you can see, this approximation of the gradient at is quite bad since the chosen parameter is way too big. So let’s make it smaller and smaller. In fact, let’s make it approach zero and re-evaluate the equation:

The result is:

Which actually contains the original function , but scaled by some parameter . Now the equation is, is there a value for , which would make this scaling factor equal one? I.e.:

Well, let’s find out and re-equate this:

So now we can define as a function of as:

Here, we will divide the exponent by a number that is approaching zero, and dividing by zero is a big no-no in mathematics. So let’s substitute for and rather than reducing , let’s increase . You will see, that this has exactly the same mathematical effect, since .

And look at that! We have found the same equation for that we did in the compound interest one. Therefore, the amazing property of $is, that weather you differentiate it or integrate it, always results in itself! How cool is that!?!

]]>

First of all, you can find a complete definition of MATLAB® classes here as I am not intending to explain all, but only the most important parts in this post. I also assume that you know what a class is and won’t get confused when I speak about an properties, methods and instances. So let’s get started! A class in MATLAB® is defined by the keyword `classdef`

. Inside its definition a class has several sections in which it defines the `properties`

and `methods`

that the class provides. So an empty class might look like so:

classdef Vehicle

properties

% all your properties (i.e. 'class variables')

end

methods

% all your methods (i.e. 'class functions')

end

end

properties

% all your properties (i.e. 'class variables')

end

methods

% all your methods (i.e. 'class functions')

end

end

Now let’s add some more information to our vehicle class. First I’ll add a property holding the velocity (3d vector) of the vehicle, so nothing too complex. Also I’ll add a constructor and make a getter (function that will return the vehicle’s speed) and a setter (function that will apply the vehicle’s speed).

classdef Vehicle

properties

velocity

end

methods

function [aVehicle] = Vehicle()

aVehicle.velocity = [0; 0];

end

function speed = getSpeed(aVehicle)

speed = sqrt( sum( aVehicle.velocity.^2 ) );

end

function [aVehicle] = setSpeed(aVehicle, speed, direction)

x = speed * cos(direction);

y = speed * sin(direction);

aVehicle.velocity = [x; y];

end

end

end

properties

velocity

end

methods

function [aVehicle] = Vehicle()

aVehicle.velocity = [0; 0];

end

function speed = getSpeed(aVehicle)

speed = sqrt( sum( aVehicle.velocity.^2 ) );

end

function [aVehicle] = setSpeed(aVehicle, speed, direction)

x = speed * cos(direction);

y = speed * sin(direction);

aVehicle.velocity = [x; y];

end

end

end

Here, it is important to note that MATLAB® does **not** reference to the class at hand like some other object orientated programming languages do, i.e. by using the `this`

or `self`

pointer. That means each time you want to access a property from within the class definition you must pass the instance into the method; just like for the `setSpeed()`

method. Additionally, if you want to modify a property of the class you can do so but all changes are done within the bounds of that method. To maintain the change, you have to let the method return the updated instance and reassign it where you called the method from. Sounds confusing? Let me help you with an example:

% the Vehicle class is saved in a file titled 'Vehicle.m' in the same directory

% first define an instance of 'Vehicle' called car

car = Vehicle();

% get the current speed (should be zero)

getSpeed(car) % I intentionally left out the semicolon

% next try changing the speed

setSpeed(car, 3.0, 0);

getSpeed(car)

% somehow the change did not stick?!?

% that's because we need to reassign the returned instance

car = setSpeed(car, 3.0, 0);

getSpeed(car)

% There you go...

% first define an instance of 'Vehicle' called car

car = Vehicle();

% get the current speed (should be zero)

getSpeed(car) % I intentionally left out the semicolon

% next try changing the speed

setSpeed(car, 3.0, 0);

getSpeed(car)

% somehow the change did not stick?!?

% that's because we need to reassign the returned instance

car = setSpeed(car, 3.0, 0);

getSpeed(car)

% There you go...

That is because many things in MATLAB® are passed by value, hence preventing you from overwriting the original. Hence why you do the overwriting manually and explicitly. Yet you can change that behavior by defining a class with as a `handle`

. Then any changes to the class’s properties by any class method saves that change to the same class, just like for other OOPLs.

classdef Vehicle < handle

% All the rest of the class definition

end

% All the rest of the class definition

end

Additionally, you can define `static`

methods or class methods that are not called with a specific instance of the class. These methods do not perform operations on the individual object instance, but provide class wide functionality:

%...

methods(Static)

% static methods (i.e. 'functions') go here

end

%...

methods(Static)

% static methods (i.e. 'functions') go here

end

%...

Static methods are though called differently though:

% this calls a method on an instance (car) of Vehicle

paintVehicle(car, 'red');

% this calls a class method of 'static' method

Vehicle.updateRoadTax(71.50)

paintVehicle(car, 'red');

% this calls a class method of 'static' method

Vehicle.updateRoadTax(71.50)

As you know, classes can inherit their functionality to subclasses, and that is the case in MATLAB®, too. Here, you can define a subclass like so:

classdef Car < Vehicle

% class stuff...

end

% class stuff...

end

Additionally, you can let a class inherit from multiple superclasses like so:

classdef Hovercraft < Vehicle & Boat

% class stuff

end

% class stuff

end

Calling a superclass method is as easy as calling it from anywhere else in code. Yet if you overwrote the superclass method (e.g. `setSpeed()`

) in your subclass, you need to explicitly address the superclass function. Let me give you an example with `Car`

and `Vehicle`

.

% some class setup code...

function [aCar] = setSpeed(aCar, kmh, direction)

% Convert kmg -> speed

speed = kmh * 1000 / 3600;

aCar = setSpeed@Vehicle(car, speed, direction);

end

% more code...

function [aCar] = setSpeed(aCar, kmh, direction)

% Convert kmg -> speed

speed = kmh * 1000 / 3600;

aCar = setSpeed@Vehicle(car, speed, direction);

end

% more code...

Something similar is true for the constructor:

% some class setup code...

function aCar = Car()

aCar@Vehicle();

end

% more code...

function aCar = Car()

aCar@Vehicle();

end

% more code...

This allows you to for instance extend superclass behavior or assure values are set correctly.

Now you may wonder why to use a getter and setter if you could also access the properties from the outside using the dot-notation. Well, you can change the access to

`public`

(default): the property is accessible by everyone`protected`

: the property is only accessible by the class and and inheriting sub-classes`private`

: the property is only accessibly by the defining class itself

To change the property access simply `Access`

like so:

classdef Vehicle

properties (Access = private)

velocity

end

end

properties (Access = private)

velocity

end

end

When working with inheriting classes, you can access the superclass’s property as if it was part of the inheriting class. So you do not need to explicitly tell MATLAB® which superclass you want to access.

Anyhow, in the code below you can replace the *KEYWORD* with any of the following MATLAB® property attributes (I only listed some relevant ones…).

classdef Vehicle

properties (KEYWORD = VALUE)

% define properties...

end

end

properties (KEYWORD = VALUE)

% define properties...

end

end

`Abstract`

=`false`

(default) or`true`

: to define a root or prototype class that is never instantiated`Constant`

=`false`

(default) or`true`

: to define whether this property is a constant which is set during initialisation`Dependent`

=`false`

(default) or`true`

: to define whether a value is a computed property and not stored in memory`GetAccess`

or`SetAccess`

=`false`

(default) or`true`

: to define the getting and setting accessibility of the property`Hidden`

=`false`

(default) or`true`

: to hide the property from the property inspector and prevent it from showing up in names window`Transient`

=`false`

(default) or`true`

: to prevent the property from being stored when the MATLAB® is saved

If it were up to me I’d say you know pretty much all that is required to code with MATLAB® classes. Yet there is that one extra thing that is pretty cool and it is called `events`

. Events are like automatic methods that are called whenever something in the class happens. For instance when a parameter changes (these functions are also called *property observers*). Well, let me show you.

For one only `handle`

classes can have observers and event handlers as the observer wouldn’t know which instance to observe (remeber they are copies by default). First let’s define the event that is triggered inside our Vehicle class:

classdef Vehicle < handle

properties

velocity = [ 0; 0 ];

end

events

% Events are defined in here

Accelerating

end

methods

function aVehicle = setSpeed(aVehicle, speed, direction)

x = speed * cos(direction);

y = speed * sin(direction);

newVelocity = [x; y];

oldVelocity = aVehicle.velocity;

delta = 1;

% Here I define the event trigger by notifying the event...

notify(aVehicle, 'Accelerating', AccelerationData(oldVelocity, newVelocity, delta));

aVehicle.velocity = newVelocity;

end

end

end

properties

velocity = [ 0; 0 ];

end

events

% Events are defined in here

Accelerating

end

methods

function aVehicle = setSpeed(aVehicle, speed, direction)

x = speed * cos(direction);

y = speed * sin(direction);

newVelocity = [x; y];

oldVelocity = aVehicle.velocity;

delta = 1;

% Here I define the event trigger by notifying the event...

notify(aVehicle, 'Accelerating', AccelerationData(oldVelocity, newVelocity, delta));

aVehicle.velocity = newVelocity;

end

end

end

Now we have not yet defined the event’s data class itself that will contain the data of the event when it’s triggered. So let’s do that now. Keep in mind that such a class capturing event data must inherit from an `event.EventData`

object.

classdef (ConstructOnLoad) AccelerationData < event.EventData

properties

acceleration = 0;

duration

end

methods

function anAccelerationData = AccelerationData(oldVelocity, newVelocity, delta)

anAccelerationData.acceleration = (newVelocity - oldVelocity) / delta;

anAccelerationData.duration = delta;

end

end

properties

acceleration = 0;

duration

end

methods

function anAccelerationData = AccelerationData(oldVelocity, newVelocity, delta)

anAccelerationData.acceleration = (newVelocity - oldVelocity) / delta;

anAccelerationData.duration = delta;

end

end

Now by itself this would not do much, but you can add a listener to look out for `Accelerating`

events. You do that by attaching a listener to the just defined handler class like so:

% create a vehicle instance

car = Vehicle();

% add a listener to the car

addlistener(car, 'Accelerating', @accelerateHandler);

% then somewhere define the handler function itself

function accelerateHandler(eventSource, eventData)

fprintf('The object was accelerating with %f m/s\n', eventData.acceleration);

fprintf('The acceleration lasted %f seconds\n', eventData.duration);

end

car = Vehicle();

% add a listener to the car

addlistener(car, 'Accelerating', @accelerateHandler);

% then somewhere define the handler function itself

function accelerateHandler(eventSource, eventData)

fprintf('The object was accelerating with %f m/s\n', eventData.acceleration);

fprintf('The acceleration lasted %f seconds\n', eventData.duration);

end

How cool is that

]]>- designed the iOS app (in Swift with MVC paradigm),
- made a horribly insecure web server to track the (sometimes out of order) data, and
- displayed the entire journey (in correct order) on a Google Map on my webpage.

But for now, here are the tracking results and me *waffling* about my journey.

One might say that being the proud possessor of a car is nothing too exciting in a first world country, but I love the mobility and flexibility. I wouldn’t disagree with cyclists’ arguments that one need not drive when commuting within a city or travelling within the bounds of a town. But being weather independent, and always warm and dry are just a few benefits my beautiful car offers me – also I’d love to see one of my bicycle friends carrying my fortnightly grocery in their bike Anyhow, enough about what I do not have in the UK (a bike that is…) and onto the topic at hand: my journey home.