private static int GenerateGraphsToFile() { // Variable string reader; bool writer, clearFile, useGeneticAlgorithm2, useInterchangeExtendedK3; int constant = 0, exponent = 0; int minCount = 0, maxCount = 0; Console.Clear(); Console.WriteLine("Generate graphs to file"); Console.WriteLine("Info: the file will be saved in " + GenerateGraphs.GenerateGraphsFile.GetPathFile()); Console.Write("Write report to console [true | false]: "); reader = Console.ReadLine(); bool.TryParse(reader, out writer); Console.Write("Clear file [true | false]: "); reader = Console.ReadLine(); bool.TryParse(reader, out clearFile); Console.Write("Use genetic algorithm (exponent: 2) [true | false]: "); reader = Console.ReadLine(); bool.TryParse(reader, out useGeneticAlgorithm2); Console.Write("Use algorithms with interchangeExtended with K3 [true | false]: "); reader = Console.ReadLine(); bool.TryParse(reader, out useInterchangeExtendedK3); do { Console.Write("Minimum number of vertices [positive int]: "); reader = Console.ReadLine(); int.TryParse(reader, out minCount); }while (minCount <= 0); do { Console.Write("Maximum number of vertices [positive int]: "); reader = Console.ReadLine(); int.TryParse(reader, out maxCount); }while (maxCount <= 0); do { Console.Write("Number of generated graphs - constant (constant * (number of vertices)^(exponent)) [positive int]: "); reader = Console.ReadLine(); int.TryParse(reader, out constant); }while (constant <= 0); do { Console.Write("Number of generated graphs - exponent (constant * (number of vertices)^(exponent)) [positive int]: "); reader = Console.ReadLine(); int.TryParse(reader, out exponent); }while (exponent <= 0); try { GenerateGraphs.GenerateGraphs generateGraphs = new GenerateGraphs.GenerateGraphsFile(constant, exponent, writer, clearFile, useGeneticAlgorithm2, useInterchangeExtendedK3); Console.WriteLine(); Console.WriteLine("Start generating... "); Console.WriteLine(); generateGraphs.Generate(minCount, maxCount); Console.WriteLine(); Console.WriteLine("Graphs were generated and saved."); Console.WriteLine(); } catch (GraphColoring.MyException.ReaderWriterException.ReaderWriterException ex) { Console.WriteLine("Something wrong: " + ex.GetType()); return(1); } catch (IOException ex) { Console.WriteLine("Something wrong: " + ex.GetType()); return(1); } return(0); }
/// <summary> /// Save data from the file to the DB /// If the file does not exist throws ReaderWriterFileDoesNotExist /// File path - GenerateGraphsFile.GetPath() /// </summary> public void SaveDataFromFileToDB() { string pathFolder = GenerateGraphsFile.GetPathFolder(); string pathFileNameExtended = GenerateGraphsFile.GetFileNameExtension(); if (!Directory.Exists(pathFolder)) { Directory.CreateDirectory(pathFolder); return; } // Variable string line; int lineNumber = 0; int ID_Graph = 0; bool graphExists = false, error = false; int CountVertices, CountEdges, MaximumVertexDegree, MinimumVertexDegree, MedianVertexDegree, CountCutVertices, CountBridges, Girth; bool IsChordal, IsRegular, IsCyclic; double AverageVertexDegree; int[] VertexDegreeArray; GraphColoring.Graph.GraphClass.GraphClass.GraphClassEnum GraphClass; GraphColoring.Graph.GraphProperty.GraphProperty.EulerianGraphEnum EulerianGraph; foreach (string filePath in Directory.EnumerateFiles(pathFolder, "*." + pathFileNameExtended)) { if (writer) { Console.WriteLine("Reading file: {0}", filePath); } using (FileStream fileStream = File.OpenRead(filePath)) using (StreamReader streamReader = new StreamReader(fileStream, Encoding.UTF8)) { while ((line = streamReader.ReadLine()) != null) { lineNumber++; if (line.StartsWith(GRAPHCOMMENT)) { continue; } // Graph if (line.StartsWith(GRAPHHEADER)) { try { error = false; if (writer && ID_Graph != 0) { if (writer) { if (!graphExists) { Console.WriteLine("Added graph with ID {0}", ID_Graph); } else { Console.WriteLine("Updated graph with ID {0}", ID_Graph); } } } string[] lineArray = line.Split(' '); if (lineArray.Length <= 15) { throw new ArgumentException(); } // Fill variables CountVertices = int.Parse(lineArray[1]); CountEdges = int.Parse(lineArray[2]); GraphClass = (GraphColoring.Graph.GraphClass.GraphClass.GraphClassEnum)Enum.Parse(typeof(GraphColoring.Graph.GraphClass.GraphClass.GraphClassEnum), lineArray[3]); IsChordal = bool.Parse(lineArray[4]); IsRegular = bool.Parse(lineArray[5]); IsCyclic = bool.Parse(lineArray[6]); EulerianGraph = (GraphColoring.Graph.GraphProperty.GraphProperty.EulerianGraphEnum)Enum.Parse(typeof(GraphColoring.Graph.GraphProperty.GraphProperty.EulerianGraphEnum), lineArray[7]); MaximumVertexDegree = int.Parse(lineArray[8]); MinimumVertexDegree = int.Parse(lineArray[9]); AverageVertexDegree = double.Parse(lineArray[10]); MedianVertexDegree = int.Parse(lineArray[11]); CountCutVertices = int.Parse(lineArray[12]); CountBridges = int.Parse(lineArray[13]); Girth = int.Parse(lineArray[14]); VertexDegreeArray = new int[lineArray.Length - 15]; for (int i = 0; i < VertexDegreeArray.Length; i++) { VertexDegreeArray[i] = int.Parse(lineArray[15 + i]); } if (VertexDegreeArray.Length != CountVertices) { throw new ArgumentException(); } // DB graphExists = database.ExistsGraph(CountVertices, CountEdges, GraphClass, IsChordal, IsRegular, IsCyclic, EulerianGraph, MaximumVertexDegree, MinimumVertexDegree, AverageVertexDegree, MedianVertexDegree, CountCutVertices, CountBridges, Girth, VertexDegreeArray); if (!graphExists) { database.InsertGraph(CountVertices, CountEdges, GraphClass, IsChordal, IsRegular, IsCyclic, EulerianGraph, MaximumVertexDegree, MinimumVertexDegree, AverageVertexDegree, MedianVertexDegree, CountCutVertices, CountBridges, Girth, VertexDegreeArray); } ID_Graph = database.GetGraph(CountVertices, CountEdges, GraphClass, IsChordal, IsRegular, IsCyclic, EulerianGraph, MaximumVertexDegree, MinimumVertexDegree, AverageVertexDegree, MedianVertexDegree, CountCutVertices, CountBridges, Girth, VertexDegreeArray); } catch (FormatException) { Console.WriteLine("Can't parse this line [{0}] (invalid type): {1}", lineNumber, line); error = true; ID_Graph = 0; } catch (ArgumentException) { Console.WriteLine("Can't parse this line [{0}] (invalid count of parameters): {1}", lineNumber, line); error = true; ID_Graph = 0; } catch (MyException.DatabaseException.GenerateGraphsDatabaseNotOpenException ex) { throw ex; } catch (Exception) { Console.WriteLine("Something wrong with this line [{0}]: {1}", lineNumber, line); error = true; ID_Graph = 0; } } // Algorithms else { if (error || ID_Graph <= 0) { continue; } try { string[] lineArray = line.Split(' '); if (lineArray.Length != 2 && lineArray.Length != 4) { throw new ArgumentException(); } switch ((GraphColoring.GraphColoringAlgorithm.GraphColoringAlgorithm.GraphColoringAlgorithmEnum)Enum.Parse(typeof(GraphColoring.GraphColoringAlgorithm.GraphColoringAlgorithm.GraphColoringAlgorithmEnum), lineArray[0], false)) { case GraphColoring.GraphColoringAlgorithm.GraphColoringAlgorithm.GraphColoringAlgorithmEnum.randomSequence: database.AddGraphColoring(ID_Graph, GraphColoring.GraphColoringAlgorithm.GraphColoringAlgorithm.GraphColoringAlgorithmEnum.randomSequence, int.Parse(lineArray[1]), int.Parse(lineArray[2]), int.Parse(lineArray[3])); break; case GraphColoring.GraphColoringAlgorithm.GraphColoringAlgorithm.GraphColoringAlgorithmEnum.largestFirstSequence: database.AddGraphColoring(ID_Graph, GraphColoring.GraphColoringAlgorithm.GraphColoringAlgorithm.GraphColoringAlgorithmEnum.largestFirstSequence, int.Parse(lineArray[1])); break; case GraphColoring.GraphColoringAlgorithm.GraphColoringAlgorithm.GraphColoringAlgorithmEnum.smallestLastSequence: database.AddGraphColoring(ID_Graph, GraphColoring.GraphColoringAlgorithm.GraphColoringAlgorithm.GraphColoringAlgorithmEnum.smallestLastSequence, int.Parse(lineArray[1])); break; case GraphColoring.GraphColoringAlgorithm.GraphColoringAlgorithm.GraphColoringAlgorithmEnum.randomSequenceInterchange: database.AddGraphColoring(ID_Graph, GraphColoring.GraphColoringAlgorithm.GraphColoringAlgorithm.GraphColoringAlgorithmEnum.randomSequenceInterchange, int.Parse(lineArray[1]), int.Parse(lineArray[2]), int.Parse(lineArray[3])); break; case GraphColoring.GraphColoringAlgorithm.GraphColoringAlgorithm.GraphColoringAlgorithmEnum.largestFirstSequenceInterchange: database.AddGraphColoring(ID_Graph, GraphColoring.GraphColoringAlgorithm.GraphColoringAlgorithm.GraphColoringAlgorithmEnum.largestFirstSequenceInterchange, int.Parse(lineArray[1])); break; case GraphColoring.GraphColoringAlgorithm.GraphColoringAlgorithm.GraphColoringAlgorithmEnum.smallestLastSequenceInterchange: database.AddGraphColoring(ID_Graph, GraphColoring.GraphColoringAlgorithm.GraphColoringAlgorithm.GraphColoringAlgorithmEnum.smallestLastSequenceInterchange, int.Parse(lineArray[1])); break; case GraphColoring.GraphColoringAlgorithm.GraphColoringAlgorithm.GraphColoringAlgorithmEnum.randomSequenceInterchangeExtended: database.AddGraphColoring(ID_Graph, GraphColoring.GraphColoringAlgorithm.GraphColoringAlgorithm.GraphColoringAlgorithmEnum.randomSequenceInterchangeExtended, int.Parse(lineArray[1]), int.Parse(lineArray[2]), int.Parse(lineArray[3])); break; case GraphColoring.GraphColoringAlgorithm.GraphColoringAlgorithm.GraphColoringAlgorithmEnum.largestFirstSequenceInterchangeExtended: database.AddGraphColoring(ID_Graph, GraphColoring.GraphColoringAlgorithm.GraphColoringAlgorithm.GraphColoringAlgorithmEnum.largestFirstSequenceInterchangeExtended, int.Parse(lineArray[1])); break; case GraphColoring.GraphColoringAlgorithm.GraphColoringAlgorithm.GraphColoringAlgorithmEnum.smallestLastSequenceInterchangeExtended: database.AddGraphColoring(ID_Graph, GraphColoring.GraphColoringAlgorithm.GraphColoringAlgorithm.GraphColoringAlgorithmEnum.smallestLastSequenceInterchangeExtended, int.Parse(lineArray[1])); break; case GraphColoring.GraphColoringAlgorithm.GraphColoringAlgorithm.GraphColoringAlgorithmEnum.randomSequenceInterchangeExtendedK3: database.AddGraphColoring(ID_Graph, GraphColoring.GraphColoringAlgorithm.GraphColoringAlgorithm.GraphColoringAlgorithmEnum.randomSequenceInterchangeExtendedK3, int.Parse(lineArray[1]), int.Parse(lineArray[2]), int.Parse(lineArray[3])); break; case GraphColoring.GraphColoringAlgorithm.GraphColoringAlgorithm.GraphColoringAlgorithmEnum.largestFirstSequenceInterchangeExtendedK3: database.AddGraphColoring(ID_Graph, GraphColoring.GraphColoringAlgorithm.GraphColoringAlgorithm.GraphColoringAlgorithmEnum.largestFirstSequenceInterchangeExtendedK3, int.Parse(lineArray[1])); break; case GraphColoring.GraphColoringAlgorithm.GraphColoringAlgorithm.GraphColoringAlgorithmEnum.smallestLastSequenceInterchangeExtendedK3: database.AddGraphColoring(ID_Graph, GraphColoring.GraphColoringAlgorithm.GraphColoringAlgorithm.GraphColoringAlgorithmEnum.smallestLastSequenceInterchangeExtendedK3, int.Parse(lineArray[1])); break; case GraphColoring.GraphColoringAlgorithm.GraphColoringAlgorithm.GraphColoringAlgorithmEnum.connectedSequential: database.AddGraphColoring(ID_Graph, GraphColoring.GraphColoringAlgorithm.GraphColoringAlgorithm.GraphColoringAlgorithmEnum.connectedSequential, int.Parse(lineArray[1])); break; case GraphColoring.GraphColoringAlgorithm.GraphColoringAlgorithm.GraphColoringAlgorithmEnum.saturationLargestFirstSequence: database.AddGraphColoring(ID_Graph, GraphColoring.GraphColoringAlgorithm.GraphColoringAlgorithm.GraphColoringAlgorithmEnum.saturationLargestFirstSequence, int.Parse(lineArray[1])); break; case GraphColoring.GraphColoringAlgorithm.GraphColoringAlgorithm.GraphColoringAlgorithmEnum.greedyIndependentSet: database.AddGraphColoring(ID_Graph, GraphColoring.GraphColoringAlgorithm.GraphColoringAlgorithm.GraphColoringAlgorithmEnum.greedyIndependentSet, int.Parse(lineArray[1])); break; case GraphColoring.GraphColoringAlgorithm.GraphColoringAlgorithm.GraphColoringAlgorithmEnum.combinationAlgorithm: database.AddGraphColoring(ID_Graph, GraphColoring.GraphColoringAlgorithm.GraphColoringAlgorithm.GraphColoringAlgorithmEnum.combinationAlgorithm, int.Parse(lineArray[1])); break; case GraphColoring.GraphColoringAlgorithm.GraphColoringAlgorithm.GraphColoringAlgorithmEnum.geneticAlgorithm: database.AddGraphColoring(ID_Graph, GraphColoring.GraphColoringAlgorithm.GraphColoringAlgorithm.GraphColoringAlgorithmEnum.geneticAlgorithm, int.Parse(lineArray[1]), int.Parse(lineArray[2]), int.Parse(lineArray[3])); break; case GraphColoring.GraphColoringAlgorithm.GraphColoringAlgorithm.GraphColoringAlgorithmEnum.geneticAlgorithm2: database.AddGraphColoring(ID_Graph, GraphColoring.GraphColoringAlgorithm.GraphColoringAlgorithm.GraphColoringAlgorithmEnum.geneticAlgorithm2, int.Parse(lineArray[1]), int.Parse(lineArray[2]), int.Parse(lineArray[3])); break; case GraphColoring.GraphColoringAlgorithm.GraphColoringAlgorithm.GraphColoringAlgorithmEnum.connectedLargestFirst: database.AddGraphColoring(ID_Graph, GraphColoring.GraphColoringAlgorithm.GraphColoringAlgorithm.GraphColoringAlgorithmEnum.connectedLargestFirst, int.Parse(lineArray[1])); break; case GraphColoring.GraphColoringAlgorithm.GraphColoringAlgorithm.GraphColoringAlgorithmEnum.connectedLargestFirstInterchange: database.AddGraphColoring(ID_Graph, GraphColoring.GraphColoringAlgorithm.GraphColoringAlgorithm.GraphColoringAlgorithmEnum.connectedLargestFirstInterchange, int.Parse(lineArray[1])); break; case GraphColoring.GraphColoringAlgorithm.GraphColoringAlgorithm.GraphColoringAlgorithmEnum.connectedLargestFirstInterchangeExtended: database.AddGraphColoring(ID_Graph, GraphColoring.GraphColoringAlgorithm.GraphColoringAlgorithm.GraphColoringAlgorithmEnum.connectedLargestFirstInterchangeExtended, int.Parse(lineArray[1])); break; case GraphColoring.GraphColoringAlgorithm.GraphColoringAlgorithm.GraphColoringAlgorithmEnum.connectedLargestFirstInterchangeExtendedK3: database.AddGraphColoring(ID_Graph, GraphColoring.GraphColoringAlgorithm.GraphColoringAlgorithm.GraphColoringAlgorithmEnum.connectedLargestFirstInterchangeExtendedK3, int.Parse(lineArray[1])); break; case GraphColoring.GraphColoringAlgorithm.GraphColoringAlgorithm.GraphColoringAlgorithmEnum.AI: database.AddGraphColoring(ID_Graph, GraphColoring.GraphColoringAlgorithm.GraphColoringAlgorithm.GraphColoringAlgorithmEnum.AI, int.Parse(lineArray[1])); break; default: throw new MyException.GenerateGraphsException.GenerateGraphsAlgorithmDoesNotExistException(lineArray[0]); } } catch (ArgumentException) { Console.WriteLine("Can't parse this line [{0}] (invalid count of parameters): {1}", lineNumber, line); } catch (MyException.DatabaseException.DatabaseAlgorithmDoesNotExistException) { Console.WriteLine("Algorithm does not exist: {0}", line); } catch (Exception) { Console.WriteLine("Something wrong with this line [{0}] : {1}", lineNumber, line); } } } } if (writer && ID_Graph != 0) { if (writer) { if (!graphExists) { Console.WriteLine("Added graph with ID {0}", ID_Graph); } else { Console.WriteLine("Updated graph with ID {0}", ID_Graph); } } } Console.WriteLine(); } }