⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 glpanel.java

📁 改进的多目标遗传算法聚类
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        JPanel scrollselect = scrollSelectPanel(new String[] {zoomLabel, rotateLabel, localityLabel});
        scrollselect.setBackground(defaultColor);
        topPanel.add(scrollselect,c);

        add(topPanel, BorderLayout.NORTH);

        c.fill = GridBagConstraints.BOTH;
        c.gridwidth = 1;
        c.gridx = 0; c.gridy = 1; c.weightx = 1; c.weighty = 1;
        scrollPanel.add(tgPanel,c);

        c.gridx = 1; c.gridy = 1; c.weightx = 0; c.weighty = 0;
        scrollPanel.add(verticalSB,c);

        c.gridx = 0; c.gridy = 2;
        scrollPanel.add(horizontalSB,c);

        add(scrollPanel,BorderLayout.CENTER);

        glPopup = new JPopupMenu();
        glPopup.setBackground(defaultColor);

        JMenuItem menuItem = new JMenuItem("Toggle Controls");
        ActionListener toggleControlsAction = new ActionListener() {
                boolean controlsVisible = true;
                public void actionPerformed(ActionEvent e) {
                    controlsVisible = !controlsVisible;
                    horizontalSB.setVisible(controlsVisible);
                    verticalSB.setVisible(controlsVisible);
                    topPanel.setVisible(controlsVisible);
                }
            };
        menuItem.addActionListener(toggleControlsAction);
        glPopup.add(menuItem);
    }

    protected JPanel scrollSelectPanel(String[] scrollBarNames) {
        final JComboBox scrollCombo = new JComboBox(scrollBarNames);
        scrollCombo.setBackground(defaultColor);
        scrollCombo.setPreferredSize(new Dimension(80,20));
        scrollCombo.setSelectedIndex(0);
        final JScrollBar initialSB = (JScrollBar) scrollBarHash.get(scrollBarNames[0]);
        scrollCombo.addActionListener(new ActionListener() {
            JScrollBar currentSB = initialSB;
            public void actionPerformed(ActionEvent e) {
                JScrollBar selectedSB = (JScrollBar) scrollBarHash.get(
                        (String) scrollCombo.getSelectedItem());
                if (currentSB!=null) currentSB.setVisible(false);
                if (selectedSB!=null) selectedSB.setVisible(true);
                currentSB = selectedSB;
            }
        });

        final JPanel sbp = new JPanel(new GridBagLayout());
        sbp.setBackground(defaultColor);
        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0; c.gridy = 0; c.weightx= 0;
        sbp.add(scrollCombo,c);
        c.gridx = 1; c.gridy = 0; c.weightx = 1; c.insets=new Insets(0,10,0,17);
        c.fill=GridBagConstraints.HORIZONTAL;
        for (int i = 0;i<scrollBarNames.length;i++) {
            JScrollBar sb = (JScrollBar) scrollBarHash.get(scrollBarNames[i]);
              if(sb==null) continue;
              if(i!=0) sb.setVisible(false);
              //sb.setMinimumSize(new Dimension(200,17));
              sbp.add(sb,c);
        }
        return sbp;
    }

    public void addUIs() {
        tgUIManager = new TGUIManager();
        GLEditUI editUI = new GLEditUI(this);
        GLNavigateUI navigateUI = new GLNavigateUI(this);
        tgUIManager.addUI(editUI,"Edit");
        tgUIManager.addUI(navigateUI,"Navigate");
        tgUIManager.activate("Navigate");
    }

    public void randomGraph() throws TGException {
        Node n1= tgPanel.addNode();
        n1.setType(0);
        for ( int i=0; i<246; i++ ) {
            Node r = tgPanel.getGES().getRandomNode();
            Node n = tgPanel.addNode();
            n.setType(0);
            if (tgPanel.findEdge(r,n)==null) tgPanel.addEdge(r,n,Edge.DEFAULT_LENGTH);
            if (i%2==0) {
                r = tgPanel.getGES().getRandomNode();
                if (tgPanel.findEdge(r,n)==null) tgPanel.addEdge(r,n,Edge.DEFAULT_LENGTH);
            }
        }
        tgPanel.setLocale(n1,2);
    }     

    public void myGraph() throws TGException {

	//	System.err.println("Maximum = " + maxa);    
	Node [] centre = new Node[K];
	Point [] p = new Point[K];

	MDS mds = new MDS(dist ,K); 
	double [][] points = mds.getPoints();
	for (int i=0; i<K; i++) {
	    // System.err.println((int)(1000.0*points[i][0]) + " " + (int)(1000.0*points[i][1]));
	    p[i] = new Point((int)(2000.0*points[i][0]),(int)(2000.0*points[i][1]));
	}
	
	for (int i=0; i<K; i++) {
	    centre[i] = tgPanel.addNode(null, new String("Cluster " + i), Color.black, p[i], i, true);
	    centre[i].setType(2);
	}

        for ( int i=0; i<N; i++ ) {
            Node n = tgPanel.addNode(labels[i], p[ass[i]], ass[i], false);
            n.setType(1);
            tgPanel.addEdge(n,centre[ass[i]],(int)(10.0+1000.0*strength[i]));
        }

	for (int i=0; i<K; i++) {
	    for (int j=0; j<i; j++) {
		tgPanel.addEdge2(centre[i],centre[j],(int)(10.0+2000.0*dist[i][j]), Color.black);
	    }
	}

	//	tgPanel.setLocale(centre[0],1);
    }  

    public static void main(String[] args) {

        JFrame frame;
        frame = new JFrame("Graph Layout");
        GLPanel glPanel = new GLPanel();
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {System.exit(0);}
        });

        frame.getContentPane().add("Center", glPanel);
        frame.setSize(500,500);
        frame.setVisible(true);
    }

} // end com.touchgraph.graphlayout.GLPanel





