Wednesday, June 27, 2012

Genetic Algorithm - To find optimum solutions

Genetic Algorithm is an adaptive method to find an optimum solution for a problem. There are some real world problems for which we cannot find a solution using a particular algorithm. But those problems may have solutions. Genetic Algorithm is a suitable method for finding an optimum solution for such a problem.

Genetic Algorithm belongs to the category of Stochastic methods.  Stochastic methods never operate in the same way twice for a particular problem. They are involved with probabilistic operations.

By applying Genetic Algorithms we expect to achieve the "Survival of Fittest". That is among a population of solutions, Genetic Algorithm works in a way such that the fittest solutions are survived and further being improved.

In Genetic Algorithm terminology, "Chromosome" represents the whole solution for a particular problem. Genetic Algorithm operations start with defining a population of such chromosomes, which represent feasible solutions for the problem. A population is called a generation.

To generate the next generation from the first generation, the fitness of the chromosomes are evaluated. This is done using the "Fitness Function". The most fittest chromosomes become candidates for crossover operation, which creates new chromosomes by including the good features from its parent chromosomes. Then they are undergone through mutation, which introduce new features which are not included in the parent chromosomes. This operation is done to maintain genetic diversity.

Thus, the Genetic Algorithm runs through generations, until it achieves the defined optimum level. In each generation, it creates an improved solution compared to previous generation. So the quality of the solution increases through the generations.

There is a computational overhead due to the lot of operations performed on each and every chromosome in the population. So to achieve a considerable performance level, Genetic Algorithm needs some operations to perform in parallel.

Tuesday, March 6, 2012

Some useful commands in C programming

Recently I have used C for one of my projects. Here are some commands which make it easy for me to compile simple C source files.

Once you save the C source code in source_file_name.c file, you can compile it with gcc command.

gcc source_file_name.c 

This command will compile your source code. The output file name will be a.out. But if you need to give the output file a name you prefer, you can simply give the following command.

gcc source_file_name.c -o output_file_name

Then it will write the object code (compiled code) in to the file "output_file_name".

You can then run the file by just giving its name in the terminal.

$ ./output_file_name

But these are two separate commands. If you need to compile and run your C program using just one command, then you can use the following command.

gcc source_file_name.c -o output_file_name && ./output_file_name

This will compile your source code first and if compilation is successful it will execute the program.

Very basic stuff, but useful. Isn't it :)

note : Why we need to use ./ to execute a program ? 

It is because, in Linux operating systems, to run a program we need to specify the directory where that program file is located. Here ./ means "Current Directory". Otherwise the directory should be listed in the PATH system variable.

Monday, December 26, 2011

"Boot-Repair" - Grub installer GUI application

I've recently installed Ubuntu 11.10 along with Windows 7. After installing Ubuntu when the machine restarted, it simply started with windows 7 just as I haven't installed any other OS.

But somewhat different from before, it showed an error saying, "MBR error 3".
And when I searched here and there, I was able to find out there's nothing wrong with Ubuntu installation.  The problem is the grub has not installed properly. So I had to reinstall the grub.

I was able to find a really cool application called "Boot-Repair". Here is how to use it and re-install grub just in few minutes.

First you need to boot Ubuntu from a Live CD. Then you have to install this "Boot-Repair" application. For that, simply enter the following commands one after the other (in a terminal).

sudo add-apt-repository ppa:yannubuntu/boot-repair
sudo apt-get update
sudo apt-get install boot-repair
This will install "Boot-Repair" application, so then you can re-intall grub just by one click. :)

Load the application and you will see an option there to re-install grub. This will repair the above error. When you restart, you will be prompted to choose the OS as you expected.

If you still want to do this manually you can do it as it is in this Ubuntu forum.
http://ubuntuforums.org/showthread.php?t=1893219

Saturday, November 19, 2011

Shell Scripting 2 - What is Shell Scripting ?

In my previous post I've explained what Linux Shell is. Simply it is another program where you can give commands in text mode to get some work done.

However you can write a sequence of commands in a file and tell the shell to execute those commands. Then those files are called shell scripts. (File extension is .sh)

Such a script can be defined as a specific command defined to get a specific work done.

Shell scripting is very useful in a way that, if you want to do a task again and again then you can write the required commands as a shell script and execute it.

Ok, now let's try a simple shell script.

A Simple Shell Script

BASH is the default shell for Linux. Let's script using BASH commands. First we will write a simple script to print "Hello world ! " in the terminal.


You can use any text editor (gedit, vi, emacs, etc.) to write a shell script. Remember the script should be saved with .sh extension. 

Write the following lines in a file and save it as 'hello.sh'.
#!/bin/bash
echo "Hello, World!" 
Now you can execute this hello.sh script.
  1. Open a terminal
  2. Go to the location where you've saved the above file
  3. Then to execute, type, sh hello.sh (Or else you can give execution permission to the above file and then execute it.)
If you have a little touch with shell commands you know the above line no. 2 is enough for printing "Hello, World" in the terminal. So why do we include line no.1 's content ?

It is called a shebang or a "bang" line. In this line we give the absolute path to the BASH interpreter.

Shebang includes the hash sign and an exclamation point character (#!), followed by the full path to the interpreter such as /bin/bash. This will ensure that, the script is using the specified BASH interpreter even if it is executed under another shell.

Note : Giving execution permission to the script.

To give the execution permission to the file you can use the following command.

>> chmod  +x  /path to file

This is a very simple shell script. We can do a lot of things using shell scripting. Hope to bring them one by one in my next posts.

Friday, October 28, 2011

Shell Scripting 1 - What is Linux Shell ?

Do you know what Linux 'Kernel' is? Simply, it is the heart of the Linux Operating System. It manages the resources. (ie. Memory management, File management, I/O management, Device management, etc.) Kernel lies close to the hardware. So the language it can understand is more over low level. 

For humans to work with the operating system, there are human readable commands. Shell is the special program which converts those human readable commands in to the format which Kernel can understand. So shell is also a user program, which provides a more convenient environment for humans to interact with the system. 


Shell executes commands that are given from the keyboard or a file. There are several shells.
  • BASH
  • CSH
  • KSH
  • TCSH
You can access the Shell simply by opening a terminal. Or else you can connect through SSH (Secure Shell).

Note :  All the available shells in the system can be found in /etc/shells file.
            Current shell name is assigned to SHELL variable.

Let's see what is Shell Scripting in my next post.

Monday, October 24, 2011

Vaadin - Including HyperLinks Inside Text

Vaadin is meant for creating Java web applications. It has built in "Link" component which is needed to include hyperlinks. It works as follows;
Link link = new Link("Click Here", 
                                 new ExternalResource("http://vaadin.com/")); 
//
But when we are using this Link component, the entire String (ie. "Click Here" in the above example), becomes the hyperlink.

But if we want something like, only the word "Here" to be the hyperlink and the "Click" to be in normal text, Vaadin has no solution for it. Vaadin doesn't have a component to use for that.


Left side hyperlink is created using "Link" component and the right side hyperlink is created using "CustomLyout"

To include that kind of a hyperlink we have to use Vaadin "CustomLayout" component. Here is how to create a hyperlink using CustomLayout.

With the CustomLayout component we can create Layouts as a template in XHTML.
String text = "Click " + "<a href='http://vaadin.com/'>" + "Here" + "</a>";
CustomLayout customLayout = new CustomLayout(new ByteArrayInputStream(text.getBytes()));
//

Further we can add styles to the above created CustomLayout, just as we are doing for other Vaadin UI Components.

Thursday, October 20, 2011

How to add Syntax Highlighting in Blogger

Do you like to post your code snippets in your blog as below ?
public class HelloWorld {
    
    public static void main(String[] args) {
        
        System.out.println("Hello World");
    }
}

It looks really pretty. SyntaxHighlighter is the solution for it.

You can download the SyntaxHighlighter from here.

You can either upload the necessary files to your preferred location or you can access them from google-code's SyntaxHighlighter project files. But when accessing them from google code project files, css styles need to be copied and pasted in to your blog's HTML template. It's bit risky, instead you can upload the files in to your website. (https://sites.google.com/ is a really cool place where you can easily upload these files).

To enable code syntax highlighting in your blog posts, you need to add few lines to the html code of your template.

To do this, go to Design -> Template -> Edit HTML -> Proceed

Then add the following lines between <HEAD></HEAD> tags. (you can simply insert them just before the </HEAD> tag).
<link href='YOUR-UPLOADED-LOCATION/SyntaxHighlighter.css' rel='stylesheet' type='text/css'/>
<script src='YOUR-UPLOADED-LOCATION/shCore.js' type='text/javascript'> 
</script> 
 
<!--According to the programming language you want to post in your blog-->
<!--include the following lines as necessary-->
<!--Below 2 lines are for java codes and xml codes--> 
<script src='YOUR-UPLOADED-LOCATION/shBrushJava.js' type='text/javascript'>
</script> 
<script src='YOUR-UPLOADED-LOCATION/shBrushXml.js' type='text/javascript'> 
</script> 
Of course you can insert the relevant lines for all the available programming languages in  SyntaxHighlighter. But this will take some more time to load your blog. So include only necessary languages.

Then insert the following lines just before </BODY> tag and save the template. Now the changes to the template are done.
<script language='javascript'>
dp.SyntaxHighlighter.BloggerMode();
dp.SyntaxHighlighter.HighlightAll('code');
</script> 
Now, you can compose a blog post with code snippets. When including code snippets, you should write them between <pre></pre> tags. To do this you have to switch the HTML tab in your blog editor.
<pre name="code" class="java">
...Java code goes here...
</pre>
To insert a Java code you should mention "java" for the "class" attribute. Like this, for all the available programming languages, there is a specific attribute value for "class" attribute. You can find the supported language list and the relevant attribute values from here.
(ie. cpp, c, c++, c#, c-sharp, csharp, css, delphi, pascal, java, js, jscript, javascript, php, py, python, rb, ruby, rails, ror, sql, vb, vb.net, xml, html, xhtml, xslt)

But, remember one thing, you can't include XML, HTML code as it is. < and > symbols will cause issues in displaying your blog post. So replace them with their counterpart escape characters. For replace < with &lt; and replace > with &gt;

You can read more on SyntaxHighlighter from here.

A small note on how to upload a file on to https://sites.google.com/

Once you create a site on https://sites.google.com/ go to More -> Manage site -> Attachments. There you can upload your files. :)