Wednesday, May 28, 2014

Proper use of stand and rand() in c++

I had many issues with stand and rand when trying to generate truly random series of numbers.
I used in for or while loop and this should be written correctly.
Here is the correct way to write randomly generated functions in C++:

            srand(time(NULL));
            while ( j < N ) {
                std::array<int,Tsp::N> genes = {};
                rand();
                index = rand() % N;
                if ( std::find(std::begin(genes), std::end(genes), index) == std::end(genes) ) {
                    genes[j]=index;
                    j++;
                }
            }




Sunday, May 25, 2014

Genetic Programming vs Genetic Algorithms


Genetic Programming vs Genetic Algorithms 
The genetic algorithm has several disadvantages, for example the length of the strings
are static and limited, it is often hard to describe what the characters of the string means
and the meaning of the characters is limited to whatever they are specified to be. These
problems could perhaps be overcome but a better solution brings back the original
question: How can computers learn to solve problems without being explicitly
programmed? Genetic programming suggests the following solution: simply apply the

genetic operations on computer programs to evolve better and better programs.


There are a lot of frameworks that you can start with Genetic Programming immediately however you do not need them to start using Artificiell Intelligence. You simply need to know about a few core knowledge to solve problems like Traveling Salesman Problem known as TSP in computer world.


Selection in Genetic Algorithm
There are different methods to select chromosomes from a population to be parents for crossover to next generation.

Roulette wheel selection


Boltzman selection
Tournament selection
Rank selection
Steady state selection


Crossover and Mutation Probability
This is two parameters of Genetic Algorithms.
Crossover takes place once the chromosomes are selected based on one of the methods above.
This will produce offsprings. A crossover of 100% or 1.0 means that all of the chromosomes will be used for reproduction, i.e. there is no servivors. Empirical studies shows that a Crossover probability of between 0.65 and 0.85 will achieve better results.If it is 0% nothing is changed and the offspring is a exact copy of one of the parent.
Some researchers recommend decreasing the cross-over probability and increasing the mutation probability in depending on the number of performed generations. Others say that it is good if the genetic algorithm has a dynamic cross-over probability that changes in dependence to spread value inside the selection mechanism.

Resources:
http://en.wikipedia.org/wiki/Genetic_algorithm


Behavior Driven Development, BDD for Java


BDD - Behavior Driver Development is an effort to meet the problems faced in Software development described by Eric Evans in his book Domain Driven Design [Eva03]



A project faces serious problems when its language is fractured. Domain experts use their jargon while technical team members have their own language tuned for discussing the domain in terms of design... Across this linguistic divide, the domain experts vaguely describe what they want. Developers, struggling to under- stand a domain new to them, vaguely understand.” 

To avoid any misunderstanding between the domain experts and developer there is a need of a language that can be understood by everyone involved in the project to reduce any misunderstanding.
Gherkin is a such language.

Stateless
Each scenario must be able to be executed independencely of any other scenarios. This is bad practice genereally to have any dependencies to other scenarios. Asume always that the system is in a blank state, hence stateless. In "Given" you can set up all the state you need for the particular state.

The scenario should not contain technical details how to retrive values for example, but what
the system in a given state expected to do things.





Available Behavior Driven Design Framework for Java:

Cucumber-jvm
Spock
JBehave


Link Resources :
Java based bdd with Cucumber
Business Readable DSL by Martin Fowler


GLUT's display func called only once

Suppose you have the following code in your main:

void Update(void){_tsp.update();}
void Display(void){_tsp.DrawCities();}
// your other code here
int main(int argc, char * argv[])
{
...
..
// your other code here
...

glutDisplayFunc(Display);  // callback for draw function
glutIdleFunc(Update);  //callbak for update function

You observe that the Display called only once and Update on all loops.
If you want both Display and Update be called, add

glutPostRedisplay(); 

In yor Update function at the end. This will trigger callback for the display-function.

Friday, May 23, 2014

Absent Code attribute in method that is not native or abstract in class file javax/persistence/GenerationType

When you use JUnit/Arquilian and get the following error when running in maven:
Tests in error:
 .....: Absent Code attribute in method that is not native or abstract in class file javax/persistence/GenerationType

You may have this issue when you have higher Java version and you may not have this issue if you run other Java versions of SDK installed. To  prevent those dependencies add the following in maven:

<dependency>
            <groupId>org.glassfish.main.extras</groupId>
            <artifactId>glassfish-embedded-all</artifactId>
            <version>4.0</version>
            <scope>test</scope>
        </dependency>


Find latest version of org.glassfish.main.extras on
http://mvnrepository.com/artifact/org.glassfish.main.extras/glassfish-embedded-all



Sunday, May 11, 2014

Methods to find suitable solution (ie. not necessarily the best solution),

Finding best Solution in Computer Science

There are methods to find solutions that is not necessarily best solution, but can find a best possible solution in a Serach Space (i.e. the space of all feasible solutions).
There are problems that can not be solved by traditional way and such problems are called NP-problems.
Problems that can not be solved in polnomyal time is such problems that need another way to solve problems. NP stands for Non-determinsitic polynomial and we can only "guess" a solution and then check the solution.Nobody today knows if there is fast exact algorithm. Proving or disproving this is a task for future reseracher. To find such algorithm today they use the following methods and Genetic Algorothm is one of them.

Hill climbing
Tabu search
Simulated annealing
Genetic algorithm


Look at Wikipedia to find out more about the finding solutions.

Saturday, May 10, 2014

no matching function for call to ‘glutinit’ error in Xcode


When you create a default project in Cocoa template of Xcode the main function is generated accordion to the following:
int main(int argc, const char * argv[])

You can solve this if you review your main-function and remove "const" from the following main function:
int main(int argc, char * argv[])
and build again.




Why you should use Iced Coffee Script (IcedCoffeeScript) to CoffeeScript


Iced Coffee Script aimed to make programming asynchronous easier and is a superset of CoffeeScript.
The main advantages are using Wait, Defer to make for example parallel searched to Twitter as you can find an example of it at the link: Iced Coffee Script. You can use a fewer lines to achieve your goal with asynchronous calls than in CoffeeScript. Take a look at the code examples provided in the link above.

In Atom Editor from Git there is a support for Iced Coffee if you wan to experiment wit it further.


Thursday, May 8, 2014

java.lang.RuntimeException: Could not create new instance of class org.jboss.arquillian.test.impl.EventTestRunnerAdaptor



java.lang.RuntimeException: Could not create new instance of class org.jboss.arquillian.test.impl.EventTestRunnerAdaptor

You get this issue if you have profiles-elements in your POM-xml and no one if the profiles are activated.
You can either activate one of the Profile Id using command-line:
$  mvn clean install -Pjboss-managed

or use your favorite IDE to choose one of the profiles to run.