mercredi 16 janvier 2019

Unexpected values after List assignment with random generator

I am working on creating a Fruchterman Reingold graph generator, and I am currently stuck on a value assignment issue. The following code assigns unexpected values on the "disp" list and the hashmap it is assigned in, named "displacement". The results are shown below. I can't figure out where the problem lies, but I expect it to be located in the final loops. I should mention all appropriate imports have been made, and that this is just a part of the code.

Thank you so much for your time! I hope that I have presented my problem in the right way!

public static void main(String[] args) throws IOException {


    Random ranx = new Random();
    Random rany = new Random();
    //hashmap for positions
    HashMap<Integer,List<Integer>> positions=new HashMap<Integer,List<Integer>>();
    //hashmap for displacements
    HashMap<Integer,List<Double>> displacement=new HashMap<Integer,List<Double>>();
    //hashmap for calculation of hypotenuse 
    HashMap<Integer,List<Integer>> hypotenuse=new HashMap<Integer,List<Integer>>();
    //hashmap for calculation of delta
    HashMap<Integer,List<Integer>> delta=new HashMap<Integer,List<Integer>>();
    //these variables will help calculate repulsive forces
    int n = 2;
    int area = 1280*720;
    int vposx=0;
    int vposy=0;
    int iterations=100;
    int vdispx=0;
    int vdispy=0;
    double DispForcex=0;
    double DispForcey=0;
    //these variables will help find edges
    int vertices = 0;
    int edges=0;
    int source=0;
    int dest=0;
    int c=0,count=1;
    String temporary=null;

    FileWriter writer = new FileWriter("C://Users/tsusi/Desktop/tempbottom.txt");

    System.out.print("Enter the size of the problem in the format of -p edges int int-\n");
    Scanner scan = new Scanner(System.in);
    String line = scan.nextLine();
    String[] wordsp = line.split(" ");
    String[] wordse = null;
    int iter = 500;
    char[] pcheck=wordsp[0].toCharArray();
    if (pcheck[0] == 'p'){
        //convert string to integer
        vertices = Integer.parseInt(wordsp[2]);
        edges = Integer.parseInt(wordsp[3]);
        //System.out.println(vertices+" "+edges);

    }else{
        System.out.println("Error. Wrong format");
        exit(0);
    }






    double k = 0.5*(Math.sqrt(area/n));
    //initialise EVERYTHING
    for (int i=0; i<=n; i++){
        List<Integer> temp = new ArrayList<>();
        vposx=ranx.nextInt(1280)+1;
        vposy=rany.nextInt(720)+1;
        //System.out.println(vposx+" "+vposy);
        temp.add(vposx);
        temp.add(vposy);
        positions.put(i,temp);
        List<Double> tempdisp = new ArrayList<>();
        tempdisp.add(0.0);
        tempdisp.add(0.0);
        displacement.put(i,tempdisp);
        List<Integer> deltalist = new ArrayList<>();
        deltalist.add(0);
        deltalist.add(0);
        delta.put(i, deltalist);

    }




    //attraction forces
    for (int i=0;i<=1;i++){
        for (int v=0;v<n-1;v++){
            vdispx=0;
            vdispy=0;
            int deltax=0;
            int deltay=0;
            //the sublist functiojns are there to split the list so that we can later
            //use the x,y coordinates to calculate delta
            List<Integer> deltalist = new ArrayList<>();



            for (int u=0;u<n-1;u++){
                if (u!=n){


                //Just as mentioned earlier, we will split the lists in order to extract coordinates
                List<Integer> test = positions.get(v);
                List<Integer> height = test.subList(0,1);
                List<Integer> width = test.subList(1,2);
                List<Integer> test2 = positions.get(u);
                List<Integer> height2 = test2.subList(0,1);
                List<Integer> width2 = test2.subList(1,2);
                //calculate the length of each vector, then store to hashmap
                int w1=width.get(0);
                int w2=width2.get(0);
                int h1=height.get(0);
                int h2=height2.get(0);
                double deltaL=Math.hypot(w2-w2, h2-h1);

                deltalist.add((w1-w2));
                deltalist.add((h1-h2));
                double repForce= (k*k)/deltaL;
                double dispForcex=Math.abs(w1-w2)*repForce/deltaL;
                double dispForcey=Math.abs(h1-h2)*repForce/deltaL;
                //split displacement list
                List<Double> test3 = displacement.get(v);
                List<Double> height1 = test3.subList(0,1);
                List<Double> width1 = test3.subList(1,2);

                double dispxfinal=height1.get(0)+(w1-w2)*repForce;
                double dispyfinal=width1.get(0)+(h1-h2)*repForce;
                List<Double> disp = new ArrayList<>();
                disp.add(dispxfinal);
                disp.add(dispyfinal);
                displacement.put(v,disp);
                }else{
                    break;
                }


                }

Actual results:{0=[NaN, NaN], 1=[0.0, 0.0], 2=[0.0, 0.0]} Expected results: {0=[random,random] ... n=[random,random]}




Aucun commentaire:

Enregistrer un commentaire