Category Archives: Code

Level 3-Real Time Computing (11/12)

The following are programs for the LPC2478 OEM Board, they solve the following problems

Mark: 71%

Problem 1

1. Write a program to output a triangle wave on the screen of an oscilloscope. The
frequency of the wave should be as high as possible. Determine what parameters
of your chosen method limit the frequency. How fast can you make it run ?
2. Extend the waveform generator so that a variety of wave shapes can be produced.
A triangle wave, a square wave and a sine wave must be produced by your
program, and the particular wave shape must be selected by using the navigation
keys.
3. Extend the waveform generator program so that the frequency of the wave can be
varied using the navigation keys.

Solution:

Removed by Request of University of Durham

Problem 2

An analogue to digital converter is to be used to record audio signals from a microphone,
store the results in memory and then play them back through the monitor speaker.
Write a program to capture and store data samples in memory. Think carefully about the
type of storage to use and how many samples to take. You should record for a few
seconds of time. Your program should prompt you on the screen to start recording, and
then indicate when recording is complete. It should then ask you to initiate playback.
Try varying the sampling rate and playback rate to see how you can extend the recording
time for the same number of samples without introducing unacceptable distortion.
As an optional extra you may wish to consider how you could add a volume control to
playback and how the interface with the user can be improved.

Solution:

Removed by Request of University of Durham

Problem 3

Write a program to control the speed of a motor using pulse width modulation (PWM).
Use the PWM unit in the microcontroller to output a pulse which switches on the motor for
a defined period of time. By varying the pulse width you can vary the speed of the motor.
Your final program should allow the user to select from the following:

  • • number of selectable motor speeds, including off
  • a continuously ramping motor speed, starting at a low speed and increasing steadily to the highest speed, then repeating.

You can observe the pulse width and the output of the motor rev detector by connecting
an oscilloscope to appropriate test points on the board. The REVS testpoint is connected
to the output of the rev detector. The PWM testpoint is connected to the pwm output from
the microcontroller. You can also see the switching waveform across the motor on the
MOTOR testpoint.

Solution:

Removed by Request of University of Durham

Java Networking RMI

Credit Card Server
This credit card server offers the ability to verify a Credit card number is genuine,

Check the limit of a credit card,

Get an authorisation code for a Credit card to pay for a set amount,

Check authorisation code is valid for a given credit card and amount.

Checkout (String authcode, String cc,double amount)

Book Store Server

The Servers offers the ability to List Books()

It is also possible to remotely verfiy a user

In addition you can also make a final purchase of the items in your basket, with an authorisation code and a  credit  card

Book Store Client

help à help

lstb à list books

addb à  add book

remb à  remove books

ckot à  checkout

bskt à  display basket

cnct à  connect to credit card, and book servers

To run

Credit Card Server

java -Djava.security.policy=wideopen.policy -jar BookStore-CreditCardServejar

BookStore Server

java -Djava.security.policy=wideopen.policy -jar Booke-Server.jar

BookStore Client

java -Djava.security.policy=wideopen.policy -jar BookStore-Client.jar

MARK 1st -83%

Removed by Request of University of Durham

SA- Health Informatics Visualization

The use shaping the data into Male/Female figures this allows the data to label its self allowing the ’Ink’ to serve multiple purposes’. Use of Male/Female allows user to get a fast impression of the data or  ‘gist’ (Ware).  The totals are represented as heights  of the Male/Female figures, and the Male/Female Percentages as the heights of the dark shades of the pink & blue, Not Area(Tufte). This means the Ink  showing  4 pieces of data Time, Gender, Total & Percentage(Tufte) Use only half Male/Female shapes to reduce duplication, and allow easier comparison between Male& Female half’s.(Ware)

Consistent use pink and blue to represent male and females, allowing the user to intake information faster(Ware), and also makes sure the graph shows design variation rather than design variation.(Tufte)

The user can switch on Data labels to get exact percentages and totals  to defeat graphical distortion and ambiguity(Tufte) , The user can switch on Data Sets on & off to allow comparisons of only percentages or only totals(Tufte)

I have kept all the numbers for each Male/Female Figure within 1sq inch this allows the user to pick them out in a small number of ‘passes’ with there eye(Ware)

Possible lie factor due to population growth  4.13 % from 1999 – 2009 (UK Census Data)

Using figure height allows user to get over perception of the data without detailed study.(Ware)

Other than Labels(which can be turned off) all Ink is Data Ink, thus this visualisation

The Graphic Displays 4 dimensions of data (Time, Gender, Total & Percentage) in 2 Dimensions

The Graphic Displays 3 dimensions of data (Waist Size, Gender, Illness) in 2 Dimensions (Tufte)

The percentage for Non Raised Waist Circumference(NRWC)  , Raised Waist Circumference(RWC), and Population Total (AVG), are draw as circles with the radius proportional to the percentage. This introduces a possibly for graphical distortion and ambiguity, In attempt to reduce this I have layed the circles on top of one another(Ware) , this focuses the user on the gap between there sizes , and thus the liner difference in radius, rather than comparing their area (Tufte)

