Sunday, May 5, 2013

Prolog resources

Getting started

An introduction to logic programming through Prolog
An introduction to logic programming through Prolog was published by Prentice-Hall in 1996 but is now out of print. The author kindly provides a pdf of the text here:
http://spivey.oriel.ox.ac.uk/corner/Logic_Programming

Learn Prolog Now! website
Online
http://www.learnprolognow.org/lpnpage.php?pageid=online


Programming in Prolog: Using the ISO Standard 
by C.S. Mellish (Author), W.F. Clocksin



Logic

"Bob's book". Logic for Problem Solving
Logic for Problem Solving by Robert Kowalski is out of print but fortunatley the text can be viewed in pdf for at his web page at Imperial College.


Simply Logical, Intelligent Reasoning by Example
Simply Logical, Intelligent Reasoning by Example by Peter Flach is out of print but he kindly provides a pdf http://www.cs.bris.ac.uk/~flach/SimplyLogical.html

AI

Prolog Programming for Artificial Intelligence
by Ivan Bratko


Interesting Sites

Logic in Action
http://www.logicinaction.org

This is the new homepage of the Logic in Action Open Course Project. The project aims at the development of elementary and intermediate courses in logic in electronic form.

Wednesday, May 1, 2013

UML Sequence diagrams - TraceModeller, It's the business!

Sequence diagrams are such a useful model. I've always thought so, and often I've wanted to create them and my employer only had Visio to hand. Visio is big, it's clever, but it's too general trying to do all things for all people.

A colleguq has shown me his personal copy of Tracemodeller and it's so good. Yes it's a bit Java UI and sparten but that shouldn't matter. $109 I think.

http://www.tracemodeler.com/

Wednesday, April 24, 2013

How to start swi-prolog on Mac OSX

So you've downloaded the swi-prolog package from http://www.swi-prolog.org/Download.html and you need to run it.

It runs in the terminal but you need to run, opt/local/bin/swipl

If you have a folder with your file in, e.g. Download/myfile.pl then why not navigate to that folder in terminal before running swipl. That way you can edit and create files easily in your editor of choice and load the file easily in SWI-prolog.

By the way, to load a file type, ['my file.pl'].


You can, if you want, run the PCEemacs editor. You'll want to do this sooner or later.
To run this you should type, emacs.

It may ask you to load X11. Just follow the instructions.

Saturday, January 5, 2013

A grid where rows, columns and diagonals are equal

My son showed me trick where I chose a number between 20 and 99 (inclusive) and he would then create a 4-by-4 grid of numbers where all the numbers would equal that number.
I thought I would write the algorithm. I thought it might be interesting for him to see how how a computer program worked. I thought C# would be easy so I used Mono on the mac.


[Main.cs]


using System;

namespace AllWaysAddUp
{
class MainClass
{
private static NumberGrid _numberGrid;

public static void Main (string[] args)
{

Console.WriteLine ("Enter a number between 20 and 99 and this will calculate"+ '\n' + 
                  "a grid of 4 rows and 4 columns where all the rows, columns"+ '\n' +
                  "and diagonals all add up to that number"+ '\n' + '\n'+
                  "Enter a number between 20 and 99...");

string s = Console.ReadLine();
int i;

if(!int.TryParse(s, out i))
Console.WriteLine("You did not enter a number");
else {
if( i < 20 || i > 99)
Console.WriteLine("Number not in range. Number must be between 20 and 99.");
else {
_numberGrid = new NumberGrid();
_numberGrid.calculate(i);

PrintNumberGrid();
}
}
Console.ReadLine();
}

private static void PrintNumberGrid()
{
for (int row = 0; row < 4; row++) {
Console.WriteLine();
for (int col = 0; col < 4; col++) {
int num = _numberGrid.GetCell(row, col);
Console.Write (" {0}" + '\t', num.ToString());
}
}
}
}
}


[NumberGrid]


using System;

namespace AllWaysAddUp
{
public class NumberGrid
{
private int[,] _grid;

// Array format: {row0 col0, row0 col1, row0 col2, row0 col3 }
//               {row1 col0, row1 col1, row1 col2, row1 col3 }
//               {row2 col0, row2 col1, row2 col2, row2 col3 }
//               {row3 col0, row3 col1, row3 col2, row3 col3 }
public NumberGrid ()
{
_grid =  new int [4,4] {{-1, 1, 12, 7} , {11, 8, -1, 2} ,{5, 10, 3,-1} ,{4,-1, 6, 9}} ;
}

public int GetCell(int rowNumber, int columnNumber)
{
return _grid[rowNumber, columnNumber];
}

public void calculate(int number)
{
_grid[0,0] = number -20;
_grid[1,2] = _grid[0,0] - 1;
_grid[3,1] = _grid[0,0] + 1;
_grid[2,3] = _grid[3,1] + 1;
}
}
}









