You are on page 1of 12

XỬ LÝ PHÂN TÁN MAPREDUCE

Solving Systems of Equations Using MapReduce


 

1.    Thuật toán MapReduce


1.1         Thuật toán Cramer

Một hệ phương trình tuyến tính có định thức của ma trận hệ số khác 0 gọi là hệ
Cramer
1. Một hệ phương trình với số phương trình bằng số ẩn có nghiệm duy nhất khi và chỉ
khi đó là hệ Cramer hay khi và chỉ khi định thức của ma trận hệ số khác 0
2. Bắt đầu từ cạnh đầu tiên của dãy này, ta cứ thêm dần các cạnh của dãy đã được xếp
vào T theo nguyên tắc cạnh thêm vào không được tạo thành chu trình trong T.
3. Hệ phương trình Cramer có thể viết dưới dạng ma trận AX = B với ma trận hệ số A
là một ma trận khả nghịch
5. Nghiệm duy nhất được xác định theo công thức: X = A-1B
1.2         Giải quyết bài toán hệ phương trình tuyến tính với Map-Reduce

Ở đây ta sẽ dựa trên thuật toán Cramer


Hàm Map sẽ đọc file input, với từng dòng một tương ứng thì nó sẽ thực hiện một lần
chạy trong phương thức Map, và cách xử lý như sau:
- Ví dụ như trong file input.txt là 1 ma trận
    [ 1 2 3 4
      5 6 7 8
      9 1 3 5 ]

- Ta sẽ đưa vào mảng với thứ tự trọng số matrix[row][column]


- Tiếp theo ta sẽ tạo 1 ma trận 1 chiều d[row] chứa những phần tử cuối cùng của từng
dòng trong matrix d = [4, 8, 5]
- Tiếp theo đó là ta thực hiện tính toán định thức theo công thức. Ví dụ tính d thì ta có
det = 1*6*3 + 2*7*9 + 3*5*1 – 3*6*9 – 2*5*3 – 1*7*1   (1)
- Tính det 1-n thì ta lần lượt thay ma trận d vào từng cột rồi tính kết quả.
Mục đích của hàm Map sẽ xuất ra các <key,val> là giá trị của các định thức để hàm
Reducer có thể tính toán thông qua các giá trị của định thức theo công thức (1).

2.    Tổ chức dữ liệu đầu vào


File input.txt 
 
1 2 -3 1
2 0 1 2
3 2 0 -1

3.    Chương trình MapReduce


3.1          Tập tin EquationSolver.java

        
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
 
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Counters;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.Reducer.Context;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.ToolRunner;
 
public class EquationSolver {
   
    public static class EquationSolverMapper extends Mapper<Object,
Text, Text, IntWritable>{
    public static Text text = new Text();
         
    public void map(Object key, Text value, Context context) throws
IOException, InterruptedException {
                   String []input = value.toString().split("\n");
                  
                   int row = input.length;
                   int column = input[0].length();
                  
                   int [][]myArray = new int[row][column];
                
                   for (int i=0; i<myArray.length; i++) {
                    String[] line = input[i].trim().split(" ");
                    for (int j=0; j<line.length; j++) {
                       myArray[i][j] = Integer.parseInt(line[j]);
                    }
                 }
                   System.out.println(myArray);
                   text.set("det");
                   int[] d = new int[row];
               for(int i = 0; i< row; i++ ) d[i] = myArray[i][column-
1];
               int[][] matrix = copy(myArray);
               int res = det(matrix);
              
               IntWritable det = new IntWritable(res);
                context.write(text, det);
               int[] dt = new int[row];
               for(int j = 0; j < row; j++ ){
                   int[][] matrix1 = copy(myArray);
                   for(int i = 0; i< row; i++ )
                       matrix1[i][j] = d[i];
                   dt[j] = det(matrix1);
                   text.set("det" + (j+1));
                   IntWritable valueDet = new IntWritable(dt[j]);
                   context.write(text, valueDet);
               }
           }
    }    
 
    static int[][] copy(int[][] arr ){
        int[][] newArr  = new int[arr.length][arr.length];
        for (int i = 0; i < arr.length; i++) {
            for(int j = 0; j< arr.length; j++ ){
                newArr[i][j] = arr[i][j];
            }
        }
        return newArr;
    }
   
    static int det(int[][] arr){ //Tinh & tra ve dinh thuc
 
        if(arr.length == 1) return arr[0][0];
        if(arr.length  == 2 ) return arr[0][0]*arr[1][1] - arr[0]
[1]*arr[1][0];
        else{
           
            int res = 0;
            for(int k = 0; k< arr.length; k++ ){
                int[][] smaller = new int[arr.length-1][arr.length-1];
                for(int i = 0; i< arr.length; i++ ){
                    for(int j = 1;  j< arr.length;  j++ ){
                        if(i< k) smaller[i][j-1] = arr[i][j];
                        else if(i > k) smaller[i-1][j-1] = arr[i][j];
                    }
                }
                int s = -1;
                if(k%2 == 0 ) s =  1;
                res+= arr[k][0]*s*det(smaller);
            }
            return res;
        }
    }
   
public static class EquationSolverReduce extends
Reducer<Text,IntWritable,Text,IntWritable> {
         
           public void reduce(Text key, Iterable<IntWritable> values,
Context context) throws IOException, InterruptedException {
                 int[] arrDet = null;
                 for (IntWritable val : values) {
                       int i = 0;
                       arrDet[i] = val.get();
                       i++;
                 }
                 
                 for (int i = 1; i < arrDet.length; i++) {
                       key.set("x" + i);
                       IntWritable result = new
IntWritable(arrDet[i]/arrDet[0]);
                       context.write(key, result);
                 }
          }//reduce
    }//class EquationSolverReduce
 
     
    public static void main(String[] args) throws Exception {
          Configuration conf = new Configuration();
          Job job = Job.getInstance(conf, "Systems of Equations
Solver");
         
          job.setJarByClass(EquationSolver.class);
        job.setMapperClass(EquationSolverMapper.class);
        job.setCombinerClass(EquationSolverReduce.class);
        job.setReducerClass(EquationSolverReduce.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
         
          FileInputFormat.addInputPath(job,cd new Path(args[0]));
          FileOutputFormat.setOutputPath(job, new Path(args[1]));
          System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}//EquationSolver

4.    Biên dịch và chạy chương trình MapReduce


§  Mở cửa sổ lệnh dưới quyền Administrator.
§  Chạy tập tin start-all.cmd để khởi động các tiến trình hadoop
§  Chuyển vào thư mục hiện hành
cd C:\hadoop\MapReduceSystemsofEquationSolver

§  Khai báo biến môi trường chỉ đường dẫn


set HADOOP_LIB=c:\hadoop\hadoop-2.6.0\share\hadoop
set HADOOP_CLASSPATH=%HADOOP_LIB%\mapreduce\hadoop-
mapreduce-client-core-2.6.0.jar;%HADOOP_LIB%\common\hadoop-common-
2.6.0.jar;
§   Biên dịch mã nguồn java thành các file classes\*.class
C:\hadoop\MapReduceSystemsofEquationSolver> md classes
C:\hadoop\MapReduceSystemsofEquationSolver> javac -classpath
%HADOOP_CLASSPATH% -d classes *.java
C:\hadoop\MapReduceSystemsofEquationSolver> dir classes      

§   Xóa file *.jar  nếu đã có trước đó


C:\hadoop\MapReduceSystemsofEquationSolver> del EquationSolverX.jar

§   Đóng gói file *. jar


C:\hadoop\MapReduceSystemsofEquationSolver>  jar -cvf
EquationSolverXYZ.jar -C  classes .
§   Xóa thư mục /inputXYZ nếu đã có, tạo mới và chép dữ liệu vào
C:\hadoop\MapReduceSystemsofEquationSolver> hadoop fs -rm -r
/inputXYZ
C:\hadoop\MapReduceSystemsofEquationSolver> hadoop fs -mkdir
/inputXYZ
C:\hadoop\MapReduceSystemsofEquationSolver> hadoop fs -put
input.txt /inputXYZ
C:\hadoop\MapReduceSystemsofEquationSolver> hadoop fs -ls /inputXYZ
§   Xóa thư mục /outputXYZ nếu đã có
C:\hadoop\MapReduceSystemsofEquationSolver>  hadoop fs -rm –r
/outputXYZ

§  Chạy chương trình Mapreduce với dữ liệu trong thư mục /inputXYZ, kết quả xuất
ra /outputXYZ
C:\hadoop\MapReduceSystemsofEquationSolver> hadoop jar
EquationSolverXYZ.jar  EquationSolver  /inputXYZ /outputXYZ

§  Xem kết quả trong /outputXYZ


CHƯƠNG TRÌNH GẶP LỖI:

C:\hadoop\MapReduceSystemsofEquationSolver>  hadoop fs -ls /outputXYZ


C:\hadoop\MapReduceSystemsofEquationSolver>  hadoop fs -cat
/outputXYZ/part-r-00000

5.    Bài tập
1. Phân tích các bước chính của giải thuật.
2. Chạy chương trình với một số bộ dữ liệu tự tạo và có kích thước lớn khác nhau. mô tả
quá trình thực hiện sử dụng giải thuật MapReduce theo các bước như sau:
a) Mô tả cấu trúc dữ liệu sử dụng trong chương trình
b) Mô tả quá trình Map
c) Mô tả quá trình Reduce

 
6.    Tham khảo
Bài toán yêu cầu:
-         Cài đặt ứng dụng trên Hadoop- Mapreduce
-         Giải quyết bài toán hệ phương trình tuyến tính với Mapreduce
-         Chạy chương trình và đưa ra kết quả tối ưu
 

 
 

 
 

 
----------------------------------------------------------
 
1

You might also like