By placing the sets of circles in a two dimensions grid, and keeping a constant scale the user may pan their eye across to make not just comparisons between Waist Size, but Gender and Illness

The user can switch on Data Sets on & off to defeat graphical distortion and ambiguity(Tufte)

Software Applications AI Search Assignment

Some Attempts at solving the traveling salesman problem with AI Search

Having looked at the possible ways of solving the travelling salesmen problem it was my intention to use implementations that use simulated annealing and Genetic Algorithms, as they appeared to be the best options looking at the problem from varying levels of intelligent choice.

>Initial Work on simulated annealing

In my work on simulated annealing I initially experimented with variation of the thread / Crystal count and the maximum temperature of the second annealing. From this I found that using a large amount of threads produced better results, though when looked at, all this is throwing more and more attempts at problem until a better solution is found, which though we must admit works, and will require an exponential increase in computing power in relation to results.

When adjusting the maximum temperature of the second annealing I found that higher temperatures made small consistent positive difference to fitness of tours.

>Initial Work on Genetics

My initial work on Genetics suffered problems in that my solutions would plateau far above an optimal solutions. In an effort to solve this I tried to give it a ‘head start’ by constructing tours  Intelligently before feeding them into the population , this worked exceptionally well as it could produce an optimal or  close to optimal tour (around 49k for file 535)about in ~0.06 seconds. However this then caused significant problems in genetics as in an attempt to diversify the population it destroy the patterns within these close to optimal tours and within 20 generations or so would return to level of the original plateau. This was solved by creating a more intelligent mutation and breeding algorithms designed for maximum pattern preservation within the tour, and convergence on a local optima.

My new mutation algorithm works by attempting to break the pairs of cities with the greatest distances between them.  So the pair with longest distance AB and second longest distance  XY swapped  iff AVG(D(AY),D(YC),D(XB),D(BZ))<AVG(D(AB),D(BC),D(XY),D(YZ))
D(AB)=Distance between A and B
AVG=Average.
If my algorithm cannot find a pair AB XY to swap such the above condition is satisfied then will attempt a number of random order reversals of sub tours within the tour, and pick the fittest of these to return to the population, but not affect the tour designated to be mutated.

My new breeding algorithm in an attempts to preserve patterns by taking a length(35-50%) of the tour from each parent and comparing the fitness of each subsection, and adding  the fittest sub section to a child repeating this until we have a child which has a complete tour, It produces around 20 children like this and returns the fittest of them to the population

>Final Simulated Annealing Algorithm

My final Simulated Annealing begins by constructing semi-optimal tours then passing them to Crystals/Threads. It runs with 10 concurrent threads, updating to a synchronized HashMap located in the Annealer Class which always them to dynamically update how fit they are relative to other thread this is then passed to annealing schedule with from a produces from this input and on the current stage of the annealing schedule a temperature.

Based on this temperature a crystal with ether adds a city to the tour, remove a city from the tour, reorder the parts of the city or do nothing

>Final Genetics Algorithm

My final genetics algorithm constructs 10,000 possible tours as this only take about 6 minutes and takes the best pre constructed tour as the base of its population. It then forms a population of clones of this tour each with different mutation to provide local genetic diversity as soon as possible.

The Population is run though a number of peaks controlled by a growth factor, so that the population should vary though the peak as a bell curve

Each peak it made out of cycles where each cycle is composed of in order Deaths, Mutations, and Breeding at

After all the cycles have finished the population is quick sorted and the tour with the highest fitness is output.

>Comparative analysis
In comparing the two it is clear from the results that the genetics produces better results.

However getting the best results may not be the only criteria on which base evaluation, as for example the simulated annealing ran in ~ 15 minutes on file535 much less than the ~1hour and 25 minutes  required for  the genetics to run. In commercial applications of software computer time is expensive and time is valuable so quick and cheap, less than perfect results often more valuable than slow, expensive and close to perfect/perfect results. Route planners are a good example of this being close to the Traveling salesman in nature. Many  commercial  pieces of software produce less than optimal routes as can been seen by sometimes making small Intelligent adjustments as the user and producing a shorter/ more optimal solutions. However they run fast, you do not want to sit around all day waiting for a solution that can save you an extra 2 miles, getting it right now and also close optimal is ‘good enough’. It can be also argued that the non-deterministic nature of these algorithms makes optimizing their running times more important as its gives them a greater degree of reliability.

>>Intelligent vs. Non-Intelligent

For the purposive of the following I will define an algorithm as Intelligent if it makes its actions based, at least partially on a value (A fitness function) of a set of data.

In their pure form and by my definition, Simulated Annealing is Non-Intelligent as it makes random realignments, and genetics performs all actions based on the preserved fitness of a solution.