How to test memory in OSX

If your are experiencing an occasional random rebooting then maybe there is a memory gremlin.

 A pretty good old-school memory test can be done by downloading memtest and installing it, then starting your Mac in command line mode (single user mode), and then running memtest.

  1. Download memtest
  2. Install. It will be placed in /usr/bin/
  3. Shut down the mac and restart keeping the cmd and s keys depressed. It will boot into single user mode.
  4. Run memtest by typing, memtest all
Where to get memtest
It can be found in a few places and I have used it from here before, http://www.optilabs.eu/download/memtest_422.zip


Multiple tests?
memtest all n

:/ root# memtest all 5

(Running 5 times took over 5 hours)


Example Output

Memtest version 4.22 (64-bit)
Copyright (C) 2004 Charles Cazabon
Copyright (C) 204-2008 Tony Scaminaci (Macintosh port)
Licensed under the GNU General Public Licence version 2 only

Max OS X 10.8.2 (12C60) running in single user mode
Memory Page Size: 4096
System has 2 Intel core(s) with SSE
Requested memory: 3864MB (4052697088 bytes)
Available memory: 3864MB (4052697088 bytes)
NOTE: Memory request is too large, reducing to acceptable value...
Allocating memory: 3756MB (3939221568 bytes) at local address 0x0000000101000000
Attempting memory lock...locked successfully
Partitioning memory into 2 comparison buffers...
Buffer A: 1878MB (196910784 bytes) starts at local address 0x0000000101000000
Buffer B: 1878MB (196910784 bytes) starts at local address 0x000000017665e020

Running 5 test sequences... (CTRL-C to quit)

Test sequence 1 of 5:

Running tests on full 3756MB region...
 Stuck Address       : ok
 Linear PRN          : ok
Running comparison tests using 1878MB buffers...
 Random Value        : ok
 Compare XOR         : ok
 Compare SYB         : ok
 Compare MUL         : ok
 Compare DIV         : ok
 Compare OR          : ok
 Compare AND         : ok
 Sequential Increment: ok
 Solid bits          : ok
 Block Sequential    : ok
 Checkerboard        : ok
 Bit Spread          : ok
 Bit Flip            : ok
 Walking Ones        : ok
 Walking Zeroes      : ok


Saturday, November 17, 2012

NSLog Format Specifiers

These formatters are used with NSLog, and also Logging.h mentioned in a previous post. Take a look.

%d, %isigned int
%uunsigned int
%ffloat or double
%efloat or double in standard form or scientific notation
%xhex int
%Xhex int
%ooctal int
%ccharacter
%Cunichar
%@object

Examples:

(1)
int num=9;
NSLog(@"The value of the integer is %i", num);

Result: The value of the integer is 9



(2)
NSDate *today= [NSDate date];
NSLog(@"the date is %@",today);

Result:  the date is 2012-11-17 01:15:00 +0000

Monday, November 12, 2012

How to add an existing Xcode project to Source control

If you create a project in Xcode and later you want to add it to source control then you need to run a couple of command line controls. But it's easy.

1. Quit Xcode.

2. Open terminal and navigate to your directory. Using something like:
cd Developer/myprojectname
There is a file in Xcode that we do not want to add to source control. This records the Xcode environment state. Because we're doing this adding manually, we have to tell Git manually to ignore it. If we had told Xcode to add the project to source control when we created the project it would have ignored this file automatically.

3. Type the find command to find the file file.
find .
The file we are looking for is UserInterfaceState.xcuserstate

4. Place the file name into an exclude file.

echo myproject.xcodeproj/project.xcworkspace/xcuserdata/John.xcuserdatad/UserInterfaceState.xcuserstate >.gitignore

This will create the file gitignore

5. Initialise Git
git  init
Did you get an error? (See bottom of post)

6. Add the files
git add .

7. Commit the files to the repository

git commit -m "write a short comment here"



Problem with Git?
Are you getting the command not found?

bash: git: command not found
This can be solved by:

  1. Open Xcode
  2. Open Preferences
  3. Chose the Downloads tab, choose components
  4. Instal the Command line tools.
Bob's your uncle.



Saturday, November 10, 2012

