February 2, 2011

Wireless Doesn’t Work in Ubuntu 10.10

For the longest time, I couldn’t figure out how to get my wireless to on my laptop. I could enable and disable my wireless card, but I could never scan and view available wireless channels. After searching many forums, I came across an article that suggested this.

rfkill unblock all

After you run this command, disable and enable your wireless card. If your problem was similar to mine, then you should be able to see wireless channels and connect. I’m not sure exactly why this works, but it worked for me. Feel free to explain if you know why this solution works. I am curious.

I hope this helps someone out. I’m very thankful I was able to come across the solution.

http://life.firelace.com/2010/10/tips-and-tricks-upgrading-to-ubuntu-10-10-wireless-fix/

February 1, 2011

Rename Files in Windows 7 using the Command Prompt

I came across a situation where a client of mine needed a whole batch of pictures renamed to display “underscores” instead of “dashes”. For example, I needed the following to be changed:

W-Lawther-4311-1263.jpg
W_Lawther_4311_1263.jpg

Going through each individual file and manually changing each dash to an underscore would be painstaking. I did some research and found a very simple solution to this problem.

ren ?-???????-????-????.jpg ?_???????_????_????.jpg

Open up the Command Prompt (Start->Run->Type: cmd)
Navigate to your directory using the ‘cd’ command and display the contents of your directory using the ‘dir’ command. Here is an example of the process I went through.

Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.
 
C:\Users\Chris>dir
 Volume in drive C has no label.
 Volume Serial Number is BC3E-4FC9
 
 Directory of C:\Users\Chris
 
01/24/2011  08:22 AM    <DIR>          .
01/24/2011  08:22 AM    <DIR>          ..
01/24/2011  03:03 PM    <DIR>          Contacts
01/31/2011  05:54 PM    <DIR>          Desktop
01/28/2011  02:27 PM    <DIR>          Documents
01/31/2011  04:02 AM    <DIR>          Downloads
01/24/2011  03:03 PM    <DIR>          Favorites
01/24/2011  03:03 PM    <DIR>          Links
01/24/2011  03:03 PM    <DIR>          Music
02/01/2011  09:56 AM    <DIR>          Pictures
01/24/2011  03:03 PM    <DIR>          Saved Games
01/24/2011  03:03 PM    <DIR>          Searches
01/24/2011  03:03 PM    <DIR>          Videos
               0 File(s)              0 bytes
              13 Dir(s)  66,118,414,336 bytes free
 
C:\Users\Chris>cd Pictures
 
C:\Users\Chris\Pictures>dir
 Volume in drive C has no label.
 Volume Serial Number is BC3E-4FC9
 
 Directory of C:\Users\Chris\Pictures
 
02/01/2011  09:56 AM    <DIR>          .
02/01/2011  09:56 AM    <DIR>          ..
02/01/2011  10:11 AM    <DIR>          4311 W Lawther 550
09/18/2010  11:13 AM           242,056 Earth.jpg
               1 File(s)        242,056 bytes
               3 Dir(s)  66,118,414,336 bytes free
 
C:\Users\Chris\Pictures>cd "4311 W Lawther 550"
 
C:\Users\Chris\Pictures\4311 W Lawther 550>dir
 Volume in drive C has no label.
 Volume Serial Number is BC3E-4FC9
 
 Directory of C:\Users\Chris\Pictures\4311 W Lawther 550
 
02/01/2011  10:11 AM    <DIR>          .
02/01/2011  10:11 AM    <DIR>          ..
02/01/2011  10:11 AM    <DIR>          JPEG
01/20/2011  02:29 PM           190,939 W-Lawther-4311-1003.jpg
01/20/2011  02:29 PM           229,187 W-Lawther-4311-1011.jpg
01/20/2011  02:29 PM           362,945 W-Lawther-4311-1019.jpg
01/20/2011  02:29 PM           315,891 W-Lawther-4311-1025.jpg
01/20/2011  02:29 PM           324,793 W-Lawther-4311-1037.jpg
01/20/2011  02:29 PM           298,915 W-Lawther-4311-1054.jpg
01/20/2011  02:29 PM           311,920 W-Lawther-4311-1062.jpg
01/20/2011  02:29 PM           218,105 W-Lawther-4311-1069.jpg
01/20/2011  02:30 PM           284,679 W-Lawther-4311-1092.jpg
01/24/2011  03:50 PM           179,684 W-Lawther-4311-1097.jpg
01/24/2011  03:55 PM           215,851 W-Lawther-4311-1132.jpg
01/24/2011  03:53 PM           119,409 W-Lawther-4311-1183.jpg
01/24/2011  03:53 PM           121,753 W-Lawther-4311-1192.jpg
01/24/2011  03:53 PM           130,715 W-Lawther-4311-1201.jpg
01/24/2011  03:50 PM           191,201 W-Lawther-4311-1216.jpg
01/24/2011  03:50 PM           188,831 W-Lawther-4311-1225.jpg
01/24/2011  03:53 PM           163,884 W-Lawther-4311-1242.jpg
01/24/2011  03:51 PM           215,726 W-Lawther-4311-1251A.jpg
01/24/2011  03:53 PM           174,017 W-Lawther-4311-1263.jpg
01/24/2011  03:54 PM           170,680 W-Lawther-4311-1273.jpg
01/24/2011  03:54 PM           150,399 W-Lawther-4311-1283.jpg
01/24/2011  03:51 PM           189,790 W-Lawther-4311-1289.jpg
01/24/2011  03:51 PM           169,936 W-Lawther-4311-1307.jpg
01/24/2011  03:51 PM           171,228 W-Lawther-4311-1321.jpg
01/24/2011  03:54 PM           155,343 W-Lawther-4311-1333.jpg
01/20/2011  02:29 PM           319,921 W-Lawther-4311-2048.jpg
01/20/2011  02:30 PM           227,850 W-Lawther-4311-2081.jpg
01/24/2011  03:55 PM           168,058 W-Lawther-4311-2144.jpg
              28 File(s)      5,961,650 bytes
               3 Dir(s)  66,118,414,336 bytes free
 
C:\Users\Chris\Pictures\4311 W Lawther 550>ren ?-???????-????-????.jpg ?_???????
_????_????.jpg
 
C:\Users\Chris\Pictures\4311 W Lawther 550>dir
 Volume in drive C has no label.
 Volume Serial Number is BC3E-4FC9
 
 Directory of C:\Users\Chris\Pictures\4311 W Lawther 550
 
02/01/2011  10:35 AM    <DIR>          .
02/01/2011  10:35 AM    <DIR>          ..
02/01/2011  10:34 AM    <DIR>          JPEG
01/20/2011  02:29 PM           190,939 W_Lawther_4311_1003.jpg
01/20/2011  02:29 PM           229,187 W_Lawther_4311_1011.jpg
01/20/2011  02:29 PM           362,945 W_Lawther_4311_1019.jpg
01/20/2011  02:29 PM           315,891 W_Lawther_4311_1025.jpg
01/20/2011  02:29 PM           324,793 W_Lawther_4311_1037.jpg
01/20/2011  02:29 PM           298,915 W_Lawther_4311_1054.jpg
01/20/2011  02:29 PM           311,920 W_Lawther_4311_1062.jpg
01/20/2011  02:29 PM           218,105 W_Lawther_4311_1069.jpg
01/20/2011  02:30 PM           284,679 W_Lawther_4311_1092.jpg
01/24/2011  03:50 PM           179,684 W_Lawther_4311_1097.jpg
01/24/2011  03:55 PM           215,851 W_Lawther_4311_1132.jpg
01/24/2011  03:53 PM           119,409 W_Lawther_4311_1183.jpg
01/24/2011  03:53 PM           121,753 W_Lawther_4311_1192.jpg
01/24/2011  03:53 PM           130,715 W_Lawther_4311_1201.jpg
01/24/2011  03:50 PM           191,201 W_Lawther_4311_1216.jpg
01/24/2011  03:50 PM           188,831 W_Lawther_4311_1225.jpg
01/24/2011  03:53 PM           163,884 W_Lawther_4311_1242.jpg
01/24/2011  03:51 PM           215,726 W_Lawther_4311_1251.jpg
01/24/2011  03:53 PM           174,017 W_Lawther_4311_1263.jpg
01/24/2011  03:54 PM           170,680 W_Lawther_4311_1273.jpg
01/24/2011  03:54 PM           150,399 W_Lawther_4311_1283.jpg
01/24/2011  03:51 PM           189,790 W_Lawther_4311_1289.jpg
01/24/2011  03:51 PM           169,936 W_Lawther_4311_1307.jpg
01/24/2011  03:51 PM           171,228 W_Lawther_4311_1321.jpg
01/24/2011  03:54 PM           155,343 W_Lawther_4311_1333.jpg
01/20/2011  02:29 PM           319,921 W_Lawther_4311_2048.jpg
01/20/2011  02:30 PM           227,850 W_Lawther_4311_2081.jpg
01/24/2011  03:55 PM           168,058 W_Lawther_4311_2144.jpg
              28 File(s)      5,961,650 bytes
               3 Dir(s)  66,118,410,240 bytes free
 
C:\Users\Chris\Pictures\4311 W Lawther 550>cd JPEG
 
C:\Users\Chris\Pictures\4311 W Lawther 550\JPEG>dir
 Volume in drive C has no label.
 Volume Serial Number is BC3E-4FC9
 
 Directory of C:\Users\Chris\Pictures\4311 W Lawther 550\JPEG
 
02/01/2011  10:34 AM    <DIR>          .
02/01/2011  10:34 AM    <DIR>          ..
02/01/2011  10:11 AM            38,673 W-Lawther-4311-1003.jpg
02/01/2011  10:11 AM            39,929 W-Lawther-4311-1011.jpg
02/01/2011  10:11 AM            41,485 W-Lawther-4311-1019.jpg
02/01/2011  10:11 AM            39,100 W-Lawther-4311-1025.jpg
02/01/2011  10:11 AM            39,778 W-Lawther-4311-1037.jpg
02/01/2011  10:11 AM            38,511 W-Lawther-4311-1054.jpg
02/01/2011  10:11 AM            38,979 W-Lawther-4311-1062.jpg
02/01/2011  10:11 AM            36,873 W-Lawther-4311-1069.jpg
02/01/2011  10:11 AM            39,101 W-Lawther-4311-1092.jpg
02/01/2011  10:11 AM            36,034 W-Lawther-4311-1097.jpg
02/01/2011  10:11 AM            39,793 W-Lawther-4311-1132.jpg
02/01/2011  10:11 AM            34,166 W-Lawther-4311-1183.jpg
02/01/2011  10:11 AM            34,872 W-Lawther-4311-1192.jpg
02/01/2011  10:11 AM            33,423 W-Lawther-4311-1201.jpg
02/01/2011  10:11 AM            38,824 W-Lawther-4311-1216.jpg
02/01/2011  10:11 AM            38,824 W-Lawther-4311-1225.jpg
02/01/2011  10:11 AM            36,862 W-Lawther-4311-1242.jpg
02/01/2011  10:11 AM            39,910 W-Lawther-4311-1251.jpg
02/01/2011  10:11 AM            36,474 W-Lawther-4311-1263.jpg
02/01/2011  10:11 AM            35,538 W-Lawther-4311-1273.jpg
02/01/2011  10:11 AM            36,691 W-Lawther-4311-1283.jpg
02/01/2011  10:11 AM            42,598 W-Lawther-4311-1289.jpg
02/01/2011  10:11 AM            35,948 W-Lawther-4311-1307.jpg
02/01/2011  10:11 AM            36,512 W-Lawther-4311-1321.jpg
02/01/2011  10:11 AM            36,163 W-Lawther-4311-1333.jpg
02/01/2011  10:11 AM            39,918 W-Lawther-4311-2048.jpg
02/01/2011  10:11 AM            36,907 W-Lawther-4311-2081.jpg
02/01/2011  10:11 AM            37,046 W-Lawther-4311-2144.jpg
              28 File(s)      1,058,932 bytes
               2 Dir(s)  66,118,410,240 bytes free
 
C:\Users\Chris\Pictures\4311 W Lawther 550\JPEG>ren ?-???????-????-????.jpg ?_??
?????_????_????.jpg
 
C:\Users\Chris\Pictures\4311 W Lawther 550\JPEG>dir
 Volume in drive C has no label.
 Volume Serial Number is BC3E-4FC9
 
 Directory of C:\Users\Chris\Pictures\4311 W Lawther 550\JPEG
 