Fitness allows in many ways allows better decisions to be made about the next action to be made on a set of data including weather to perform an action or if to reverse a previous action made. However this also comes with several problems the first and most obvious of these is the cost of calculating the fitness in the genetics used here the FitnessFactor Function is O(n2) which means for file535 a cost of around 148k calculations per call . This can be solve to some degree with estimated fitness functions of reduced complexity, or by storing the fitness and only updating it when the data is changed, but with these you may not get as accurate results. The second main problem is convergence towards local optima i.e as fitness is always relative so if a set of data starts to converge towards to fitness the algorithm maybe be unable to move it towards a more global optima, as it designed such that it does not allow actions which have a negative effect upon that data’s fitness but may prove important to a global optima.

In respect of my algorithms introducing a small amount of decision making into my Simulated annealing in the form of its re-order parts function which mimicked that of my genetic algorithms mutate function, allowed for large improvement in results.

Drawing from this it would seem that an algorithms resource consumption vs  results rely heavily on the amount in which it is called to make intelligent decisions, the more complex the decision making functions the more resources are required but the more likely you are to make a possible step towards a optimal solution.

It would also appear that that making positive decisions is more important in large the sets of data, as the number of solutions increases factorially with size, but for most problems there is only one or a few optimal solutions, so for larger and larger sets of data it become less and less likely that an algorithm will stumble upon the correct solution or ‘path’ to it.

Thus in conclusion I would be my believe that simulated Annealing is a the most useful tool if the search space or available resources are small, but genetics will fare better with large state spaces, were resources are not at a premium.

Mark-1st-80%

Code:-

Creative Commons License
Software Applications AI Search Assignment by Gwilym Newton is licensed under a Creative Commons Attribution-Share Alike 2.0 UK: England & Wales License.
Based on a work at personal.vacau.com.

Java 3d Wind Turbine Simulator

Project to create a Java3D program that draws a simple horizontal axis wind turbine with rotating blades.  with  a 3D button to allow you to interactively change the direction of the wind and therefore rotate the blades and turbine head about the vertical axis.

User Guide

>To open make sure that the data folder is in the same directory as the Jar File.
>To rotate the View, press down on the left mouse button and move the mouse to the left or right.
>To zoom in roll the mouse wheel forwards, to zoom out roll the mouse wheel backwards
>To Increase the Wind-Speed click on the + Button
>To Decrease the Wind-Speed click on the – Button
>To move Turbine Head right click on the -> Button
>To move Turbine Head Right click on the <- Button

Mark  1st -95%

Creative Commons License
Java 3d Wind Turbine Simulator by Gwilym Newton is licensed under a Creative Commons Attribution-Share Alike 2.0 UK: England & Wales License.
Based on a work at personal.vacau.com.

Computer SystemsII SearchEngine Server-Client

This is my Solution to the Computer SystemsII Search Engine

>Technical Features
>>Servers

  • SE-Server constantly listens thought the ‘Server Listener’ Class for connections from other servers, when a connection is revised a server worker is spawned, and the socket passed to the worker, which then fulfils any requests made by the connected server. This allows the
  • Listener to keep accepted more connections.
  • SE-Server maintains a list of all know running servers.
  • SE-Server searches for additional local servers every 60 seconds
  • SE-Server announces its presence to other servers every 60 seconds
  • On a server with infinite resources, would be able to handle an infinite number of server connections.
  • Allows the addition of an extra running server or the deletion of an existing running server
  • dynamically while the multi-server system is running.

>>Clients

  • SE-Server constantly listens thought the ‘Client Listener’ Class for connections from clients when a connection is revised a client worker is spawned, and the socket passed to the worker, which then fulfils any search requests made by the connected client
  • On a server with infinite resources, would be able to handle an infinite number of client connections.
  • When a search request is sent the client program either transmits the information of the matched website back to the client program for display, or sends the client program a error message if a matched website does not exist.
  • If the Client Worker cannot find the keyword, it will send a search request to all other servers on the list of all known running servers.

>>Database

  • A set of  records, where each record is formulated by two data fields: 1)Website Object containing website name, and website URL  and 2) a short set of keywords describing of the website
  • Database stored locally to search server.
  • Can backup Database to .sdb files
  • Can load .sdb files and merge into database

>>Shutdown

  • On shutdown , the ‘Shutdown’ thread gets passed to JVM though the Runtime.getRuntime().addShutdownHook();
  • The ‘Shutdown’ thread sends message to all other servers  a copy the server’s database, to all other servers on the list of all known running servers.

>>Updater
Every 60 seconds the updater

  • SE-Server searches for additional local servers.
  • SE-Server announces its presence to other servers.
  • Performs maintenance on the list of all know running servers, removing duplicates instances of itself, loop back addresses and inactive servers.
  • Requests copies of the databases of all know running servers, and merges it into the server’s own database.

Mark: 1st – 84%

Creative Commons License
Computer SystemsII SearchEngine Server-Client by Gwilym Newton is licensed under a Creative Commons Attribution-Share Alike 2.0 UK: England & Wales License.
Based on a work at personal.vacau.com.

1 2