Monday, June 17, 2013

Demspter-Shafter theory application

Dempster-Shafer theory in decision making
Let us define
A= “not crowed at all”
B= “normal”
C=“ very crowed”
Then whole Environment E={A,B,C}. 


Vehicle       Message content      Message reputation score

V1                    C                                0.6       
V2                    C                                0.8      
V3                    A                                0.7      
(1) considering V1and V2(@ here means Unknown)
                                m1({C})=0.6           m1({@})=0.4
 m2({C})=0.8        {C}  0.48                 {C}   0.32
 m2({@})=0.2       {C}   0.12                {@}   0.08
Thus  m1+m2({C})=0.48+0.32+0.12=0.92         m1+m2({@})=0.08         

(2)  take V3 into consideration
                                       m1+m2 ({C})=0.92           m1+m2 ({@})=0.08
 m3({A})=0.7               NULL  0.644                          {A}   0.056
 m3({@})=0.3                {C}  0.276                             {@} 0.024
Which means m1+m2+m3({NULL})=0.644,  m1+m2+m3({A})=0.056
                        m1+m2+m3({C})=0.276,  m1+m2+m3({@})=0.024

(3) standardization because of Empty set
Because the m1+m2+m3({NULL})=0.644 should be 0, so after standard process:
m1+m2+m3({A})=0.056/(1-0.644)=0.157
m1+m2+m3({C})=0.276/(1-0.644)=0.775
m1+m2+m3({@})= 0.024/(1-0.644)=0.068
So, confidence interval :
A=[0.157, 0.157+0.068]=[0.157,0.225]
C=[0.775, 0.775+0.068]=[0.775,0.843]

Bad case for Dempster-Shafer theory
two doctor and one patient
1st doctor diagnoses the patient was attacted by tumor with 0.9 propability, and cancer with 0.1;
2nd doctor diagnoses the patient was attacted by concussion with 0.9 propability, and cancer with 0.1;
thus
                                                     m1({tumor})=0.9                  m1({cancer})=0.1
 m2({concussion})=0.9                {NULL}  0.81                   {NULL}   0.09
 m2({cancer})=0.1                       {NULL}   0.09                  {cancer}   0.01

=>    m1+m2({NULL})=0.81+0.09+0.09=0.99         m1+m2({tumor})=0.01  
       
       K= m1+m2({NULL})=0.99
       after standardization:
       m1+m2({tumor})=0.01/(1-K)=1
      Which would be greatly contrary with our intuition.
       So, when K is equal or very close to 1, we should just cancle the standardization

Saturday, June 15, 2013

shared folder between virtualbox and host

1. Create a folder on host like this:
    F:\Shared
2.On VirtualBox Manager:
   Settings-Shared Folders-Add shared folder
   Folder Path: select the one you create above
   Folder Name: use the default one
   tick "Auto-mount"  and "Make permanent" and then save it.
3.Open terminal on Ubuntu on VirtualBox:
   sudo mount -t vboxsf Shared zhiguang/Win_Lin/
   # zhiguang/Win_Lin/ is the corresponding folder for Shared, you should create it before running this command

   then once you put files in  F:\Shared, you can share and edit them in zhiguang/Win_Lin/ immediately.
  

"The system is running in low-graphics mode" for Ubuntu on VM VirtualBox

When I installed Ubuntu Desktop 12.04 LTS into VirtualBox, it prompts "The system is running in low-graphics mode". I tried 6 soutions which solved the problem for other people but not me. Although I do not the exactly intrinsic reason, the following command finally served me well:

sudo apt-get update
sudo apt-get upgrade
sudo reboot
I hope it can also help you.

Tuesday, June 11, 2013

Full screen for ubuntu on VM VirtualBox

I have VM VirtualBox on Win 7, and Ubuntu on VM VirtualBox. By default, the ubuntu could not be displayed as fullscreen. But there is way to make it seem as the ubuntu os is directly installed on your PC hardisk.

1. Installing Guest Additions for full screen

   [Device]--->[Install Guest Addtions]

2. After some configure by the machine, you can open the Guest Additions folder which should be on your Desktop in Ubuntu with name like this: VBOXADDITIONS_#

3. Open terminal, and cd into this folder VBOXADDITIONS_# by:

cd  /media
cd  VBOXADDITIONS_#
sudo sh VboxLinuxAdditions.run

4. Restart the ubuntu.
you can see the ubuntu is enlarged filling up the whole VirtualBox. However the VirtualBox window frame still exists, you can implement the real full screen by HOST+F,  which would make  the VirtualBox window frame  disappear. Another  HOST+F will bring the window frame back again. By default in Ubuntu, the HOST key is the Right Ctrl

Monday, June 3, 2013

ANSI C (2)

12. recursion
#include <stdio.h>

void binary_to_ascii( unsigned int value)

{

   unsigned int quotient;

   quotient=value/10;

   if(quotient!=0)

      binary_to_ascii(quotient);//recursion

   putchar(value%10+'0');



}



value=4267  quotient=?

value=4267  quotient=426

value=426  quotient=42

value=42  quotient=4

value=4  quotient=0

value=42  quotient=4

value=426 quotient=42

value=4267  quotient=426


13.variable parameter

#include <stdarg.h>

float average(int n_values,...)

{

  va_list var_arg;

  int count;

 float sum=0;



 var_start(var_arg,n_values);

for(count=0; count<n_values;count+=1)

{

   sum+=va_arg(var_arg, int);

}

va_end(var_arg);

return sum/n_values;

}


14. array
//(1)

int array[10];

2[array]  ==  *(2+(array)) ==array[2];



//(2)

int array[10], a;

for(a=0;a<10;a=a+1)

     array[a]=0;



int array[10], *ap;

for(ap=array; ap<array+10;ap++)  // more efficient here than array

   *ap=0;

// when   accessing the array elements by a fixed increment,  using pointer would more efficient than that of array index;



//(3)

#include <stdio.h>

#include <stdlib.h>

void my_parameter1(char string[]);

void my_parameter2(char *string);

int main()

{

    char arr[]="zhiguang";

    char *arr1="zhiguang";

    printf("%d\n",sizeof(arr));//9--array: 'z'  'h' ... 'g','\0'

    printf("%d\n",strlen(arr));//8--array:  'z'  'h' ... 'g'

    printf("%d\n",sizeof(arr1));//4--pointer size

    printf("%d\n",strlen(arr1));//8-- the content length the pointer points to

    my_parameter1(arr);//4--pointer size, it is not 9, because it only pass the array as pointer in fact

    my_parameter1(arr1);//4--pointer size

    my_parameter2(arr);//4--pointer size

    my_parameter2(arr1);//4--pointer size

    return 0;

}

void my_parameter1(char string[])

{

   printf("%d\n",sizeof(string));

}

void my_parameter2(char *string)

{

   printf("%d\n",sizeof(string));

}



//(4)

    char arr[]="zhiguang";

    char *arr1="zhiguang";//constant

    arr[2]='w';// ok

    arr1[2]='w';//error, because it is constant

    *(arr1+2)='w';//error, because it is constant



//(5)

int matrix[3][10];

matrix;//pointer, points to the first whole row, 10 elements

matrix+1;//pointer, points to the second whole row, 10 elements

*(matrix+1);// it is an array, also the points to the first element in second row;

*(matrix+1)+5;//pointer, points to matrix[1][5]

*(*(matrix+1)+5);//value, content of matrix[1][5]

*(matrix[1]+5);//value, content of matrix[1][5]

matrix[4,3]==matrix[3];//because of ','



//(6)

int vector[10], *vp=vector;//ok

int matrix[3][10], *mp=matrix;//error;

int (*p)[10]=matrix;//ok, p is a pointer first, points to an array with 10 elements;

int *pi=&matrix[0][0];//ok

int *pi=matrix[0];//ok



//(7)

// if the parameter mat is 2-D array, you can declare the function as:

void fun(int (*mat)[10]);//ok

void fun(mat[][10]);//ok

//however, the following is wrong

void func2(int **mat);//error



//(8)

char  *keyword[5];// an array, each element is a pointer;

char const *keyword[]={"do","for","while"};

// the best way to determine the length of this array is:

sizeof(keyword)/sizeof(keyword[0]);



//(9)
void function0(int);
void function1(int);
void function2(int);
int main(void)
{
   void (*f[3])(int) = {function0,function1,function2};
//f is first an array, each element is a pointer, pointing to a func with an int parameter
   return 0;
}


15. string
//strlen returns an unsigned in, so

if(strlen(x)>strlen(y)) //always right

if(strlen(x)-strlen(y)>=0) //sometimes, error

if(strlen(x)>=10)//always right

if(strlen(x)-10>=0) //sometimes, error


16. void *
//any type pointer could be converted to void*, and that is why

void  *memcpy(void *dst, void const *src, size_t length);

void  *memmove(void *dst, void const *src, size_t length);

void  *memcmp(void *a, void const *b, size_t length);

//...


17. structure
//(1)

struct{

   int a;

   int b;

}x;



struct SIMPLE{

   int a;

   int b;

};

struct SIMPLE x;



typedef struct {

   int a;

   int b;

} Simple;

Simple x;



//(2)

typedef struct{

    int a;

    struct SELF_REF3 *b;//error, there is no SELF_REF3 yet;

    int c;

} SELF_REF3; 



typedef struct SELF_REF3_TAG{

    int a;

    struct SELF_REF3_TAG *b;//OK, there is SELF_REF3_TAG already;

    int c;

} SELF_REF3_TAG; 



//(3)

// the following is error

struct A{

struct B *partner;

};

struct B{

struct A *partner;

}

//the following is correct

struct B;// declare first, even if it is not complete

struct A{

struct B *partner;

};

struct B{

struct A *partner;


18. memory
//(1)

void *malloc(size_t size);

void free(void *pointer);

// if malloc failed, it would return an NULL



//(2)

int *pi;

pi=malloc(25*sizeof(int));

if(pi==NULL)

{

   printf("out of memory!\n");

   exit(1);

}



//(3)

// the pointer passed to free must be generated by malloc, calloc, or realloc, not others;



//(4)

// simplest way of memory leak

for(int i=0; i<10;i++)

    int *p=malloc(25*sizeof(int));

Sunday, June 2, 2013

ANSI C (1)

1. logically delete the command
# if 0

   statements

#endif

2. exit
exit(EXIT_SUCCESS);// stdlib.h

exit(EXIT_FAILURE);// stdlib.h

3.array variable as parameter
array variable, as function parameter, will pass the reference to the function, other kinds of variables will pass the values to the function
4.NUL  NULL
NUL=='\0' ;// the ASCII character 

NULL==0; // the pointer

5. read
#define MAX_INPUT 1000

char input[MAX_INPUT];

int ch;

while(gets(input)!=NULL)//read the line

{

}

while((ch=getchar())!=EOF&&ch!='\n')//read the character 

{

}

6. standard structure
#include <stdlib.h>

int main(void)

{

   return 0;

}

7.typedef  define
typedef char   *ptr_to_char;

define  p_ptr_to_char char*;

8 const pointer
int const *pci ; // you can modify pci, but not *pci

int *const pci;  // you can modify *pci, but not pci;

9 switch
switch(command)

{

  case 'A':

      add_entry();

      break;

  case 'B':

      delete_entry();

     // break;

  case 'C':

      edit_entry();

      break;

}

if command=='B', then delete_entry()  and  edit_entry() would be implemented, because there is no 'break' for case'B'. So pay attention to all the 'break'.
10 sizeof
it is an operator, not function
sizeof(int); // return the length by bytes

sizeof x;

11. pointer to string
//  string length

#include <stdlib.h>

size_t strlen(char *string)

{

   int length=0;

   while(*string++ !='\0')

          length+=1;

   return length;

}


// find the char in strings

#include <stdio.h>

#include<assert.h>

#define TRUE 1

#define FALSE 0

int find_char(char **strings, char value)

{

   assert(strings!=NULL)



   while(*strings !=NULL)

  {

       while(**strings!='\0') 

       {

            if(*(*strings)++==value)

                return TRUE;

       }

       strings++;

  }

   return FALSE;

}



Thursday, May 30, 2013

Java basic

1. in one .java file, there could be as many class as you wish, but there should be at most 1 public class, and this class name should be the same with the file name.

// B.java

class A

{
}

public class B

{
}

class C

{
}

2. for boolean, there are only true and false, no -1, 0,1
in C++, you can write like this:

while(1)

{
}

but in Java, while(1) is wrong, and you can only use:
while (true)

{
}


3. define array
//-1

int num[];

num=new int[3]; //ok;

int num[]=new int[3];//ok

int num[]={1,2,3};//ok

int num[]=new int[]{1,2,3};//ok

int num[]=new int[3]{1,2,3};//error

int num[3]; //error



//-2

int [][] num;

num=new int[2][];

num[0]=new int[3]; //3 elements

num[1]=new int[4]; //4 elements



4. for loop
for(int i=0; i<5; i++)

{
   int a=i; //ok

}

int b=i;// error, i is not defined out of the for loop

5. print hex number
  int hx=0xffffffff;
  int hc=hx<<2;
  System.out.println(Integer.toHexString(hc));
6. default value
int   0;

char '\0'

boolean false

object  null

7.  same name for parameter and member
class Point

{
    int x,y;