02/01/2011  10:36 AM    <DIR>          .
02/01/2011  10:36 AM    <DIR>          ..
02/01/2011  10:11 AM            38,673 W_Lawther_4311_1003.jpg
02/01/2011  10:11 AM            39,929 W_Lawther_4311_1011.jpg
02/01/2011  10:11 AM            41,485 W_Lawther_4311_1019.jpg
02/01/2011  10:11 AM            39,100 W_Lawther_4311_1025.jpg
02/01/2011  10:11 AM            39,778 W_Lawther_4311_1037.jpg
02/01/2011  10:11 AM            38,511 W_Lawther_4311_1054.jpg
02/01/2011  10:11 AM            38,979 W_Lawther_4311_1062.jpg
02/01/2011  10:11 AM            36,873 W_Lawther_4311_1069.jpg
02/01/2011  10:11 AM            39,101 W_Lawther_4311_1092.jpg
02/01/2011  10:11 AM            36,034 W_Lawther_4311_1097.jpg
02/01/2011  10:11 AM            39,793 W_Lawther_4311_1132.jpg
02/01/2011  10:11 AM            34,166 W_Lawther_4311_1183.jpg
02/01/2011  10:11 AM            34,872 W_Lawther_4311_1192.jpg
02/01/2011  10:11 AM            33,423 W_Lawther_4311_1201.jpg
02/01/2011  10:11 AM            38,824 W_Lawther_4311_1216.jpg
02/01/2011  10:11 AM            38,824 W_Lawther_4311_1225.jpg
02/01/2011  10:11 AM            36,862 W_Lawther_4311_1242.jpg
02/01/2011  10:11 AM            39,910 W_Lawther_4311_1251.jpg
02/01/2011  10:11 AM            36,474 W_Lawther_4311_1263.jpg
02/01/2011  10:11 AM            35,538 W_Lawther_4311_1273.jpg
02/01/2011  10:11 AM            36,691 W_Lawther_4311_1283.jpg
02/01/2011  10:11 AM            42,598 W_Lawther_4311_1289.jpg
02/01/2011  10:11 AM            35,948 W_Lawther_4311_1307.jpg
02/01/2011  10:11 AM            36,512 W_Lawther_4311_1321.jpg
02/01/2011  10:11 AM            36,163 W_Lawther_4311_1333.jpg
02/01/2011  10:11 AM            39,918 W_Lawther_4311_2048.jpg
02/01/2011  10:11 AM            36,907 W_Lawther_4311_2081.jpg
02/01/2011  10:11 AM            37,046 W_Lawther_4311_2144.jpg
              28 File(s)      1,058,932 bytes
               2 Dir(s)  66,118,402,048 bytes free
 
C:\Users\Chris\Pictures\4311 W Lawther 550\JPEG>

December 21, 2010

Executable Prolog CLP Program for the Following Job Shop Scheduling Problem

The job shop scheduling problem has a wide range of applications in industry. The problem we will explore is a simple job shop scheduling for a print shop. The print shop has 6 machines: [printer, stapler, sorter, binder, packager, copier], and schedules a number of jobs over a two day (48 hour) period. Each job is a sequence of tasks which have a name, duration (in hours) and machine that they must be performed on. A schedule assigns a start time for each task, so that no task starts before the previous task in the job has finished, and no machine is busy with two tasks at the same time. The main user defined predicate defined to solve the scheduling problem is data(TaskList, Jobs, Machines, Vars) where

  • TaskList is a list of tasks. Each task is represented as a term task(Name, Duration, Machine, StartTime) where Name, Duration, StartTime are the name, start time, and duration of the task, Machine is the machine the task needs.
  • Jobs is a list of jobs which in turn by definition is a list of tasks. Each job in this list consists of the tasks that make up this job in the order that they should be completed,
  • Machines is the list of all machines in the shop, and
  • Vars is the list of all start time variables (of the tasks).

As an example, data/4 below describes a pair of small advertising print jobs.

data(
   %%% First argument is a list of tasks with name, duration
   %%% and machine and start time (unconstrained)
   [
     task(ad1_copy, 4, copier, AC1),
     task(ad1_sort, 4, sorter, AS1),
     task(ad1_pack, 3, packager, AD1),
     task(ad2_copy, 5, copier, AC2),
     task(ad2_sort, 4, sorter, AS2),
     task(ad2_pack, 4, packager, AD2)
   ],
   %%% second argument is a list of lists of task names to be
   %%% performed in order
   [[ad1_copy,ad1_sort,ad1_pack],
    [ad2_copy,ad2_sort,ad2_pack]
   ],
   %%% third argument is the list of different machines
   [copier,sorter,packager],
   %%% fourth argument is the list of all start time variables
   [AC1,AS1,AD1,AC2,AS2,AD2]
).

The file ’job.pl’ above, containing the data/4, should be saved under the same directory as your program. When your program is tested, it should be able to handle more jobs and each job may have more tasks. This program uses the template schedule-template.pl, below.

%% This is a program stub for the job shop scheduling problem
%% given in the project.
 
% Load in finite domain module
:- use_module(library(clpfd)).
% Other modules you may use are loaded here
 
% You must use the following to load in the data
:-['job.pl'].
 
% Your definition of the userdefined constraint jobshop(L) should be here.
 
member(X,[X|_]).
member(X,[_|T]):-
	member(X,T).
 
jobshop(L) :-
	data(W,X,_,Z),
	init(W),
	precLL(X,W),
	capacity(W),
	labeling([],Z),
	L = Z.
 
init([]).
init(TASKLIST):-
	TASKLIST=[task(_,D,_,ST)|T],
	ST in 0..48,
	ST+D #=< 48,
	init(T).
 
precLL([], _).
precLL(JOBS,TASKLIST) :-
	JOBS = [H|T],
	precL(H, [2], TASKLIST),
	precLL(T,TASKLIST).
 
precL(_, [], _).
precL([], _, _).
precL(JOBS, _, TASKLIST) :-
	JOBS=[H|T], T=[HH|TT],
	member(task(H,D1,_,T1), TASKLIST),
	member(task(HH,_,_,T2), TASKLIST),
	T1 + D1 #< T2,
	%write(H), write('-'), write(HH),write('\n'),
	precL(T, TT,TASKLIST).
 
exlude(T1, D1, T2, _) :-
	T1 + D1 #< T2.
exlude(T1, _, T2, D2) :-
	T2 + D2 #< T1.
 
capacity([]).
capacity(TASKLIST) :-
	TASKLIST = [task(J,D,M,_)|T],
	%write(J), write('-'), write(D), write('-'), write(M), write('-'), write(ST), write('\n'),
	nonoverlaps(J,D,M,T,TASKLIST),
	capacity(T).
 
nonoverlaps(_,_,_,[],_).
nonoverlaps(J,D,M,L,TASKLIST) :-
	L = [task(_, _, M2, _) | T],
	M \= M2,
	nonoverlaps(J,D,M,T,TASKLIST).
nonoverlaps(J,D,M,L,TASKLIST) :-
	L=[task(_,D2,M2,T2)|T],
	M = M2,
	member(task(J,D,M,T1),TASKLIST),
	exlude(T1,D,T2,D2),
	nonoverlaps(J,D,M,T,TASKLIST).
 
% Your rules should not go below this line
 
% Don't delete or comment away the following
 
%% Output the solution
myoutput :- jobshop(L),
 	    write('The starting time of the tasks are: \n'),
            myprint(L, 1).
 
%% this is to output the sarting time
myprint([], _).
myprint([X|L], I) :- write('task '), write(I), write(' starts at '),
                   write(X), write('\n'), Y #= I+1, myprint(L, Y).
 
%% initiat the output.
:- myoutput.

Sample query and output under Sicstus Prolog

banana$ sicstus
SICStus 3.11.2 (sparc-solaris-5.7): Wed Jun  2 11:43:16 MET DST 2004
Licensed to ttu.edu
| ?- ['schedule-template.pl'].
% consulting /home/undergrad3/ebaldelo/schedule-template.pl...
%  loading /usr/local/lib/sicstus-3.11.2/library/clpfd.po...
%  module clpfd imported into user
%   loading /usr/local/lib/sicstus-3.11.2/library/atts.po...
%   module attributes imported into clpfd
%    loading /usr/local/lib/sicstus-3.11.2/library/lists.po...
%    module lists imported into attributes
%    loaded /usr/local/lib/sicstus-3.11.2/library/lists.po in module lists, 10 msec 18528 bytes
%   loaded /usr/local/lib/sicstus-3.11.2/library/atts.po in module attributes, 10 msec 46400 bytes
%   loading /usr/local/lib/sicstus-3.11.2/library/assoc.po...
%   module assoc imported into clpfd
%    module lists imported into assoc
%   loaded /usr/local/lib/sicstus-3.11.2/library/assoc.po in module assoc, 20 msec 17988 bytes
%   module lists imported into clpfd
%   loading /usr/local/lib/sicstus-3.11.2/library/ordsets.po...
%   module ordsets imported into clpfd
%   loaded /usr/local/lib/sicstus-3.11.2/library/ordsets.po in module ordsets, 0 msec 21496 bytes
%   loading /usr/local/lib/sicstus-3.11.2/library/ugraphs.po...
%   module ugraphs imported into clpfd
%    module ordsets imported into ugraphs
%    module lists imported into ugraphs
%    module assoc imported into ugraphs
%    loading /usr/local/lib/sicstus-3.11.2/library/random.po...
%    module random imported into ugraphs
%     module assoc imported into random
%     loading foreign resource /usr/local/lib/sicstus-3.11.2/library/sparc-solaris-5.7/random.so in module random
%    loaded /usr/local/lib/sicstus-3.11.2/library/random.po in module random, 20 msec 7776 bytes
%   loaded /usr/local/lib/sicstus-3.11.2/library/ugraphs.po in module ugraphs, 70 msec 84872 bytes
%   loading foreign resource /usr/local/lib/sicstus-3.11.2/library/sparc-solaris-5.7/clpfd.so in module clpfd
%  loaded /usr/local/lib/sicstus-3.11.2/library/clpfd.po in module clpfd, 170 msec 829440 bytes
%  consulting /home/undergrad3/ebaldelo/job.pl...
%  consulted /home/undergrad3/ebaldelo/job.pl in module user, 0 msec 912 bytes
The starting time of the tasks are:
task 1 starts at 0
task 2 starts at 5
task 3 starts at 10
task 4 starts at 5
task 5 starts at 11
task 6 starts at 16
% consulted /home/undergrad3/ebaldelo/schedule-template.pl in module user, 200 msec 836040 bytes
yes
| ?-

September 24, 2010

Adding Color, Drawing a Tetrahedron, Cube with WebGL

Our second assignment asked us to use color when drawing a tetrahedron and cube using WebGL. Here is a screen shot of my work.

If you have a WebGl capable browser, then follow the link and you can test it yourself. Make sure you view source on the page.

Here are the three files you need to run this lesson.
lesson2.html
sylvester.js
glUtils.js

September 18, 2010

How to get the Symbol Browser Plugin to work in Gedit

Ok, so twice I had to research and figure out how I got the symbol browser working in gedit. I started out trying to install the Class Browser. But the ubuntu community suggested that this plugin was no good and to instead use the symbol browser. If you quickly rush and start downloading the symbol browser plugin, you will find that you just downloaded a whole bunch files that need to be compiled. Lucky for us though, you can instead download the binary files for the symbol browser. Go to SourceForge and instead of clicking on “Download Files”, look below under “Browse Files for Gedit Symbol Browser Plugin” and you should see

gedit-symbol-browser-plugin-bin-ubuntu-i386-0.1.tar.gz

If you are using 64 bit, make sure download the 64 bit binary version.

Extract your files and move it over to ~/.gnome2/gedit. You should have the following files…
~/.gnome2/gedit/symbols
~/.gnome2/gedit/plugins/libsymbolbrowser.so
~/.gnome2/gedit/plugins/symbolbrowser.gedit-plugin

A lot of us will now try and open Gedit and enable the plugin. As you try, it will automatically get grayed out. This is because you need the following package.

libgnomeprintui

Once you restart Gedit and enable the plugin, you might realize that it’s still not displaying any class, functions, or variables after you open a file. This is because you need to install exuberant-ctags. After you do this, you should be ready to go.

Let me know if you have any problems.

September 15, 2010

Drawing Simple Shapes – Triangle, Square in WebGL

Our first assignment asked us to draw simple shapes using WebGL. For this particular project I drew a simple square and triangle. Here is a screen shot of my work.

If you have a WebGl capable browser, then follow the link and you can test it yourself. Make sure you view source on the page. I heavily commented the code.

Here are the three files you need to run this lesson.
lesson1.html
sylvester.js
glUtils.js

September 8, 2010

Introduction to WebGL

I’m currently taking a computer graphics class and I’ll be spending this semester delving into WebGL. So, I’ll be giving some tutorials as I get new projects to work on.

Setting Up Your Browser To Support WebGL

First do a Google search for “google chrome developer build”, and it should be the first result. Here is the link it took me to.

http://www.google.com/chrome/intl/en/eula_dev.html

Once you have chrome’s developer build, click on [Start->All Programs->Google Chrome] and then right-click on ‘Google Chrome’. Select ‘properties’ and append this to the end of the [Target] string.

--enable-webgl

Here is how my target string looked like.

C:\Users\Chris\AppData\Local\Google\Chrome\Application\chrome.exe --enable-webgl

Now open up Chrome by going through [Start->All Programs->Google Chrome] and you should be ready to view WebGL webpages. Click here to test for your self.

Getting Started With WebGL

Here are the three files you need to set up your frist WebGL script.
webgl.html
sylvester.js
glUtils.js

September 7, 2010

Cache Problem in CakePHP

I recently had a problem on our website with caching in our CakePHP framework. We initially had caching turned on. And then we started noticing when we submitted a form, our changes wound not be updated until we manually refreshed the page.

