public void GenerateBaseModel(string datFilePath) { _constraintModel = DatFileParser.ParseDatFile(datFilePath); ExampleName = new FileInfo(datFilePath).Name; try { var path = @"C:\IJCAI\Output\TestRuns\Logs\"; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } env = new GRBEnv(path + ExampleName + ".log") { LogToConsole = 0, NodefileStart = 0.5 }; _model = new GRBModel(env); _k = _constraintModel.K; _b = _constraintModel.B; // Optimization values S = _model.AddVar(0.0, _k - 1, 0.0, GRB.CONTINUOUS, "S"); // Number of interrupted job pairs L = _model.AddVar(0.0, _k - 1, 0.0, GRB.CONTINUOUS, "L"); // Longest time a cable resides in storage M = _model.AddVar(0.0, _k - 1, 0.0, GRB.CONTINUOUS, "M"); // Maximum number of cables stored simultaneously N = _model.AddVar(0.0, _k - 1, 0.0, GRB.CONTINUOUS, "N"); // Number of violated soft atomic constraints pfc = CreatePermutationVariable(); // objective var objExpr = new GRBQuadExpr(); objExpr.AddTerm(Math.Pow(_k, 3), S); objExpr.AddTerm(Math.Pow(_k, 2), M); objExpr.AddTerm(Math.Pow(_k, 1), L); objExpr.AddTerm(Math.Pow(_k, 0), N); _model.SetObjective(objExpr); _model.Parameters.TimeLimit = 300; // 300 seconds } catch (Exception e) { Console.WriteLine(e.ToString()); } }
public static ConstraintModel ParseDatFile(string path) { if (!path.EndsWith(".dat")) { var fileWithoutExtension = path; while (fileWithoutExtension.Contains('.')) { fileWithoutExtension = Path.GetFileNameWithoutExtension(fileWithoutExtension); } path = Path.GetDirectoryName(path) + @"\" + fileWithoutExtension + ".dat"; } var model = new ConstraintModel(); var state = ParseState.None; foreach (var line in File.ReadAllLines(path).Select(line => line.Trim())) { if (line.Contains("=") && line.EndsWith(";")) { if (line.StartsWith("k")) { model.Nbchambers = ParseInt(line); } if (line.StartsWith("b")) { model.B = ParseInt(line); } } if (line.EndsWith("};")) { state = ParseState.None; } if (state != ParseState.None) { var ints = ParseTuple(line); if (state == ParseState.AtomicConstraints) { model.AtomicConstraints.Add(ParseAtomicConstraint(ints)); } if (state == ParseState.SoftAtomicConstraints) { model.SoftAtomicConstraints.Add(ParseAtomicConstraint(ints)); } if (state == ParseState.DisjunctiveConstraints) { model.DisjunctiveConstraints.Add(ParseDisjunctiveConstraint(ints)); } if (state == ParseState.DirectSuccessors) { model.DirectSuccessors.Add(ParseDirectSuccessor(ints)); } } if (line.StartsWith("AtomicConstraints = {")) { state = ParseState.AtomicConstraints; } if (line.StartsWith("SoftAtomicConstraints = {")) { state = ParseState.SoftAtomicConstraints; } if (line.StartsWith("DisjunctiveConstraints = {")) { state = ParseState.DisjunctiveConstraints; } if (line.StartsWith("DirectSuccessors = {")) { state = ParseState.DirectSuccessors; } } return(model); }