    void func1(int x, int y)

   {

      x=x; //parameter to parameter, not what we want

      y=y; //parameter to parameter, not what we want

   }

  

    void func2(int x, int y)

   {

      this.x=x; //parameter x to member x

      this.y=y; //parameter y to member y

   }



}
8. call another construction method
 class Point

{
    int x,y;

    Point(int a, int b)

  {

     x=a;

     y=b;

  }



  Point()

 {

    this(3,4);// call another construct method, and should be the first statement

 }

}

9. static
static methods could call static methods and access the static variable, could not call the non-static methods or access variable;
while  non-static methods  could call static methods and access to static variable.
10.. final variable

In most cases, the final variable should be initialized when it is defined.
class Point

{

    final double PI=3.1415926;

}



one exception:

class Point

{

    final double PI;

    Point()

  {

     PI=3.1415926; 

  }

}

however, if the final variable is also static, you must initialize it when defining it:
static final double PI=3.1415926;

11. call parent method

public class MyAnimal
{
   MyAnimal()
  {
       System.out.println("I am Animal! without parameter");
  }

     MyAnimal(int param)
  {
       System.out.println("I am Animal! with parameter");
  }

}


 class MyFish extends MyAnimal
{

      public MyFish()
   {
      // in this place, super()is called even if you do not write it;

     // so if there is no MyAnimal() at all, error would happen.

     // in that case, you could use  super(0) if  MyAnimal(int param) is defined

     // or you could redifine the MyAnimal() 

      System.out.println("I am fish! without parameter");
   }

    public MyFish(int param)
   {
      //in this place, still super(),not super(0), is called even if you do not write it;

     // to call MyAnimal(int param), should write super(0) in this place

     // 0 represents the parameter, you can use anyone you like
      System.out.println("I am fish! with parameter!");
 
 } 

    public void breathe()
    {
          super. breathe(); // use this call  parent  breathe();

          System.out.println("Fish breathe!");

    }

}
12. polymorphic

class Parent
{
    void fun1()
  {
     System.out.println(" Parent 1");
  }
   void fun2()
  {
     System.out.println(" Parent 2");
  }
}

class Child extends Parent
{
  
   void fun1()
  {
     System.out.println(" Child 1");
  } 
  public static void main(String[] args)
 {
       Child cd=new Child();
       Parent pt=cd;//Ok
       pt.fun1();    // result: Child 1, because of new Child();
       pt.fun2();    // result: Parent 2, because Child dose not override fun2()

       Parent pt1=new Parent();//Ok
       Child cd1=pt1;//error, can not convert from Parent to Child
 }
}