Friday, December 6, 2013

My Funny Lisp book

           
         Artificial intelligence was one of the subject that made a really big influence on me. Understanding and Implementing the AI algorithms and concepts are really interesting eve though it is very much hard to understand. I learnt "Lisp" as a programming language that we can implement these concepts with minimum effort. 
             Since Lisp has its own style it was really hard to adapt to that style.Almost all the people got confuse with this style. I was one of the student that got overmuch confused regarding lisp and score 0.00 several times in AI class. But finally i worked hard and understood the style and some of essential concepts of the Lisp programming language. But until the final movement, some of my friends unable to understand the thing. so i prepared following document for them 


                The funny thing was i was one of the student who had low marks for the continuous assignment. But i made a document for others. Finally my AI lecturer gave me 56 marks out of 60 because i was one of the student who implemented "Generic Algorithm" using Lisp among few of students who got succeed.

My Generic Algorithm        

Saturday, October 5, 2013

Chop-down algorithm

Day today programming we need to find solutions for various logical problems. today i got a task to create a program to find triangular numbers between any given two numbers. I implemented it using python. Following code shows how i did it.

   
import math,sys

#create a list with numbers
def create_list():
    num_list=[]
    for num in range(int(input("insert start point: ")),int(input("Insert end point: "))):
        num_list.append(num)
    return num_list

#function to find triangular numbers
def get_triangles(numlist):
    triangles = []
    for i in numlist:
        if (check_triangle(i)):
            triangles.append(i)
    return triangles

#function to check number is triangular or not
def check_triangle(n):
    return math.sqrt((8*n)+1).is_integer()

#fuction main to run the process
def main():
    numlist = create_list()
    print(get_triangles(numlist))
Even though it seems like the task is completed it was not. I tried it with the range of  0 - 100000000(1*10^8) numbers . it was horrible to see the out put and time taken to complete the task. my computer was stuck for few minutes and  unable to complete the task. i was tried with 10000000 (1*10^7). then i saw that 95% of memory  was occupied by the program. Then i modified the program as follow
   
import sys,_thread,time,math

#function to create a  list with numbers
def create_list():
    num_list=[]
    for num in range(int(input("insert start point: ")),int(input("Insert end point: "))):
        num_list.append(num)
    return num_list
   

#determine 3 cutdownpoints
def cut_down_points(count):
    if(count%4==0):
        return [count*0.25,count*0.5,count*0.75]
    else:
        while(count%4!=0):
            count = count+1
            first = int(count*0.25)
            second = int(count*0.5)
            third = int(count*0.75)
        return [first,second,third]

#cutdown list to 4 peces
def cutdown(num_list,cut_points):
    return [num_list[0:cut_points[0]],num_list[cut_points[0]:cut_points[1]],
            num_list[cut_points[1]:cut_points[2]],num_list[cut_points[2]:]]

#function to find triangular numbers
def find_triangle_numbers(numlists):
    triangles=[]
    for i in numlists:
        if(check_triangle(i)):
            triangles.append(i)
    print (triangles)
    #return triangles
            
#function to check number is triangle or not
#if number x is a triangle 8x+1 need to be a perfect squre number 
def check_triangle(n):
    return math.sqrt((8*n)+1).is_integer()

#start 4 threads to find triangles
def find_all_triangles(numberlist):
    #print (numberlist)
    num_set_01= _thread.start_new_thread(find_triangle_numbers,(numberlist[0],))
    num_set_02= _thread.start_new_thread(find_triangle_numbers,(numberlist[1],))
    num_set_03= _thread.start_new_thread(find_triangle_numbers,(numberlist[2],))
    num_set_04= _thread.start_new_thread(find_triangle_numbers,(numberlist[3],))

#main function to run the process 
def main():
    numbers = create_list()
    temp_cutdown_point_list = cut_down_points(int(len(numbers)))
    cutdown_point_list=[int(i) for i in temp_cutdown_point_list]
    cut_list = cutdown(numbers,cutdown_point_list)
    find_all_triangles(cut_list)
I sow that the program was not stuck as previous and did the task smoothly with a minimum execution time. After careful consideration i defined following algorithm to accomplish this kind of tasks.

NAME : Chop-down algorithm 
TASK : increase the efficiency of searching tasks  in vary large domains
ALGORITHM :
assume Domain size is equal to M where M > 10e6
Start
   chop down the domain in to n parts where n <= M/[(M*5)/100]
   create n number of  individual threads (avoid resource killing)
   assign single sub domain to each thread
   perform searching
End



Eg:  assume that we need to find triangular numbers between 0 and 10e6
             STEP 01 :
             n = 10e6/[(10e6 * 5)/100] =  20
             so we need to break the domain in to 20 parts
             STEP 02 :
             create 20 threads
             STEP 03 :
             assign single sub domain to each thread
                               Thread_01 >>>> 0  -  5*10e4
                                Thread_02>>>> 5*10e4  -  10*10e4
                                 .
                                 .
                                 .

             STEP 04 :
             Start threads to perform search
                               EG.  Thread_03 ->
                                             Search each value in its domain and print triangular values
             End


                         
                             










Friday, October 4, 2013

Algorithm to find GCD (Greatest common divisor )

What is GCD ?
 The largest integer that divides a set of numbers

Eg . GCD for 15 and 10 is 5

To find this prophetically Eucilds algorithm can be used. it is very much easy and fast way to find GCD within the programs.

Algorithm :
assume "a" and "b" are the numbers we need to find GCD

A=a and B=b
while B>0 do
         R= A mod B
         A = B
         B = R
return A

Following python code shows how it can be used in a python program  
#Find GCD with Eucilds Algorithm
def FindGCD_Eucilds(num_one,num_two):
    A=num_one
    B=num_two
    while (B>0):
        R=A%B
        A=B
        B=R
    return A

print(FindGCD_Eucilds(15,5))

Wednesday, October 2, 2013

Useful Python regular expressions

Regular expressions:

In general we are using expressions to represent common format of a mathematical domain. The term "Regular expression" also engage with a job exactly similar to that. We can see regular standard patterns of strings such as email addresses, phone numbers, ID numbers etc. When we talking about software developing some times we may need to use this formats in order to verify the corresponding insertions of users. That is the place where "Regular expression" plays it's role. we can define a pattern for the day today string patterns in order to verify them.

Regular expression designing is some kind of a art which has a challenge. Following patterns are designed by my self in order to use in python programs.
           

import re

#regular expression to find email addresses ie. sandaru.weerasooriya@gmailcom
regmail = r'(?:[a-z]|[A-Z])+(?:[.]?)(?:[a-z]|[A-Z])+[@][a-z]+[.][a-z]+(?:[^ *])'
founded_mails=re.findall(regmail,"Sandaru.wEerasooriya@gmail.com amalka@gmail.com")
print (founded_mails)
 
#regular expression to identify phonenumbers ie.international and local mobile number formats
regnum = r'[/+]?[0-9][0-9][0-9][-]?[0-9][0-9][0-9][-]?[0-9][0-9][0-9][0-9][0-9]?'
founded_numbers = re.findall(regnum,"071-4969763 077-496-9763 +94714969763 0714969763 0717896678")
print(founded_numbers)

#regular expression to identify only mobitel numbers
regMobitel = r'(?:(?:[/+][9][4][7][1])|(?:[0][7][1][-]?[1-9][1-9]))(?:[0-9][-]?[0-9][0-9][0-9][0-9][0-9]?)'
founded_mobitels = re.findall(regMobitel,"071-4969763 077-496-9763 +94714969763 0714969763")
print(founded_mobitels)

#regular expression to identify only dialog numbers
regDialog = r'(?:(?:[/+][9][4][7][7])|(?:[0][7][7][-]?[1-9][1-9]))(?:[0-9][-]?[0-9][0-9][0-9][0-9][0-9]?)'
founded_dialog = re.findall(regDialog,"071-4969763 077-496-9763 +94714969763 0714969763")
print(founded_dialog)

Monday, July 22, 2013

Software Engineering from Sandaru's eye

   I found this from the internet. Yes this is true. Because most of the people who use Software Engineer as him/her designation are unaware about the meaning of it. Why this is happening

   Software engineering is not only the coding. Even a person who don't know any thing about the programming languages can make an application by copy and past from the internet. Can we call those applications as complete?

   Most of the people think software engineering is a competition which depend on programming languages. and cording efficiency. but according to my thoughts it is more than that. Programming language is the component which has least priority within the software engineering process. 

        As the software engineer first we need to aware about the problem domain. for this they need to stand on the ground not on the sky.then only they can identify the problems which can apply the software solutions. Most of software engineers sit front of the computer and try to find the solutions for every problem. and most of people categorize them as best and worship until recognize there failures. So first thing need to do by software engineer is switch off the computer until he experience the problem domain. For the better understanding he need to be a well socialized person who has grate communication skills. then only he can suck out all the impotent criteria from the domain beginning from the root level. 

      Rather than imaging the problem experiencing it is much effective. So Software engineer need to be a practical man who can adopt with any kind of situations.People who has better team playing skills and Listening skills can adopt with any kind of situation.

      Next important thing is sort out the problems along with the time. Just we herd the problem we come up with the problems arising along the software developing. but it is not the case we need to consider. in practical live we have to manage several problem faces. family management, entertainment and health management are also need to be considered as problem related to the software engineering. To do any king of work we need to keep attention on it for that we  need to have clear mind. so to come up with a good and effective software, software engineer need to have a clear mind. To accomplish this target software engineer need to be a good manager. Good sorting skills indirectly case to build a good algorithms. 

      Framing is the next thing which need to overcome by the software engineer. any kind of a narrow minded person cannot be considered as engineer. he need to identify the opportunities which can apply his technological knowledge by baring a calculated risk to expand the Performance of the software system. Person who just following someone cannot accomplish this target. So software engineers need to have good backbone to bare a risk.

     finally software Engineer need to have an updated technical knowledge. this is not refer for the programming languages but about the best practices and concepts of software engineering. When we have the concepts it is nothing to adopt to any kind of a programming language. Algorithm designing and resource utilization concepts are much more important for software engineers.

     According to my knowledge and vision for avoid foolish and useless implementation and there continuity those are the evaluation criteria software engineers need to have more than skills to implement any thing that given.                 

           

LINQ with Microsoft SQL

      LINQ Stand for Language Integrated Queries which is introduced by Microsoft for better database programs. with this brilliant technology we don't need to worry about connection setups and query writing.

       First we need to create the database and the tables. then we can add a "linq to sql class" from new items menu. just after drag and drop the tables on to that class we have a complete class which refer to the relevant database. Now no need to write any query, creating an object of that class will enable you to do any thing to the database without Queries on your hand. 

For the deep undesirability refer following links 

1.LINQ to SQL
2.LINQ Query Expressions  

Sunday, May 5, 2013

Phone Gap

Phone gap is an marvelous technology that can be used to create web application for lot of mobile platforms. I tried it with android.

What is the specialty ?
 With phone gap we don't need to have deep knowledge about relevant platform. you just need to have html,css,java script  knowledge. as far as the deepness your knowledge about those and additionally about PHP and jqery you're more success on phone gap. siply you can code and activate the application as a web page.

Benefits ?
We can simply develop an attractive application with minimum effort with compare to other methods.
As we are in a world leading to the online mobile application world, it is much more compatible to archive such tasks.
There are minimized bottlenecks to develop web based mobile applications.

as i experienced there isn't any kind of different between android java web application and android Phone gap application. But we need to think about the speed when we using the j query and images for additional animations.

Reference : PhoneGap Home

Sunday, April 7, 2013

Why we love Lisp

There are lots of articles on the topic of  "Why we hate lisp" but sundry on the "why we love lisp" with compare to the first topic. Simply i fell in love with lisp because of its power and simplicity. no unnecessary rules no briers to implement what is on mind, simultaneously minimized language limitations. 

For example think about java to print "hello world" on the CMD we need to write bunch of codes. but in lisp single code can print it 

(print "Hello World")

someone can think same thing can be done with python. yes but there also lot of difficulties while we handling knowledge bases in order to implement AI concepts. and also i think Python is a copy of those valuable concepts on lisp.    

List structures are the basic of the Lisp which is most amazing feature in the lisp.  Person who needs to understand the power of lisp simply go through the Lists can be elaborate it more than ever. List is the key of lisp which enable the Lisp features to the user. 

Not only those things Lisp has more flexible features which can use to work with other programming languages rather than other languages. 

According to most articles main problem in the lisp is brackets. but it is none when we use it with a text editor like "Sublime Text"  or "Notepad ++" which indicate the matching brackets with color changes . And next thing is they specializing is "difficult to understand by others" and   " lack of alternative solutions" . I never ever seen a language which has that much of alternative paths to archive a single task other than lisp. There for i wonder what they are saying .

 "difficult to understand by others" has both pros and cons. because its very much hard to copy the codes by others. Because of full of alternative paths functions are depending on the knowledge of the engineer. There for it can't be understand or change by other person easily.    

Thursday, March 14, 2013

MVT & What is Compaction ?

To avoid the main drawbacks, which are define as "memory consuming", and "limitation of processes can be run at a time even though have relevant memory" in MFT(Multiprogramming with Fixed number of Tasks), MVT (Multiprogramming with Variable number of Tasks) mechanism was introduced stage.

in here we are mainly focusing on external fragmentation rather than internal fragmentation. 

consider about the above job allocation table following diagram show the steps of MVT

Here compaction can be consider as some kind of a Defragmentation process which happens automatically in main memory.Compaction is high cost event because CPU needs to stop all the other works in order to do the compaction.  

some body can mislead MVT is not using the internal fragmentation. it is using internal fragmentation in very special occasions.

For example, think that we have 50 byte fragment after compaction. if we use it as external fragment we cannot use it for any process. On the other hand for keep track about the free segments we need to use entries. in this kind of a situation that can be needed space more than the free space on the external fragment there for we are using internal fragmentation. but ideally we consider MVT only use external fragments. 

(ref: video lecture of prof: P.K Bisvas)       

Sunday, March 10, 2013

Can't it be changed

In a normal operating system we can see a process state diagram as follow.
in this algorithm i have seen a special scenario which i want to express as a drawback. After we activated the Process in a situation it needs some user input or an out put of a another process as a input it needs to wait on the" wait state" mean while other processes are executing. After having the needed input, process need change its state to activate. but in nowadays system this is not a direct activity. Again that process need to go to "ready state" queue and wait until chance comes to it. 

According to my opinion this is not an fair activity. why it can't be changed as follow.  
Here we can assign a priority value for each processes which needs to queue on waiting state.Priority value need to have two values as high and low. After completed the relevant input need process need to change to the "check state". At this state priority value need to be checked. If it is high, translation of "Ready state" queue need to be paused and transform "check state" process to active state. If it is normal it will be queued under the "ready state" as normally.

Algorithm :

Transform New State to ready State
Activate the Process
     If need to not be perform "wait State"
             continue until halted state
    else
            put to the waiting state
           check the priority value
           if high
               pause the "Ready state" transformation
              change "check state" to "Active state"
          else(low)
              change the "check" state to "ready state"  

I don't whether this idea is acceptable or not by the OS architectures. But this was the idea that pop up suddenly on my mind while listening to the process state diagram lecture at university.  

Saturday, March 9, 2013

Crossover Algorithm

Crossover is an important and attractive biological function which is happen in natural environment with respect to animal's reproduction procedure. This will case to generate new child who has genes collection of parents plus totally new or extended features. Mutation is the process which specialize the uniqueness while the crossover process. 

Core of the genetic algorithm is containing this concept. but how to do the crossover in genetic algorithm. this is my way to do it.

assumption --> crossover probability = 0.7
Select float "n" below 1.00.
if  n>0.7 return any parent as child
     else do following 
       Select 2 parents randomly.
       Decode them to 10 bit binary codes.
 Eg:     parent A : 1  1  0  0  1  0  1  0  0  1
           parent B : 1  0  1  0  0  1  0  1  0  1

      loop "x" from 0 to length of the bit string (10) select the couple of bits from index "x" of each string.

  Eg:   zeroth element of parent A and zeroth element of parent B (1 1
     
     If both are same use the same as "x" index element of the child 
              else randomly select 0 or 1 as the "x" index element . do this until end of the loop 

  Eg:    parent A : 1  1  0  0  1  0  1  0  0  1
           parent B : 1  0  1  0   1  0  1  0  1
           Child      : 1  0  1  0  1  1  0  0  0  1

        Encode the child bit string 
        Return the children
   
This is the lisp codes that is following above algorithm.

(defun crossover(xA xB);;crossover and produce child
  (let ((b-xA (int-to-binary xA)) (b-xB (int-to-binary xB)) 
  (child (int-to-binary nil)) (random-gene '(#\0 #\1)))
  (loop for y from 0 to (- (length b-xA) 1) do
  (if (char/= (aref b-xA y) (aref b-xB y)) 
  (progn (setf (char child y) (random-item random-gene)))
  (setf (char child y) (char b-xB y))))
(parse-integer child :radix 2)))

Friday, March 8, 2013

Truth of stupidity by Einstein



At the time i found this  grate quote, i thought that this is the thing first we need to learn. we cant do all the things. but we are special for one criteria even for stupidity.

Thursday, March 7, 2013

Agile for Implementations

I think we need to pay more attention on input and output of a program rather than thinking the technology that we are going to use for implementing. we need to have clear idea about the inputs we can make and output we need to get.

Then we can select a programming language, special algorithms (if necessary) and a platform in order to define the output generating procedure.

major wrong thing we do is try to minimize the codes or reduce the time consuming at the beginning. This may case for headache and errors which we cannot identify and correct easily. Yes some times we can do it and reduce the codes. but after complete the program we will fed up. and also probability of identifying the correct super technology is low within this kind of a process rather than concentrating on it latter.

there for i think that its better implement a program step by step.first we can consider the input and out put. then we need to implement methods to get the out put, without thinking about the efficiency or flexibility of the program. i would like to call this as "fool programming method". then some how we have the output. As the next step we can review the program and do the changes for the coding. this can be do several times. first we can remove the most foolish things we did. and next bit lower foolish things.

EG step 1 : Input --> any couple of integers x,y
                  Output --> (x*y)/(x+y)
 
      step 2 : "fool programming method"
                 
                     public void (int x,int y)
                           {
                             int temp1= x+y;
                             int temp2=x*y;
                            double answer = temp2/temp1;
                            system.out.println(answer);
                           }
       Step 3 : review and correct (can be do several times)
                          1.  public void (int x,int y)
                                 {
                                     double answer = (x*y)/(x+y);
                                     system.out.println(answer);
                                 }


                          2.  public void (int x,int y)
                                 {
                                     system.out.println((x*y)/(x+y));
                                 }


                          3.  public double (int x,int y)
                                 {
                                     return ((x*y)/(x+y));
                                 }

I thing this can be more effective than traditional "all in first time" method. this is some what same to agile way using for big software system designing process management. 


Avoid depending on (traditional) Algorithms :-O

Algorithms are important and interesting Part of computer science. Main aspect to reduce the time and resource consuming of the computer.

But i have seen several cons of this concept. Think that you need to search a data within a big database. ah... searching algorithms can reduce the time consuming. now i'm searching what is the best. linear search? m..... no how about the binary search ?.. woow there is a new technology GENETIC ALGORITHM Word is big,Technology is high. i used it. 

But the thing is  we don't even think about a new algorithm we just use it. i think its better to avoid the traditional algorithms. we need to search new concepts. we don't need to make a frames for coding such as "algorithm need to be very fast resource saving one". think that we just coding according to our algorithm. it is very fast than traditional one But resource killer, with compare to the system resources we have. What is the thing we do is neglect it. 

Think, about a situation we have enough compatible resources for that algorithm. then it will be the most powerful algorithm for that scenario.there for we can not measure the algorithm according to its resource using levels.

Some times we neglect some algorithms in order to time consuming but we need to think about the accuracy. For some scenarios we have enough time but accuracy need to be 100%. Even though it has time problem we can trust its accuracy. 

I think its better to plush "time and cost reducing concept" from our minds. All the algorithms has pros and cons. but these are not universal. it depend on the scenario that we are working. According to my opinion its better to use our own algorithms for programming. Make the traditional popular algorithms as referens but don't depend on it.