NGHIÊN CỨU CÁC PHƯƠNG PHÁP BIỂU DIỄN TRI THỨC TRONG LẬP TRÌNH LOGIC

114 241 0
NGHIÊN CỨU CÁC PHƯƠNG PHÁP BIỂU DIỄN TRI THỨC TRONG LẬP TRÌNH LOGIC

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Header Page of 16 B GIO DC V O TO TRNG I HC BCH KHOA H NI LUN VN THC S KHOA HC NGHIấN CU CC PHNG PHP BIU DIN TRI THC TRONG LP TRèNH LOGIC NGNH: CễNG NGH THễNG TIN NGUYN THANH T Ngi hng dn khoa hc: PGS.TS.NGUYN THANH THY H NI 2006 Footer Page of 16 Header Page of 16 lời cảm ơn Trớc tiên xin gửi lời cảm ơn đặc biệt tới PGS.TS Nguyễn Thanh Thủy, ngời định hớng đề tài tận tình hớng dẫn bảo suốt trình thực luận văn thạc sỹ khoa học, từ ý tởng đề cơng nghiên cứu, phơng pháp giải vấn đề, đến điều kiện lý tởng để thực hành luận văn Tôi xin chân thành bày tỏ lòng biết ơn tới tất giáo s, đặc biệt GS José Júlio Alferes, trung tâm Logic tính toán, Universidade Nova de Líboa, Bồ Đào Nha cho nhiều kiến thức quý báu vấn đề đại ngành logic tính toán, trí tuệ nhân tạo, công nghệ thông tin, cho môi trờng tập thể, khoảng thời gian khó quên động viên, giúp đỡ khích lệ thời gian thực luận văn Bản luận văn đợc hoàn thành với động viên giúp đỡ bạn bè lớp cao học Công nghệ thông tin 2004 - 2006 Tôi xin bày tỏ lòng cám ơn chân tình tới tất bạn, bạn dành nhiều thời gian quý báu để trao đổi, giúp đỡ gặp vớng mắc suốt thời gian thực luận văn Nguyễn Thanh Tú Công nghệ thông tin 2004 - 2006 Footer Page of 16 Header Page of 16 MC LC M U Chng CHNG TRèNH LOGIC TNG QUT 1.1 M u 1.2 Biu din tri thc chng trỡnh logic tng quỏt 12 1.3 Cõu tr li cho truy 17 1.4 Mt s ng ngha khỏc ca chng trỡnh logic tng quỏt 19 Chng LP TRèNH LOGIC M RNG 22 2.1 Biu din tri thc s dng cỏc chng trỡnh logic m rng 26 2.2 Ng ngha khỏc ca chng trỡnh logic m rng 37 2.3 Cỏc chng trỡnh logic phõn bit (Disjunctive Logic Programs) 38 2.3.1 Gii thiu 38 2.3.2 Biu din tri thc s dng chng trỡnh logic phõn bit 42 2.3.3 Tỡm cõu tr li cho truy 46 Chng MễI TRNG LP TRèNH LOGIC 50 3.1 Gii thiu 50 3.2 H thng DLV 53 3.2.1 Ngụn ng ca mụi trng DLV 54 3.2.2 Cu trỳc mt chng trỡnh 57 a C s d liu m rng EDB 57 b C s d liu c bn IDB 58 (i) Lut 58 (i.1) Lut ngm nh Footer Page of 16 59 Header Page of 16 (i.2) Lut phõn bit 61 (i.3) Lut ph nh 62 (ii) Rng buc 65 Chi Ha(ii.1) Rng buc ton 65 (ii.2) Rng buc yu 67 3.3 Gúi DLV Java 70 3.3.1 Biu din d liu: cỏc lp Predicate, Literal, Model v Program 70 3.3.2 Kin trỳc gúi DLV: lp DlvHandler 72 Chng CC BI TON MINH HA 77 4.1 Bi toỏn N quõn hu 78 4.1.1 Phõn tớch bi toỏn 78 4.1.2 Ci t 82 4.2 Bi toỏn Cõy khung nh nht 84 4.2.1 Mụ t bi toỏn 84 4.2.2 Phõn tớch v ci t 85 a Chng trỡnh logic DLV 85 b Ci t trờn Java 87 KT LUN 93 TI LIU THAM KHO 95 PH LC 97 Footer Page of 16 Header Page of 16 M U Logic tớnh toỏn c cỏc nh logic hc a vo nhng nm 1950, da trờn cỏc k thut t ng húa quỏ trỡnh suy din logic Logic tớnh toỏn c phỏt trin thnh lp trỡnh logic vo nhng nm 1970 T ú hỡnh thnh mt khỏi nim quan trng l lp trỡnh khai bỏo (declarative programming) i lp vi lp trỡnh cu trỳc (procedural programming) V ý tng, cỏc lp trỡnh viờn ch cn a khai bỏo ca chng trỡnh cũn vic thc hin c th mỏy tớnh t xỏc lp, ú vic thc hin cỏc chng trỡnh hng th tc li c xỏc lp c th bi lp trỡnh viờn Ngụn ng Prolog l mt cụng c thc hin rừ ý tng ny Chng trỡnh dch Prolog u tiờn i ó chng t ú l mt ngụn ng thc hnh v c ph bin trờn ton th gii S phỏt trin ca lp trỡnh logic chớnh thc bt u vo cui nhng nm 1970 Nhng phỏt trin xa hn t c vo u thp k 80, bt u vi s xut hin ca quyn sỏch u tiờn núi v cỏc c s lp trỡnh logic Vic la chn lp trỡnh logic lm mụ hỡnh c s cho d ỏn Cỏc h thng mỏy tớnh i th ca Nht (Japanese Fifth Generation Computer Systems Project) ó m u cho s phỏt trin ca cỏc ngụn ng lp trỡnh logic khỏc Nh kh nng khai bỏo t nhiờn ca lp trỡnh logic, Prolog nhanh chúng tr thnh mt ng c viờn cho vic biu din tri thc Tớnh y ca nú tr nờn rừ rng hn mi liờn h gia cỏc chng trỡnh logic vi c s d liu suy din c a vo gia thp k 80 Vic s dng lp trỡnh logic v c s d liu suy din biu din tri thc c gi l cỏch tip cn logic cho vic biu din tri thc Cỏch tip cn ny da trờn ý tng l chng trỡnh mỏy tớnh c cung cp cỏc c thự Footer Page of 16 Header Page of 16 logic ca tri thc ú, ú nú c lp vi bt k cỏch thc hin riờng bit no, vi ng cnh t do, d dng thao tỏc v suy din Chớnh vỡ vy, cỳ phỏp ca ngụn ng lp trỡnh phi kt hp c bt k chng trỡnh no vi c thự khai bỏo ca nú Khi ú, vic thc hin cỏc phng phỏp tớnh toỏn s thụng qua so sỏnh cỏc thuc tớnh c th vi cỳ phỏp khai bỏo Vic a mt cỳ phỏp thớch hp cho cỏc chng trỡnh logic c coi nh mt nhng lnh vc nghiờn cu quan trng nht v khú nht lp trỡnh logic Lun ny s trỡnh by cỏc kt qu nghiờn cu v cỳ phỏp v ng ngha ca chng trỡnh logic, bao gm cỏc lp trỡnh logic thụng thng v lp trỡnh logic m rng, tip ú s cp mụi trng lp trỡnh logic DLV (Datalog with Vel) v cỏch thc kt hp mụi trng logic ny mó ngun hng i tng Java, cui cựng trỡnh by hai bi toỏn minh (bi toỏn N quõn hu v bi toỏn Cõy khung nh nht) c ci t trờn DLV v c chy mó ngun hng i tng Java Footer Page of 16 Header Page of 16 Chng CHNG TRèNH LOGIC TNG QUT 1.1 M u Ngụn ng ca mt chng trỡnh logic tng quỏt c xõy dng trờn bng ch cỏi c nh ngha nh sau: nh ngha 1.1 Bng ch cỏi bao gm cỏc loi ký hiu sau: - Cỏc bin - Cỏc hng s i tng (cú th gi l hng s) - Cỏc ký hiu hm (function symbol) - Cỏc ký hiu v t (predicate symbol) - Cỏc liờn kt logic: not, v , - Cỏc ký hiu phõn cỏch ( v ) Trong ú, not l liờn kt logic c gi l ph nh ngm (negation as failure); bin l xõu bt k bao gm cỏc ký t ca bng ch cỏi v cỏc ch s, c bt u bng ch cỏi vit hoa; hng s, ký hiu hm v ký hiu v t l cỏc xõu bt u bi ch cỏi vit thng Thụng thng, s dng cỏc ch cỏi p, q, cho cỏc ký hiu v t, X, Y, Z, cho cỏc bin, f, g, h, cho cỏc ký hiu hm v a, b, c, cho cỏc hng s nh ngha 1.2 Mt toỏn hng c nh ngha nh sau: Footer Page of 16 Header Page of 16 (i) bin l toỏn hng, (ii) hng s l toỏn hng, (iii) Nu f l mt ký hiu hm bc n v t1 , , tn l cỏc toỏn hng thỡ f ( t1 , , tn ) cng l mt toỏn hng nh ngha 1.3 Mt toỏn hng c gi l cú tớnh cht nn (ground) nu khụng cú bin no xut hin nú nh ngha 1.4 Mt nguyờn t biu din trờn bng ch cỏi l mt biu thc cú dng p ( t1 , , tn ) , ú p l mt ký hiu v t v ti l cỏc toỏn hng Nu mi ti l toỏn hng nn thỡ nguyờn t ny cng c gi l cú tớnh cht nn Mt lut ca chng trỡnh c biu din di dng: A0 A1 , , Am , not Am+1 , , not An (1.1) ú, Ai l cỏc nguyờn t V trỏi ca lut c gi phn u hay l kt lun, v phi ca lut l phn thõn hay l gi thit Mt cỏc lut to thnh mt chng trỡnh logic tng quỏt (cũn c gi l chng trỡnh logic thụng thng) Chng trỡnh logic tng quỏt khụng cha not thỡ c gi l chng trỡnh xỏc nh Cỏc biu thc v lut khụng cha bin thỡ c gi l cú tớnh cht nn nh ngha 1.5 Khụng gian xỏc nh Herbrand biu din trờn ngụn ng ca chng trỡnh , ký hiu l HU ( ) , l tt c cỏc toỏn hng nn c biu din vi cỏc hm v hng s Tp tt c cỏc nguyờn t nn ngụn ng ca mt chng trỡnh c nh ngha l HB ( ) (c s Herbrand ca ) Vi mt v t p, atoms(p) c nh ngha l ca Footer Page of 16 Header Page of 16 HB ( ) c biu din di dng v t p v vi mt cỏc v t A, atoms(A) l mt cỏc phn t ca HB ( ) c biu din di dng cỏc v t thuc A Vớ d 1.1 Xột chng trỡnh logic thụng thng sau: p ( a ) p ( b ) p ( c ) p ( f ( X ) ) p ( X ) Ngụn ng ca chng trỡnh da trờn bng ch cỏi bao gm v t p, hm f v cỏc hng s a, b v c { } HU ( ) = a, b, c, f ( a ) , f ( b ) , f ( c ) , f ( f ( a ) ) , f ( f ( b ) ) , { ( ) } HB ( ) = p ( a ) , p ( b ) , p ( c ) , p ( f ( a ) ) , p ( f ( b ) ) , p ( f ( c ) ) , p f ( f ( a ) ) , Mt chng trỡnh logic c coi l mt c t cho phộp xõy dng cỏc lý thuyt cú th cho mt th gii quan cũn cỏc lut chng trỡnh l nhng rng buc m cỏc lý thuyt ny cn phi tha Ng ngha ca chng trỡnh logic c phõn bit tựy theo cỏch nh ngha tớnh tha cỏc lut Trong lun ny s s dng ng ngha v mụ hỡnh n nh v cỏc dng m rng ca nú Vi ng ngha ny, cỏc lý thuyt c xỏc nh nh cỏc nguyờn t nn, gi l cỏc mụ hỡnh n nh ca mt chng trỡnh Ng ngha c nh ngha nh sau: nh ngha 1.6 Mụ hỡnh n nh ca mt chng trỡnh xỏc nh l mt nh nht S ca HB cho vi mi lut A0 A1 , , Am ca , nu A1 , , Am S thỡ A0 S Footer Page of 16 Header Page 10 of 16 Mụ hỡnh n nh ca chng trỡnh xỏc nh c ký hiu l a( ) Gi l mt chng trỡnh logic tng quỏt bt k Vi mi phn t S, t S l mt chng trỡnh thu c t bng cỏch xúa: (i) cỏc lut cú cha not A vi A S (ii) tt c cỏc not A cỏc lut cũn li Rừ rng, S khụng cha not v tn ti mt mụ hỡnh n nh ó nh ngha trờn Nu mụ hỡnh n nh ny trựng vi S, thỡ ta núi rng S l mt mụ hỡnh n nh ca Hay núi cỏch khỏc, mụ hỡnh n nh ca c biu din bi phng trỡnh: S = a (S ) (1.2) Mt phn t nn P l ỳng S nu P S , ngc li P l sai (tc l ơP l ỳng) S suy din mt biu thc f (ký hiu bi |= f ) nu f l ỳng mi mụ hỡnh n nh ca Ta cng núi rng cõu tr li cho mt truy nn q l cú nu q l ỳng mi mụ hỡnh n nh ca (tc l |= q ), l khụng nu ơq l ỳng mi mụ hỡnh n nh ca (tc l |= ơq ) v khụng xỏc nh trng hp cũn li Vớ d 1.2 Xột ngụn ng cha hai i tng a v b v mt chng trỡnh : p ( X ) not q ( X ) q ( a ) Ta s ch rng S = {q ( a ) , p ( b )} l mt mụ hỡnh n nh ca Xõy dng chng trỡnh S theo cỏch trờn, ta cú S = { p ( b ) , q ( a ) } cú mt mụ hỡnh n nh trựng vi S Do ú S chớnh l mụ hỡnh n nh ca Footer Page 10 of 16 98 Header Page 100 of 16 Bi toỏn Cõy khung nh nht DLV: root(a) node(a) node(b) node(c) node(d) node(e) edge(a, b, 4) edge(a, c, 3) edge(c, b, edge(b, e, 4) edge(d, e, 5) in_tree(X, Y, C) v out_tree(X, Y) :edge(X, Y, C), reached(X) :- root(X), in_tree(_, X, C) :- in_tree(X, Y, C), in_tree(Z, Y, C), X != Z reached(X) :- root(X) reached(Y) :reached(X), in_tree(X, Y, C) :- node(X), not reached(X) :~ in_tree(X, Y, C) [C:1] Footer Page 100 of 16 2) edge(c, d, 3) Header Page 101 of 16 PH LC Bi toỏn N quõn hu trờn DLV: row(X) :- #int(X), X > col(X) :- #int(X), X > out(X, Y) :- row(X), col(Y), not in(X, Y) in(X, Y) :- row(X), col(Y), not out(X, Y) has_queen(X) :- row(X), col(Y), in(X, Y) :- row(X), not has_queen(X) :- Y YY, in(X, Y), in(X, YY) :- X XX, in(X, Y), in(XX, Y) :- in(X1, Y1), in(X2, Y2), X2=X1+N, Y2 = Y1 + N, N > :- in(X1, Y1), in(X2, Y2), X2=X1+N, Y1 = Y2 + N, N > Bi toỏn Cõy khung nh nht DLV: root(a) node(a) node(b) node(c) node(d) node(e) edge(a, b, 4) edge(a, c, 3) edge(c, b, 2) edge(c, d, 3) edge(b, e, 4) edge(d, e, 5) in_tree(X, Y, C) v out_tree(X, Y) :edge(X, Y, C), reached(X) :- root(X), in_tree(_, X, C) :- in_tree(X, Y, C), in_tree(Z, Y, C), X != Z reached(X) :- root(X) reached(Y) :reached(X), in_tree(X, Y, C) :- node(X), not reached(X) :~ in_tree(X, Y, C) [C:1] Footer Page 101 of 16 Header Page 102 of 16 Chng trỡnh MSTGUI.java package com.studyMST; import java.awt.Container; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.WindowEvent; import java.io.File; import java.io.IOException; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextField; import org.jgraph.JGraph; import org.jgraph.graph.DefaultGraphModel; import org.jgraph.graph.GraphModel; import DLV.DLVInvocationException; public class MSTGUI extends JFrame { public Container contentPane; private JTextField textNameInputFile = new JTextField("MST.inp"); private JButton buttonOpenInputFile = new JButton("Open Input File"); private JTextField textNameProgramFile = new JTextField("MST.dl"); private JButton buttonOpenProgramFile = new JButton("Open Program File"); private JTextField textNameOutputFile = new JTextField("MST.out"); private JButton buttonOpenOutputFile = new JButton("Open Output File"); private JButton buttonPress = new JButton("Solve!"); private JFileChooser fileChooserInput = new JFileChooser(); private JPanel inputPanel = new JPanel(); private static final long serialVersionUID = 1L; public static void main(String[] args) throws Exception, DLVInvocationException { MSTGUI studyGUIObject = new MSTGUI(); studyGUIObject.setSize(800, 175); studyGUIObject.contentPane = studyGUIObject.getContentPane(); Footer Page 102 of 16 Header Page 103 of 16 studyGUIObject.contentPane.setLayout(new BoxLayout(studyGUIObject.contentPane, BoxLayout.Y_AXIS)); studyGUIObject.initPanels(); studyGUIObject.initContainer(); studyGUIObject.setVisible(true); studyGUIObject.addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(WindowEvent winEvt) { System.exit(0); } }); } public void initContainer() { this.contentPane = this.getContentPane(); this.contentPane.add(this.inputPanel); } public void initPanels() throws IOException, DLVInvocationException { //set the layout to be rows and columns GridLayout inputPanelLayout = new GridLayout(4,3); this.inputPanel.setLayout(inputPanelLayout); //first row this.inputPanel.add(new JLabel("Enter MST Input File")); textNameInputFile.setColumns(25); this.inputPanel.add(textNameInputFile); this.buttonOpenInputFile.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { buttonOpenInputFileActionPerformed(evt); } }); this.inputPanel.add(buttonOpenInputFile); //second row this.inputPanel.add(new JLabel("Enter MST Program File")); textNameProgramFile.setColumns(25); this.inputPanel.add(textNameProgramFile); Footer Page 103 of 16 Header Page 104 of 16 this.buttonOpenProgramFile.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { buttonOpenProgramFileActionPerformed(evt); } }); this.inputPanel.add(buttonOpenProgramFile); //third row this.inputPanel.add(new JLabel("Enter MST Output File")); textNameOutputFile.setColumns(25); this.inputPanel.add(textNameOutputFile); this.buttonOpenOutputFile.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { buttonOpenOutputFileActionPerformed(evt); } }); this.inputPanel.add(buttonOpenOutputFile); //fourth row this.buttonPress.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { try { buttonPressActionPerformed(evt); } catch (IOException e) { e.printStackTrace(); } catch (DLVInvocationException e) { e.printStackTrace(); } } }); this.inputPanel.add(new JLabel("")); this.inputPanel.add(new JLabel("")); this.inputPanel.add(buttonPress); } private void buttonOpenInputFileActionPerformed(ActionEvent evt) { int returnVal = fileChooserInput.showOpenDialog(MSTGUI.this); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = fileChooserInput.getSelectedFile(); Footer Page 104 of 16 Header Page 105 of 16 //This is where a real application would open the file textNameInputFile.setText(file.getAbsolutePath()); } } private void buttonOpenProgramFileActionPerformed(ActionEvent evt) { int returnVal = fileChooserInput.showOpenDialog(MSTGUI.this); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = fileChooserInput.getSelectedFile(); //This is where a real application would open the file textNameProgramFile.setText(file.getAbsolutePath()); } } private void buttonOpenOutputFileActionPerformed(ActionEvent evt) { int returnVal = fileChooserInput.showOpenDialog(MSTGUI.this); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = fileChooserInput.getSelectedFile(); //This is where a real application would open the file textNameOutputFile.setText(file.getAbsolutePath()); } } private void buttonPressActionPerformed(java.awt.event.ActionEvent evt)throws IOException, DLVInvocationException { String inputFileName = textNameInputFile.getText(); String outputFileName = textNameOutputFile.getText(); String dlvFileName = textNameProgramFile.getText(); MST mstObject = new MST(); GraphModel model = new DefaultGraphModel(); mstObject.dlvFileName = dlvFileName; mstObject.inputFileName = inputFileName; mstObject.outputFileName = outputFileName; mstObject.graph = new JGraph(model); mstObject.drawGraphFromInput(); mstObject.calculateMST(); mstObject.drawMSTFromOutput(); //create a new frame for the result JFrame resultFrame = new JFrame(); Footer Page 105 of 16 Header Page 106 of 16 //add the result to the new frame just created resultFrame.getContentPane().add(new JScrollPane(mstObject.graph)); resultFrame.setSize(800, 600); resultFrame.setVisible(true); resultFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); } } Chng trỡnh MST.java: package com.studyMST; import java.awt.Color; import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashMap; import java.util.Hashtable; import java.util.Map; import java.util.Random; import java.util.StringTokenizer; import javax.swing.BorderFactory; import org.jgraph.JGraph; import org.jgraph.graph.DefaultEdge; import org.jgraph.graph.DefaultGraphCell; import org.jgraph.graph.GraphConstants; import DLV.DLVException; import DLV.DLVExceptionUncheked; import DLV.DLVInvocationException; import DLV.DlvHandler; import DLV.Model; import DLV.Predicate; import DLV.Program; public class MST { private static final int NODE = 1; private static final int EDGE = 2; Footer Page 106 of 16 Header Page 107 of 16 private HashMap mapOfNodes = new HashMap(); private ArrayList listOfEdges = new ArrayList(); public ArrayList listOfInTree = new ArrayList(); private HashMap mapOfEdges = new HashMap(); public String dlvFileName = "MST.dl"; public String inputFileName = "MST.inp"; public String outputFileName = "MST.out"; private String dlvExeFile = "dl.exe"; public JGraph graph; /** * @param args * @throws IOException */ public void calculateMST () throws IOException, DLVInvocationException{ File outputFile = new File(this.outputFileName); FileWriter out = new FileWriter(outputFile); // build a Program object and setup input Program pr=new Program(); // set input pr.addProgramFile(this.dlvFileName); pr.addProgramFile(this.inputFileName); // build a DlvHandler object DlvHandler dlv=new DlvHandler(this.dlvExeFile); // set input program dlv.setProgram(pr); // set invocation parameters dlv.setNumberOfModels(1); // computes no more than solutions dlv.setIncludeFacts(false); dlv.setFilter(new String[]{"in_tree"}); try { // run DLV by using model synchronous method of invocation dlv.run(DlvHandler.MODEL_SYNCHRONOUS); // DLV output handling while(dlv.hasMoreModels()) // for each model, wait until DLV find a new model { Footer Page 107 of 16 Header Page 108 of 16 Model m=dlv.nextModel(); // gets next model if(!m.isNoModel()) { while(m.hasMorePredicates()) // for each predicate in m { Predicate p=m.nextPredicate(); // gets next predicate System.out.println(p.toString()); // print out p out.write(p.toString()); } System.out.println(" - END Model"); } else System.out.println("I cannot find a model"); } } catch(DLVException d) { d.printStackTrace(); } catch(DLVExceptionUncheked du) { du.printStackTrace(); } finally { System.err.println(dlv.getWarnings()); // print out errors } out.close(); } public void drawGraphFromInput() throws IOException { BufferedReader d = new BufferedReader(new InputStreamReader(new FileInputStream(new File(this.inputFileName)))); String line = ""; while((line = d.readLine()) != null) { String verticeOrEdge = line.substring(line.indexOf("(") + 1, line.indexOf(")")); //System.out.println(verticeOrEdge); int type = this.getType(verticeOrEdge); if(type == MST.NODE) { Node theNode = new Node(); theNode.label = verticeOrEdge; mapOfNodes.put(theNode.label, theNode); } else if(type == MST.EDGE) { StringTokenizer st = new StringTokenizer(verticeOrEdge, ","); Footer Page 108 of 16 Header Page 109 of 16 String labelNode1 = st.nextToken().trim(); String labelNode2 = st.nextToken().trim(); int edgeWeight = Integer.parseInt(st.nextToken().trim()); Node node1 = (Node) mapOfNodes.get(labelNode1); Node node2 = (Node) mapOfNodes.get(labelNode2); Edge theEdge = new Edge(); theEdge.node1 = node1; theEdge.node2 = node2; theEdge.weight = edgeWeight; listOfEdges.add(theEdge); } else { } } System.out.println("no of nodes = " + mapOfNodes.size()); buildNodesCoordinate(); buildGraph(); } public void drawMSTFromOutput() throws IOException { BufferedReader d = new BufferedReader(new InputStreamReader(new FileInputStream(new File(this.outputFileName)))); String line = ""; while((line = d.readLine()) != null) { String edge = line.substring(line.indexOf("(") + 1, line.indexOf(")")); StringTokenizer st = new StringTokenizer(edge, ","); String labelNode1 = st.nextToken().trim(); String labelNode2 = st.nextToken().trim(); int edgeWeight = Integer.parseInt(st.nextToken().trim()); Node node1 = (Node) mapOfNodes.get(labelNode1); Node node2 = (Node) mapOfNodes.get(labelNode2); Footer Page 109 of 16 Header Page 110 of 16 Edge theEdge = new Edge(); theEdge.node1 = node1; theEdge.node2 = node2; theEdge.weight = edgeWeight; listOfInTree.add(theEdge); } modifyGraph(); } public void modifyGraph() { for(int i=0; i

Ngày đăng: 15/03/2017, 07:47

Từ khóa liên quan

Mục lục

  • bìa

  • Loi cam on

  • MỤC LỤC

  • MỞ ĐẦU

  • Chương 1

  • Chương 2

  • Chương 3

  • Chương 4

  • KET LUAN

  • TÀI LIỆU THAM KHẢO

  • PHỤ LỤC

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan