U E D R , A S I H C RSS

Load Balancing Problem/임인택

LoadBalancingMain.java


~cpp 
/**
 * @author Administrator
 *
 * To change this generated comment edit the template variable "typecomment":
 * Window>Preferences>Java>Templates.
 * To enable and disable the creation of type comments go to
 * Window>Preferences>Java>Code Generation.
 */
import junit.framework.*;
import junit.textui.*;

public class LoadBalancingMain {
	public static void main(String[] args) {
		TestSuite suite = new TestSuite();
		
		suite.addTestSuite(TestLoadBalancing.class);
		
		TestRunner.run(suite);
	}
}
  

TestLoadBalancing.java


~cpp 
/**
 * @author Administrator
 *
 * To change this generated comment edit the template variable "typecomment":
 * Window>Preferences>Java>Templates.
 * To enable and disable the creation of type comments go to
 * Window>Preferences>Java>Code Generation.
 */

import junit.framework.*;

public class TestLoadBalancing extends TestCase{

	public void testSetNGetData() {
		LoadBalancing bal = new LoadBalancing(3);
		
		int sData[] = {1,3,3};
		int gData[];
		
		bal.setData(sData);
		gData = bal.getData();
		
		for(int i=0; i<sData.length; i++)
			assertEquals(sData[i], gData[i]);
	}	
	public void testGetSumOfJob() {
		LoadBalancing bal = new LoadBalancing(3);
		
		int data[]={1,3,4};
		bal.setData(data);
		
		assertEquals(bal.getSumOfJob(), 8);
		assertEquals(bal.getMaximumJob(), 4);
		assertEquals(bal.getMinimumJob(), 1);
	}
	public void testCheckLeftRight() {
		LoadBalancing bal = new LoadBalancing(10);
		
		/*int data[]={1,2,3,4,5,6,7,8,9,10};
		bal.setData(data);
		
		assertEquals(bal.checkLeft(0), false);
		assertEquals(bal.checkLeft(1), true);
		assertEquals(bal.checkLeft(2), true);
		assertEquals(bal.checkLeft(3), true);
		assertEquals(bal.checkLeft(4), true);
		assertEquals(bal.checkLeft(5), true);
		assertEquals(bal.checkLeft(6), true);
		assertEquals(bal.checkLeft(7), true);
		assertEquals(bal.checkLeft(8), true);
		assertEquals(bal.checkLeft(9), true);
		
		assertEquals(bal.checkRight(0), false);
		assertEquals(bal.checkRight(1), false);
		assertEquals(bal.checkRight(2), false);
		assertEquals(bal.checkRight(3), false);
		assertEquals(bal.checkRight(4), false);
		assertEquals(bal.checkRight(5), false);
		assertEquals(bal.checkRight(6), false);
		assertEquals(bal.checkRight(7), false);
		assertEquals(bal.checkRight(8), false);
		assertEquals(bal.checkRight(9), false);
		
		int data_[] = {5,5,5,5,5,5,5,5,5,10};
		bal.setData(data_);
		assertEquals(bal.checkLeft(0), false);
		assertEquals(bal.checkLeft(1), false);
		assertEquals(bal.checkLeft(2), false);
		assertEquals(bal.checkLeft(3), false);
		assertEquals(bal.checkLeft(4), false);
		assertEquals(bal.checkLeft(5), true);
		assertEquals(bal.checkLeft(6), true);
		assertEquals(bal.checkLeft(7), true);
		assertEquals(bal.checkLeft(8), true);
		assertEquals(bal.checkLeft(9), true);
		
		assertEquals(bal.checkRight(0), false);
		assertEquals(bal.checkRight(1), false);
		assertEquals(bal.checkRight(2), false);
		assertEquals(bal.checkRight(3), false);
		assertEquals(bal.checkRight(4), false);
		assertEquals(bal.checkRight(5), false);
		assertEquals(bal.checkRight(6), false);
		assertEquals(bal.checkRight(7), false);
		assertEquals(bal.checkRight(8), false);
		assertEquals(bal.checkRight(9), false);
		
		int data__[] = {10,9,8,7,6,5,4,3,2,1};
		bal.setData(data__);
		
		assertEquals(bal.checkLeft(0), false);
		assertEquals(bal.checkLeft(1), false);
		assertEquals(bal.checkLeft(2), false);
		assertEquals(bal.checkLeft(3), false);
		assertEquals(bal.checkLeft(4), false);
		assertEquals(bal.checkLeft(5), false);
		assertEquals(bal.checkLeft(6), false);
		assertEquals(bal.checkLeft(7), false);
		assertEquals(bal.checkLeft(8), false);
		assertEquals(bal.checkLeft(9), false);
		
		assertEquals(bal.checkRight(0), true);
		assertEquals(bal.checkRight(1), true);
		assertEquals(bal.checkRight(2), true);
		assertEquals(bal.checkRight(3), true);
		assertEquals(bal.checkRight(4), true);
		assertEquals(bal.checkRight(5), true);
		assertEquals(bal.checkRight(6), true);
		assertEquals(bal.checkRight(7), true);
		assertEquals(bal.checkRight(8), true);
		assertEquals(bal.checkRight(9), false);*/
		
		bal = new LoadBalancing(10);
		int data___[]={13,13,13,13,13,14,14,15,13,13};
		bal.setData(data___);
		
		//assertEquals(true, bal.checkRight(7));
		//assertEquals(true, bal.checkLeft(7));
		
		
	}
	public void testLoadBalancing() {
		
		LoadBalancing bal = new LoadBalancing(3);
		
		int data[]={0,3,0};
		bal.setData(data);
		bal.loadBalance();
		
		bal.printInfo();
		
		bal = new LoadBalancing(10);
		int data_[] = {1,2,3,4,5,6,7,8,9,10};
		bal.setData(data_);
		bal.loadBalance();
		
		bal.printInfo();
		
		bal = new LoadBalancing(10);
		int data__[]={5,0,10,6,1,49,1,50,3,9};
		assertEquals(10, data__.length);
		bal.setData(data__);
		bal.loadBalance();
		
		bal.printInfo();
		
		bal = new LoadBalancing(10);
		int data___[]={13,13,13,13,13,13,13,13,15,14};
		bal.setData(data___);
		bal.loadBalance();
		
		bal.printInfo();
		
		bal = new LoadBalancing(10);
		int data____[]={0,9,0,0,0,0,0,0,0,0};
		bal.setData(data____);
		bal.loadBalance();
		
		bal.printInfo();
	}
	
}
  

LoadBalancing.java


~cpp 
/**
 * @author Administrator
 *
 * To change this generated comment edit the template variable "typecomment":
 * Window>Preferences>Java>Templates.
 * To enable and disable the creation of type comments go to
 * Window>Preferences>Java>Code Generation.
 */




public class LoadBalancing {
	
	private int _processor[];
	private int _processorNum;
	private int _movedJob;
	
	public LoadBalancing(int processorNum) {
		_processor = new int[processorNum];
		_processorNum = processorNum;		
	}
	
	public int getProcessorNum() {
		return _processorNum;
	}
	
	public int[] getProcessorInfo() {
		return _processor;
	}
	
	public int getSumOfJob() {
		int sum=0;
		
		for(int i=0; i<_processor.length; i++)
			sum+=_processor[i];
		
		return sum;
	}
	
	public void setData(int[] data) {
		for(int i=0; i<data.length; i++)
			_processor[i] = data[i];
	}
	
	public int[] getData() {
		return _processor;
	}
	public void loadBalance() {
		int sum = getSumOfJob();
		int average = sum/_processor.length;
		int remain  = sum%_processor.length;
		int pos=0;
		
		while( !isFinishedCondition() ) {
			if( pos!=0 && checkLeft(pos) && _processor[pos]!=0) {
				_processor[pos-1]++;
				_processor[pos]--;
				_movedJob++;
				
				System.out.println(pos + " 에서" + (pos-1) + " 로 옮김");
			}
			else if( pos<_processor.length-1 && checkRight(pos) && _processor[pos]!=0) {
				_processor[pos]--;
				_processor[pos+1]++;
				_movedJob++;
				
				System.out.println(pos + " 에서" + (pos+1) + " 로 옮김");
			}
			
			pos++;
			
			printInfo();
			
			if( pos==_processor.length ) {
				pos = 0;
			}
		}
	}
	
	public boolean isFinishedCondition() {
		if( getMaximumJob()-getMinimumJob() <= 1 )
			return true;
		else
			return false;
	}
	
	public int getMaximumJob() {
		int ret = Integer.MIN_VALUE;

		for(int i=0; i!=_processor.length; i++) {
			ret = (ret<_processor[i])?_processor[i]:ret;
		}
		return ret;
	}
	
	public int getMinimumJob() {
		int ret = Integer.MAX_VALUE;
		
		for(int i=0; i!=_processor.length; i++) {
			ret = (ret>_processor[i])?_processor[i]:ret;
		}
		return ret;		
	}
	public boolean checkLeft(int index) {
		int average = getSumOfJob() / _processor.length;
		int remian = getSumOfJob() % _processor.length;
		int leftSum = 0;
		int rightSum = 0;
		int partialLength = (index==0)? 1:index;
		
		if( index==0 ) {
			return false;
		}
		
		// 왼쪽 평균<오른쪽 평균
		for(int i=0; i<index; i++) {
			leftSum+=_processor[i];
		}
		rightSum = getSumOfJob() - leftSum;


		if( average==0 ) {
			if(leftSum < index-1 )
				return true;
			else
				return false;
		}
		else if( leftSum/index < rightSum/(_processor.length-index) ) {
			System.out.println("레프트");
			return true;
		}
		/*else if(leftSum/index == rightSum/(_processor.length-index) 
			&& _processor[index] > leftSum/index ) {
			return true;
		}*/
		else {
			return false;
		}
	}
	public boolean checkRight(int index) {
		int average = getSumOfJob() / _processor.length;
		int remian = getSumOfJob() % _processor.length;
		int leftSum = 0;
		int rightSum = 0;
		int partialLength = (index==0)? 1:index;
		
		if( index==_processor.length-1 ) {
			return false;
		}
		
		// 왼쪽 평균>오른쪽 평균
		for(int i=0; i<=index; i++) {
			leftSum+=_processor[i];
		}
		if(index==0)
			leftSum = _processor[0];
		rightSum = getSumOfJob() - leftSum;
		
		/*if( average != 0 ) {
			System.out.println("인덱스 : " + index );
			System.out.println("평균 : " + _processor[index] + "왼쪽나머지:" + (leftSum%average)
							+ "오른쪽 나머지 : " + (rightSum%average) );
		}*/
		if( average==0 ) {
			if( rightSum< (_processor.length-index-1) )
				return true;
			else
				return false;
		}
		else if( leftSum/(index+1) > rightSum/(_processor.length-index-1)  ){ 
			return true;
		}
		else if( leftSum/(index+1) == rightSum/(_processor.length-index-1 ) 
			&& _processor[index] > rightSum/(_processor.length-index-1) ) {
			System.out.println("라이트");
			return true;
		}
		else {
			return false;
		}
	}
	public void printInfo() {
		System.out.println();
		System.out.println("옮긴 횟수: " + getMovedJob() );
		for(int i=0; i<_processor.length; i++) {
			System.out.print(_processor[i] + "  " );
		}
		System.out.println();
	}
	public int getMovedJob() {
		return _movedJob;
	}
}
  

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2009-05-27 07:09:19
Processing time 0.1012 sec