class MDS {

	private double [][] points;			
	private int dimension = 2;			
	private int iterations = 10;			// number of iterations of optimisation
	private double learnrate = 0.05;		// learning rate used in optimisation
	
	                                                // for normalisation:
	private double reshift;                      	// shift to origin
	private double rescale;                         // scale to interval [0, 1]
	
	private int its = 0;                         	// iteration counter
	private double [][] p;	                        // points working matrix
	private int N;                                  // number of data elements


	/** Constructor starting from readily extracted distance data
	* @param distances the precomputed inter-document distance data
	* @param N the number of inter-document distances
	* @param conf the current parameter settings
	*/
	public MDS(double [][] distances, int N) {
	
		try {
		    this.N = N;
		    for (int i=0; i<N; i++) {
			for (int j=i; j<N; j++) {
			    distances[i][j] = distances[j][i];
			}
		    }
		
		    // find minimum distance		
		    reshift = distances[1][0];
		    for (int i=0; i<N; i++) {
			for (int j=0; j<N; j++) {
			    reshift = Math.min(reshift, distances[i][j]);
			}
		    }
			// shift to origin
			for (int i=0; i<N; i++) {
				for (int j=0; j<N; j++) {
					distances[i][j] += -reshift;
				}
			}

			// find maximum distance
			rescale = distances[1][0];
			for (int i=0; i<N; i++) {
				for (int j=0; j<N; j++) {
					rescale = Math.max(rescale, distances[i][j]);
				}
			}
			// normalize to interval [0, 1]
			for (int i=0; i<N; i++) {
				for (int j=0; j<N; j++) {
					distances[i][j] *= 1.0/rescale;
				}
			}

			// initialisation
			points =  new double[N][dimension];
			p = new double[N][dimension];
			for (int i=0; i<N; i++) {
			    for (int j=0; j<dimension; j++) {
				p[i][j] = Math.random()*0.01-0.005;
				
			    }
			}
			
		} catch (Exception e) {
			System.err.println("MDS: Failure during preparations");
			System.err.println(e.getMessage());
			return;
		}
		
		try {
			
			int [] pinning_order;
		
			// main loop
			while (its<iterations) {
			    
			    its++;				
   				
			    // randomly permute the objects to determine the order
			    // in which they are pinned for this iteration
			    pinning_order = randperm(N);
   				
			    for (int i=0; i<N; i++) {

				int m = pinning_order[i];
					
      				// having pinned an object, move all of the other on each dimension
	      			// according to the learning rule
      				for (int j=0; j<N; j++) {
					
				    if (m != j) {
					double dummy = norm(m, j);
					
					if (dummy != 0) {
					    for (int k=0; k<dimension; k++) {
						p[j][k]=p[j][k]-learnrate*(dummy-distances[j][m])/dummy*Math.abs(p[j][k]-p[m][k])*sign(p[j][k]-p[m][k]);
									
					    }
					    
					}
				    }
				}       	
			    }
	        	}
	    	} catch (Exception ex) {
		    System.err.println("MDS: Failure during main optimisation loop");
		    System.err.println(ex.getMessage());
		    return;
		}
		
		for (int i=0; i<N; i++) {
		    for (int j=0; j<dimension; j++) {
			points[i][j] = p[i][j];//*rescale+reshift;
			
		    }
		}
		double [] mean = new double[dimension];
		for (int j=0; j<dimension; j++) {
		    mean[j] = 0.0;
		}
		for (int i=0; i<N; i++) {
		    for (int j=0; j<dimension; j++) {
			mean[j] += points[i][j];
		    }
		}
		for (int j=0; j<dimension; j++) {
		    mean[j] /= (double)(N);
		}

		for (int i=0; i<N; i++) {
		    for (int j=0; j<dimension; j++) {
			points[i][j] -= mean[j];
			
		    }
		}
		


	}

	// signum function
	private int sign(double value) {
		if (value == 0) return 0;
		else if (value > 0) return 1;
		else return -1;
	}

	// compute the Euclidean distance between the vectors represented by column m and j
	private double norm(int m, int j) {
		double sum = 0;
		for (int i=0; i<dimension; i++) {
			sum += (p[m][i] - p[j][i])*(p[m][i] - p[j][i]);
		}
	
		return Math.sqrt(sum);
	}

	// create a random ermutation of integers 0 to N-1
	private int[] randperm(int n) {
		int [] perm = new int[n];
		int [] numbers = new int[n];
		for (int i=0; i<n; i++) {
			numbers[i] = i+1;
		}
		for (int k=0; k<n; k++) {
			while (true) {
				int guess = (int)Math.floor(Math.random()*n);
				if (numbers[guess] != 0) {
					perm[k] = numbers[guess]-1;
					numbers[guess] = 0;
					break;
				}
			}
		}
		return perm;
	}


        /** Return the two-dimensional coordinates for the documents
	* @return a two-dimensional array of coordinates
	*/
	public double [][] getPoints() {
		return points;
	}
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -