[an error occurred while processing this directive]

Task 2
Network Simulator

Specification

Version: 1.0


Index


Overview: Task 2 and the project

Task 2 is to write a simple network simulator. This will help you get familiar with a train network and how it operates. You will be able to use your network simulator to when you do the project - it will help you with testing.

The Project is to implement some functions to decide the most profitable way of routing some trains around the network. The trains collect and deliver goods and receive profit for each delivery. Your objective in the project is to run the network safely (no accidents!) and to maximise the network profit.

For the project you will work in pairs, for task 2 you will work individually.

The Train Network

The network is composed of simple pieces of track (straight and curved sections for example) and "fork" tracks (Y shaped pieces of track). Each fork has a switch (a "point") on it that connects one of the two arms of the fork to the central section. This can be switched remotely to change which arm is connected to the central section. Some of the simple sections of track have a "signal" on them which can be used to stop any train which reaches that section.

You have no control over the speed of the trains other than being able to stop them at signals. You can alter the route of a train by changing the points.

Trains pick up and deliver goods automatically (and without needing to stop!) when they reach collection and delivery points. The type of goods a train can carry depends upon the type of trucks it pulls. There are Coal trucks, Milk trucks, Oil trucks, and so on.

Files to download

There are a number of files to download for this task and the project. New files will be added over the next few weeks, and files will be corrected if we find errors in them. Check back here regularly to ensure you have the latest version of each download file.

Download the stub file Network-stub.hs and save it as Network.hs. Other than adding a header comment at the top of the file you cannot alter or add anything at or above the line containing the comment:

-- you may change lines below here only

Download the library file ThomasLib.hs. You may not alter this file. Import it in your module to access the shared types used in task 2 and the project.

Assumptions you may make about the assignment.

What you are to do

Task 2 is:
Design a type Network to represent a train network, and implement the specified interface functions for this type.

The Network Type

You are to write your own implementation for the type Network. There are many many ways to do this - this is no single correct solution. The idea is simply to make sure you store enough information in the type to be able to write the interface functions which use it.

Many people design interface types by just starting with a very simple type and move quickly on to writing the functions. You can always go back and add more to your type definition if this turns out to be needed when you are implement the functions.

As an aside: The Network type is to be abstract. That means it must be a user defined data type, rather than being a type synonym defined using type. It also means you can't export constructor for your network type - but since you can't alter the export line anyway for this task (it is protected) this will all automatically be the case.

The Interface Functions

You are to write your own implementations of the interface functions.

There will be some marks for each function. If you don't get to implement each function then just leave the provided stub definitions in for the unimplemented ones (so that your module still compiles ocrrectly with the testing software).

There are 5 functions to implement for the core part of the assignment. There is 1 extra function you can implement for bonus marks. There are also three further functions in the interface which you do not have to implement in task 2, they are for the project only.

You may include in your Network module any additional "helper" functions you need to write. These are not to be exported however.

Core Functions

  • makeNetwork :: TrackData -> [(Train, Track)] -> Market -> [Speed] -> Network

    Create a variable of type Network, as specified by the input data

  • getTicks :: Network -> Int

    Return the number of ticks that have elapsed since the simulation started.

  • getLights :: Network -> [On]

    Return the current status of all the lights on the Fat Controller's control panel in order (ie the head of the list refers to the state of light 0, and so on..).

  • sendCommands :: Network -> [Command] -> Network

    Set the state of the specified points and signals. Points and signals not included in the list of commands retain their previous setting. No two commands in the list can refer to the same point or switch.

  • nextTick :: Network -> Network

    Simulate the network one tick. Move each train the distance determined by their current speed value and the current settings for each switch and point.


    Challenge Function

    This function is slightly harder than the others; attempt it if you are feeling adventurous. It is worth 2 bonus marks (so you can get 12/10 for this assignment...)


     

    Update History
    1.01: 22-4-03 "at" changed to "by" in the definition of the getProfit.
    1.0: 21-4-03 Final Spec Released
    0.1: 15-4-03 preliminary version released
    [an error occurred while processing this directive]