So let’s say I have a row of users. I choose to delete one of the users. I click on delete, hit submit and cake removes that user from my table. Cake then redirects me back to same page, but the deleted user is still listed on our page. Once we manually hit the refresh button on our browser, then our deleted user disappears from our form page.

We scratched our heads and tried figuring out why this was happening. We turned off all caching and even added the <cake:nocache> tag. No matter what we did, we could not get rid of the problem.

After much research, we realized that the browser itself was actually caching the previous form page. So we added this snippet of code to our app controller.

$this->disableCache();

This is used to tell the user’s browser not to cache the results of the current request. The headers sent to this effect are:

Expires: Mon, 26 Jul 1997 05:00:00 GMT
Last-Modified: [current datetime] GMT
Cache-Control: no-store, no-cache, must-revalidate
Cache-Control: post-check=0, pre-check=0
Pragma: no-cache

I hope this saves you some time. If you find a better way fixing this problem, please leave a comment.

April 19, 2010

Simple Constraint Programming Tutorial

A programming language is like a tool. Every tool has a specific purpose. One tool does one job better and another tool does another job better. Programming Languages should be thought of the same way.

Why Constraint Programming

The N-Queens puzzle is very popular and we were able to find several working examples in C and in Prolog.  We analyzed, compiled and tested all these programs and weighed all the pros and cons these two types of languages.

Prolog, being a higher level language, is intuitively easier to understand and easier to program. You can think about the problem with more abstraction. The challenge in actually programming in prolog is that you need to be able to correctly “define” and thus “constrain” the program to do what needs to be done. You don’t have to worry about data types, pointers, data structures and other lower level issues. All the prolog solutions we came across were between 40 and 50 lines of code. On the downside, it was inconvenient when trying to test any n size greater than 12. Trying to solve the puzzle for n=13 takes about 148 seconds.

We found one good example of the n queens problem written in C. We compiled it under Visual Studio 2008, and ran it. The obvious upside in the C program was the speed. It could still solve the puzzle in under a second for n=13. That’s a huge difference compared with the prolog program.  The downside is trying to understand the code and trying to write the code. It’s not as obvious to see what’s happening. The level of abstraction is not as narrow as it is in prolog. On top having to worry about correctly implementing a solution, you have to worry about correctly defining a data structure, defining the correct data types, properly handling arrays and pointers, and other lower level issues not in any way related to the puzzle’s algorithm. The C program that we were testing was 400 lines of code. 236 lines if you remove the comments. That’s almost 5 times the size of the prolog program.

Time Comparisons between Prolog and C

N Prolog (seconds) C (seconds)
1 < 1 < 1
2 < 1 < 1
3 < 1 < 1
4 < 1 < 1
5 < 1 < 1
6 < 1 < 1
7 < 1 < 1
8 < 1 < 1
9 < 1 < 1
10 < 1 < 1
11 3.5 < 1
12 20.3 < 1
13 Too long < 1
14 Too long 1
15 Too long 4
16 Too long 23
17 Too long Too long

Next, we wanted to discuss the simplicity in solving the n-queens problem in prolog. Let’s define our solution for N=4.

We define our solution by stating that each of the 4 queens must occupy it’s very own row, column, downward diagonal, and upward diagonal on a 4×4 board.

And that’s our definition! Now we need to tell prolog to do this and write our constraints.

%% DEFINE OUR BOARD     
%% sq(y-coord, x-coord, downward-diag, upward-diag) 
 sq(1,1,4,1).
 sq(1,2,5,2).
 sq(1,3,6,3).
 sq(1,4,7,4).
 sq(2,1,3,2).
 sq(2,2,4,3).
 sq(2,3,5,4).
 sq(2,4,6,5).
 sq(3,1,2,3).
 sq(3,2,3,4).
 sq(3,3,4,5).
 sq(3,4,5,6).
 sq(4,1,1,4).
 sq(4,2,2,5).
 sq(4,3,3,6).
 sq(4,4,4,7).
 
%% CONSTRAINT
%% We define our solution by stating that
%% each of the 4 queens must occupy it’s
%% very own row, column, downward diagonal,
%% and upward diagonal on a 4x4 board.
 chess(A,B, E,F, I,J, M,N) :-
 sq(A,B,C,D), sq(E,F,G,H), sq(I,J,K,L), sq(M,N,O,P),
 A=1, E=2, I=3, M=4,
 A\=E, A\=I, A\=M,
 B\=F, B\=J, B\=N,
 C\=G, C\=K, C\=O,
 D\=H, D\=L, D\=P,
 E\=I, E\=M,
 F\=J, F\=N,
 G\=K, G\=O,
 H\=L, H\=P,
 I\=M,
 J\=N,
 K\=O,
 L\=P.

Here is our output once we run our program:

?- chess(A,B,E,F,I,J,M,N).
 
A = 1,
B = 2,
E = 2,
F = 4,
I = 3,
J = 1,
M = 4,
N = 3 ;

Which means:  { Q1(1, 2) , Q2(2, 4) , Q3(3, 1) , Q4(4, 3) }

Which is of course, the right solution. Here was the other solution.

A = 1,
B = 3,
E = 2,
F = 1,
I = 3,
J = 4,
M = 4,
N = 2 ;

Which means:  { Q1(1, 3) , Q2(2, 1) , Q3(3, 4) , Q4(4, 2) }

Mapped File, Paging and Memory Protection – C Programming

This program shows use of a number of system calls such as how to map a disk file in the virtual memory of a process space, divide it into pages, set access protection code such as read_only, write_only, etc to individual pages, read/write individual pages, and copy the file back to disk page by page.

#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
extern int errno;
 
main()
{
int fid1, fid2;
void *address1, *address2;
char buf[10], data[10]="edwin c";
char *ptr, *base, data1[10]="baldelomar";
int i, n, pagesize;
struct stat statbuf;
int inFileSize, outFileSize;
 
if ((fid1 = open("alice29.txt", O_RDWR,  0600)) < 0)
 {perror("alice open failed");
  printf (" errno=%d \n", errno); 
 }
 
if (fstat(fid1, &statbuf)<0) perror("fstat error");
inFileSize = statbuf.st_size;
printf("Temp1 file size=%d\n", inFileSize);
 
if ((fid2 = open("temp2", O_RDWR | O_CREAT | O_TRUNC,  0600)) < 0)
 perror("temp2 open failed"); 
 
/* temp2 is empty file. It has no space. Let us make it 20000 bytes
long by setting pointer to 9999 and then writeing a byte. */
if ((lseek(fid2, 19999, SEEK_SET)) < 0) perror ("lseek error");
if (write(fid2, "X", 1) != 1) //write 'X' at end
  {perror("write error"); exit(-1);}
 
 
if (fstat(fid2, &statbuf)<0) perror("fstat error");
outFileSize = statbuf.st_size;
printf("Temp2 file size=%d\n", outFileSize);/*prints size=20000*/
 
/*----------------------------------------------------*/
if ((pagesize=sysconf(_SC_PAGESIZE))<0) perror("sysconf failed");
printf("page size =%d\n", pagesize);
 
/* map source file entirely in memory for reading by possibly
multiple processed, who may share all changes. */
 
if ((address1 = mmap((caddr_t) 0, inFileSize, PROT_READ, MAP_SHARED, fid1, 0))==MAP_FAILED) 
{perror("mmap failed"); printf("errno=%d\n", errno);}
printf("actual mapped address1=0x%x\n", address1);
 
base = ptr = (char *) (address1+500);
for (i=0; i< 8; i++) buf[i] = *ptr++; buf[8]='\0';
printf ("Reading Source: %s\n", buf);
 
/* Following attempt to write to source file should cause segmentation
error, because of read_prot, although the file is onped RDWR.*/
 
/*
ptr = base;
for(i=0; i<8; i++) *ptr++ = data[i]; 
fprintf(stderr, "Writing to source: %s\n", base);
*/
/*----------------------------------------------------*/
if ((address2 = mmap((caddr_t) address1, 10000, (PROT_READ | PROT_WRITE),  MAP_SHARED, fid2, 0))==MAP_FAILED) 
{perror("mmap failed"); printf("errno=%d\n", errno);}
printf("actual address2=0x%x\n", address2);
 
/* Make first page write only */
if ((mprotect(address2, pagesize, PROT_WRITE))<0) printf("protect write error=%d\n", errno);
/* make second page read only. */
if ((mprotect(address2+pagesize, pagesize, PROT_READ))<0) printf("protect read  error=%d\n", errno);
 
 
/* Dump one page of memory space to output file */
if ((msync(address2, pagesize, MS_SYNC))<0) printf("m-sync error=%d\n", errno);
munmap(address1, inFileSize);//remove memory space
munmap(address2, outFileSize);
 
 
/* I/O is possible using file descriptor, but not through alloacted space now. */
close(fid1);
close(fid2);
}