Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Creating an array that works like a torus

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Creating an array that works like a torus

    I have create a program that takes a random array which is created by starting from 0 and adding Math.random() (double between 0 and 0.999) n times, and calculates the weighted average of each position within a certain radius. I currently have a program that does this but i was wondering how to create one using a torus. The basic principle is the last element is now equal to the first element and when the first element updates its position it takes into account the difference between the other elements including some of the last elements in the array.

    Its not so much help with the coding but with the principle behind it, I cant work out how this would be possible for multiple iterations.

    heres the code so far that works for one iteration. After one the code is incorrect and calculates the wrong values.

    I think using a circular list or a ring buffer may work but i have little experience with either. Any help on the matter would be much appreciated.

     
        import java.text.DecimalFormat;
        import java.util.Scanner;
     
        /**
        * Created by jameshales on 12/03/2014.
         */
        public class Torus {
     
        public static void main(String[] args) {
     
     
            DecimalFormat df = new DecimalFormat("#.###"); 
     
     
            System.out.println("how many numbers of agents on the real line?"); 
     
            Scanner input = new Scanner(System.in);
            int n = 0;
            n=Integer.parseInt(input.nextLine()); 
     
     
            double[] agentPosition = new  double[n];  
            double[] newAgentPosition = new double[n];  
            double[] originalAgentPosition = new double[n];  
     
            System.out.println("Please select your desired radius? select 1 normally"); 
     
     
            double r = 0;
            r = input.nextDouble(); 
     
            int t = 0;  // sets t to 0
     
            double epsilon = 0.001;  
     
            for (int i = 0; i <= n - 1; i++) {  
     
                if (i > 0)
                    agentPosition[i] = agentPosition[i - 1] + Math.random(); // this equation creates the random array 
                else agentPosition[i] =0.0;
            }
     
            System.arraycopy(agentPosition,0,originalAgentPosition,0,n);
     
            while(true) { 
     
                for (int i = 0; i <= n - 1; i++) {
     
                    double total1 = agentPosition[i];    
     
                    double total2 = 0;  
     
                    int numposition = 1; 
     
     
                        for (int j = i - 1; j >= 0; j--) {  
     
                            if ((agentPosition[i] - agentPosition[j]) <= r) {  
     
                                numposition++;        
     
                                total1 += agentPosition[j];  
     
                            } else break;  // stops the program once it has passed a position of a distance of 1
     
                        }
     
                        for (int k = i + 1; k <= n - 1 ; k++) { 
     
                            if (Math.abs(agentPosition[k] - agentPosition[i]) <= r) {     
     
                                numposition++;   
     
                                total2 += agentPosition[k]; 
                            } else break;    
                        }
     
                       for (int j = n - 2; j >= 1; j--) {   
     
                            if (((agentPosition[n-1] + agentPosition[i]) - agentPosition[j]) <= r) {  
     
                                numposition++;
     
                                total1 += (agentPosition[j] - agentPosition[n - 1]);   
     
                            }    else break;
     
                        }
     
                        for (int k = 1; k <= n - 2 ; k++) { 
     
                            if (Math.abs((agentPosition[i] - agentPosition[n - 1]) - agentPosition[k]) <= r) {     /
     
                                numposition++;      
     
                                total2 += (agentPosition[n - 1] + agentPosition[k]); 
     
                            }     else break;
                        }
     
                        newAgentPosition[i] = (total1 + total2) / numposition; // this calculates the new weighted average. ( sum of assigned random variable/ sum of position)
     
                    }
     
                for (int i = 0; i <= n - 1; i++){
     
                    if (newAgentPosition[i] > originalAgentPosition[n - 1]){
                        newAgentPosition[i] = newAgentPosition[i] - originalAgentPosition[n - 1];
                    }
     
                    if(newAgentPosition[i] < 0) {  
                        newAgentPosition[i] = newAgentPosition[i] + originalAgentPosition[n - 1];
                    }
     
                }
     
                t++;  // This sums up how many iterations it will take.
     
                double largestDiff = 0.0;  // This assigns largestDiff to 0
     
                for (int i = 0; i <= n-1; i++) {
     
                    double diff = Math.abs(agentPosition[i] - newAgentPosition[i]);  // This calculates the difference between the previous and current array at position i.
     
                    if(diff > largestDiff)  // If the difference between the agents is bigger than 0, assign it to the variable largestDiff.
                        largestDiff = diff;
     
                }
     
                if(largestDiff <= epsilon){  // This checks if the difference is bigger than the set epsilon,
     
                    break;  // This stops the program if the difference is smaller than epsilon
                }
     
                agentPosition = new double[n];
     
                System.arraycopy(newAgentPosition, 0, agentPosition, 0, n);  
     
            }
     
            for (int i = 0 ; i <= n - 1; i++) {   
     
                System.out.println(i + ": " + df.format(originalAgentPosition[i]) + "\t->\t" + df.format(agentPosition[i]));
     
            }
     
            int sumdofclusters = 1;  // This sets the sum of clusters to 1
     
            System.out.println("The different clusters are:\n" + df.format(agentPosition[0]));   // This prints out the first cluster only.
     
            for (int i = 1; i <= n - 1 ; i++) {
     
                if(Math.abs(agentPosition[i] - agentPosition[i - 1]) >= epsilon) {  // This checks if the element after the element at hand is different by a set epsilon.(how to work out different clusters)
     
                    sumdofclusters++;  // This sums the number of clusters.
     
     
                    System.out.println(df.format(agentPosition[i])); // This prints out the different clusters other than the first 1.
                }
     
            }
            System.out.println("Number of clusters is:" + sumdofclusters); // This prints out the number of clusters.
     
            System.out.println("Number of iterations:" + t);   // This prints out the number of iterations.
     
     
        }
        }
    Last edited by jameshales; March 15th, 2014 at 07:06 AM.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Creating an array that works like a torus

    Welcome to the forum! Please read this topic to learn how to post code correctly and other useful info for new members.

    Please edit your post so that the code is posted correctly and code formatting is preserved.

    Sample runs would be helpful. Show what the program's output looks like when "it works" and when it doesn't and explain the difference. Since we're not all math majors, some may have trouble following you. Well, I do.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Creating an array that works like a torus

    Also posted at: Creating an array that works like a torus
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 2
    Last Post: February 24th, 2014, 10:48 PM
  2. Replies: 9
    Last Post: September 15th, 2013, 02:48 PM
  3. compile errors when creating a 2nd array the same size as 1st array
    By javaiscool in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 26th, 2013, 09:35 PM
  4. how array works ?
    By erdy_rezki in forum Java Theory & Questions
    Replies: 4
    Last Post: May 1st, 2012, 09:57 AM
  5. Exception when creating an array
    By Majora94 in forum Collections and Generics
    Replies: 2
    Last Post: February 23rd, 2012, 05:33 PM

Tags for this Thread