How & why incentives do not work


Firstly, if you have 18 minutes to spare, then rather than read this, go and watch Dan Pink's video at TED.com. It really is food for thought.


In short, Dan says that proof exists to support the statement that incentives - the carrot or the sharp stick - do not work.
They do not work in many types of work. Not all, but most that we are likely to deal with.

But, and here's an interesting thing. Not only do they not work, they actually do harm. They give the opposite of the desired effect.


The candle problem
Dan starts by looking at the the candle problem.

If you give people the following: a candle, a box of pins and some matches, and you then ask them to fix the candle to the wall so that the lit candle's wax doesn't drip onto the table. How could this be done? and do they perform?




(A clue. To solve this you over come functional fixedness)

Do incentives work
If you incentivise this task, i.e. if you give a group incentives such as, 'if you're the fastest you'll get £50, if you're in the top 10% you'll get £20'. Then you get interesting results. (these £ values were not the ones used in the quoted experiement but I have adjusted for inflation :).  I'm sure the experiment holds)

The team that were given the financial incentives actually did a lot worse, and this is not a freak of this experiment or of this challenge. There are many more examples and experiments that back this up.

The solution to the candle problem?
It is at the bottom of the blog page.


Restate the problem and...
If you restate the problem as in this diagram and still give incentives, then the incentives make a significant difference. They have the desired effect and deliver an improvement in performance.

I think it's clear to see why when you realise the pins are no longer in the tray that was being used to collect them together.




So incentives appear to work for mechanistic, well defined tasks, but not for jobs that require creative thought. If the work is clearly defined and has an easy to see goal then the incentives work. It is almost as if the value of the incentive narrows the focus through a fear of losing the sweet carrot or money, or fear of meeting the pointy end of the stick.

This kind of left-brain work seems ideally suited to incentives but more and more our work is right-brain - creative, inventive.

A couple of quotes:
As long as the task involved only mechanical skill, bonuses worked as they would be expected: the higher the pay , the better the performance. 
But once the task called for "even rudimentary cognitive skill," a larger reward "led to poorer performance."

D. Ariely, U. Gneezy, G. Lowenstein, & N. Mazar, Federal Reserve Bank of Boston Working Paper No. 05-11, July 2005; NY Times, 20 Nov. 08



Autonomy - Mastery - Purpose
If we can no longer motivate using incentives or threats then what can motivates?
Dan suggests:
Autonomy - the urge to direct our own lives
Mastery - to get better and better at something
Purpose - the urge to be a art of something bigger than ourselves.


Traditional management
Management is a recent concept and it did not emanate from nature. It's an invention from mankind. It's ideal for compliance but it does not mean that it's always right. Maybe, at times, we need to rethink the management styles based on the three themes above.

I currently work in a very traditional environment and have found myself in a team of dispirited developers that distrust management, distrust each other, and are distrusted by management. I'm stuck in the middle as a Development Manager. My approach has been to apply two themes to improve things. One, traditional management - that is, clearly manage the tasks, the projects and the list of work. Senior management folk like that and the developers do too. The estimates are still off a bit but we can work on that.
The other theme is non-traditional. I encourage Mastery and Autonomy in the way I treat developer tasks and collaboration. I expand their range of influence and never pass on their successes as my own up the traditional management line. I motivate by letting them motivate themselves.
Warning. Senior management don't get this. Not mine anyway and that's probably why my days will be numbered. But I'll keep on trying. It's the only right way.




The answer.




Friday, November 9, 2012

Pages 4.2 not linking to iCloud - This is not your primary account

I have Pages on my iPad and Mac mini, and also my iMac. But I have recently upgraded the OS on my Mac Book Pro to Mountain Lion and Pages and Numbers don't connect to iCloud.

I checked the version of Pages - 4.2. So that should be ok.
I tried to update it from App store and of course it said don't be silly.

I went to iCloud in System Preferences and signed in. All I saw was the oportunity to select Mail, Contacts, Calendars and Notes. What I really wanted was shown below.

The iCloud form told me that I was not logged in with my primary account. But I was.



The task is to delete the iCloud account from the Preferences and log in again.

To do this un-tick the Contacts, Calendars and Notes. Try to untick Mail but it might complain.
If it does complain go to mail and remove the mail account.
Then go to Preferences again and untick the Mail. Now remove the iCloud account by using the "-".

Now add your iCloud account and ensure you tick "Documents & Data".

Now go to Pages etc and when you start it should let you look at your iclid files.

Just the ticket.