/// <summary> /// Inits the block. /// </summary> /// <param name="eI">The e I.</param> private void InitBlock(AllenDomain eI) { EnclosingInstance = eI; mi = EnclosingInstance.minField; choice = mi; ma = EnclosingInstance.maxField; d = EnclosingInstance.Duration; s = EnclosingInstance.step; }
/// <summary> /// Caps the specified d. /// </summary> /// <param name="d">The d.</param> /// <returns></returns> public override Domain Cap(Domain d) { if (!(d is AllenDomain)) { return(EMPTY); } var newD = new AllenDomain(); AllenDomain d0 = this; var d1 = (AllenDomain)d; try { int i0 = 0; int i1 = 0; while (i0 < d0.intervals.Count && i1 < d1.intervals.Count) { var interval = (int[])d0.intervals[i0]; int min0 = interval[0]; int max0 = interval[1]; interval = (int[])d1.intervals[i1]; int min1 = interval[0]; int max1 = interval[1]; if (max0 < min1) { i0++; continue; } if (max1 < min0) { i1++; continue; } interval = new int[2]; interval[0] = Math.Max(min0, min1); interval[1] = Math.Min(max0, max1); newD.intervals.Add(interval); if (max0 <= max1) { i0++; } if (max1 <= max0) { i1++; } } } catch (IndexOutOfRangeException) { } newD.UpdateSize(); newD.UpdateMinMax(); if (newD.Empty) { return(EMPTY); } return(newD); }
/// <summary> /// Caps the interval. /// </summary> /// <param name="lo">The lo.</param> /// <param name="hi">The hi.</param> /// <returns></returns> public virtual AllenDomain CapInterval(int lo, int hi) { AllenDomain d = this; if (MinValue < lo) { d = d.Delete(MinValue, lo - 1); } if (hi < MaxValue) { d = d.Delete(hi + 1, MaxValue); } return(d); }
/// <summary> /// Deletes the specified lo. /// </summary> /// <param name="lo">The lo.</param> /// <param name="hi">The hi.</param> /// <returns></returns> public virtual AllenDomain Delete(int lo, int hi) { if (sizeField == 0 || lo > hi || hi < minField || maxField < lo) { return(this); } if (lo == hi) { return(Delete(lo)); } var d = new AllenDomain(); try { for (int i = 0; i < intervals.Count; i++) { var interval = (int[])intervals[i]; int mi = Math.Max(lo, interval[0]); int ma = Math.Min(hi, interval[1]); if (mi <= ma) { if (interval[0] < mi) { var inv = new int[2]; inv[0] = interval[0]; inv[1] = mi - 1; d.intervals.Add(inv); } if (ma < interval[1]) { var inv = new int[2]; inv[0] = ma + 1; inv[1] = interval[1]; d.intervals.Add(inv); } } else { var inv = new int[2]; inv[0] = interval[0]; inv[1] = interval[1]; d.intervals.Add(inv); } } } catch (IndexOutOfRangeException) { } d.UpdateSize(); d.UpdateMinMax(); return(d); }
/// <summary> /// Clones this instance. /// </summary> /// <returns></returns> public override Object Clone() { var d = new AllenDomain(); try { for (int i = 0; i < intervals.Count; ++i) { d.intervals.Add(((int[])intervals[i]).Clone()); } } catch (IndexOutOfRangeException) { } d.sizeField = sizeField; d.minField = minField; d.maxField = maxField; d.step = step; d.duration = duration; return(d); }
/// <summary> /// Satisfies the equals. /// </summary> /// <param name="v1">The v1.</param> /// <param name="v2">The v2.</param> /// <param name="trail">The trail.</param> /// <returns></returns> private static bool SatisfyEquals(Variable v1, Variable v2, Trail trail) { var d1 = (AllenDomain)v1.Domain; if (d1.Empty) { return(false); } var d3 = new AllenDomain(d1.Min(), d1.Max() + d1.Duration, d1.Duration, d1.Step); var d2 = (AllenDomain)v2.Domain; if (d2.Empty) { return(false); } for (int st1 = d1.Min(); st1 <= d1.Max(); st1 += d1.Step) { bool found = false; for (int st2 = d2.Min(); st2 <= d2.Max(); st2 += d2.Step) { int lt1 = st1 + d1.Duration; int lt2 = st2 + d2.Duration; if ((st1 == st2) && (lt1 == lt2)) { found = true; break; } } if (!found) { d3.Remove(st1); if (d3.Size() == 0) { return(false); } } } //v1.UpdateDomain(d3, trail); return(true); }
/// <summary> /// Satisfies the precedes. /// </summary> /// <param name="v1">The v1.</param> /// <param name="v2">The v2.</param> /// <returns></returns> private static bool SatisfyPrecedes(Variable v1, Variable v2) { var d1 = (AllenDomain)v1.Domain; if (d1.Empty) return false; var d3 = new AllenDomain(d1.Min(), d1.Max() + d1.Duration, d1.Duration, d1.Step); var d2 = (AllenDomain)v2.Domain; if (d2.Empty) return false; for (var st1 = d1.Min(); st1 <= d1.Max(); st1 += d1.Step) { var found = false; for (var st2 = d2.Min(); st2 <= d2.Max(); st2 += d2.Step) { var lt1 = st1 + d1.Duration; if (lt1 >= st2) continue; found = true; break; } if (found) continue; d3.Remove(st1); if (d3.Size() == 0) { return false; } } //v1.UpdateDomain(d3, trail); return true; }
/// <summary> /// Initializes a new instance of the <see cref="AllenDomainIterator"/> class. /// </summary> /// <param name="enclosingInstance">The enclosing instance.</param> public AllenDomainIterator(AllenDomain enclosingInstance) { InitBlock(enclosingInstance); }
//[DllImport("GTCSP.dll")] //public static extern int generateFile(int argc, string[] argv); //public static extern int getp(int argc); //[DllImport("msvcrt.dll")] //internal static extern int _flushall(); static void Main(string[] args) { //string[] sss = {"20", "0.5", "0.6", "0.7", "3", "3", "0.7", "0.02", "80", "67", "1", ">", "file.txt"}; //generateFile(13, sss); //return; //Console.Out.WriteLine(getp(9)); //Console.In.ReadLine(); //_flushall(); Network net = new Network(); // reading events and SOPO StreamReader re = File.OpenText("numfile.txt");//"Anum1.txt");//"fn1.txt"); //"numfile.txt"); string input = re.ReadLine(); string[] s = input.Split(' '); int n; try { n = Convert.ToInt16(s[1]); // no of events } catch { Console.WriteLine("Failed to read no of events!!"); return; } if (n==0) // no events { Console.WriteLine("No of events must be greater than 0 !!"); return; } var A = new AllenVariable[n]; for (var i=0; i<n;i++) { try { //input = ""; //while (input.Equals("")) input = re.ReadLine(); s = input.Split(' '); A[i] = new AllenVariable(net, Convert.ToInt16(s[1]), Convert.ToInt16(s[2]), Convert.ToInt16(s[3]), Convert.ToInt16(s[4]), "A" + i); } catch { Console.WriteLine("Error occured while reading file numfile.txt !!"); return; } } re.Close(); // READING REALTIONS re = File.OpenText("symbfile.txt");//"Asym1.txt");//"fs1.txt");//"symbfile.txt"); try { re.ReadLine(); // we don't need the first line input = re.ReadLine(); // this is the number of constraints int m = Convert.ToInt16(input); // m represents the number of constraints for (int i = 0; i < m; i++) { input = re.ReadLine(); s = input.Split(' '); int firstVar = Convert.ToInt16(s[0]); int secondVar = Convert.ToInt16(s[1]); string rel = s[2]; for (int j = 0; j < rel.Length; j++) { char r = rel[j]; switch (r) { case 'P': A[firstVar].Precedes(A[secondVar]); break; case 'p': A[firstVar].Precededby(A[secondVar]); break; case 'F': A[firstVar].Finishes(A[secondVar]); break; case 'f': A[firstVar].FinishedBy(A[secondVar]); break; case 'M': A[firstVar].Meets(A[secondVar]); break; case 'm': A[firstVar].MetBy(A[secondVar]); break; case 'S': A[firstVar].Starts(A[secondVar]); break; case 's': A[firstVar].StartedBy(A[secondVar]); break; case 'D': A[firstVar].During(A[secondVar]); break; case 'd': A[firstVar].Contains(A[secondVar]); break; case 'O': A[firstVar].Overlaps(A[secondVar]); break; case 'o': A[firstVar].OverlappedBy(A[secondVar]); break; case 'E': A[firstVar].Equals(A[secondVar]); break; default: Console.WriteLine("Error occured while reading relations"); return; } } } } catch { Console.WriteLine("Error occured while reading file symbfile.txt !!"); return; } /*AllenVariable[] A = new AllenVariable[3]; A[0] = new AllenVariable(net, 30, 60, 25, "M"); A[1] = new AllenVariable(net, 20, 60, 30, "L"); A[2] = new AllenVariable(net, 20, 46, 20, "J"); A[0].meets(A[1]); A[0].during(A[1]); A[0].overlaps(A[1]); A[0].starts(A[1]); A[0].precedes(A[1]); A[1].equals(A[2]); A[1].starts(A[2]); A[1].startedBy(A[2]); A[0].equals(A[2]); A[0].overlaps(A[2]); A[0].overlappedBy(A[2]); A[0].starts(A[2]); A[0].startedBy(A[2]); A[0].finishes(A[2]); A[0].finishedBy(A[2]); A[0].during(A[2]); A[0].contains(A[2]);*/ /*AllenVariable John = new AllenVariable(net, 0, 40, 30,1, "John"); AllenVariable Mary = new AllenVariable(net, 35, 60, 20, "Mary"); AllenVariable Wendy = new AllenVariable(net, 0, 60, 50, "Wendy"); AllenVariable Soccer = new AllenVariable(net, 30, 135, 105, "Soccer"); John.equals(Mary); John.starts(Mary); John.startedBy(Mary); John.meets(Mary); John.equals(Wendy); John.starts(Wendy); John.startedBy(Wendy); John.meets(Wendy); John.overlaps(Soccer); Mary.finishes(Wendy); //Mary.during(Wendy); Mary.finishedBy(Wendy); Mary.during(Soccer); Mary.contains(Soccer);*/ Solver solver = new AllenSolver(net); //solver.findFirst(); //solver.waitNext(); int c = 0; long timer = DateTime.Now.Ticks; StreamWriter wr = File.CreateText("Output.txt"); wr.WriteLine("Started at "+DateTime.Now); Console.WriteLine("Started at "+DateTime.Now); wr.WriteLine("================================"); Console.WriteLine("================================"); //st(); //Console.Out.WriteLine(Fibonacci(9)); //Console.Out.WriteLine(anotherway(4400)); //Console.In.ReadLine(); //return; for (solver.Start(); solver.WaitNext(); solver.Resume()) { Solution solution = solver.Solution; AllenDomain[] ad = new AllenDomain[n]; for (int k=0; k<n; k++) { ad[k] = (AllenDomain) (A[k].Domain); } int[] s0 = new int[n]; for (int k=0; k<n; k++) { s0[k] = solution.GetIntValue(A[k]) + ad[k].Duration; Console.Out.WriteLine(solution.GetIntValue(A[k]) + "-" + s0[k]); wr.WriteLine(solution.GetIntValue(A[k]) + "-" + s0[k]); } /*AllenDomain ad0 = (AllenDomain)(John.Domain); AllenDomain ad1 = (AllenDomain)(Mary.Domain); AllenDomain ad2 = (AllenDomain)(Wendy.Domain); AllenDomain ad3 = (AllenDomain)(Soccer.Domain); int s0 = solution.getIntValue(John) + ad0.Duration; int s1 = solution.getIntValue(Mary) + ad1.Duration; int s2 = solution.getIntValue(Wendy) + ad2.Duration; int s3 = solution.getIntValue(Soccer) + ad3.Duration; Console.Out.WriteLine(solution.getIntValue(John) + "-" + s0 + " John "); Console.Out.WriteLine(solution.getIntValue(Mary) + "-" + s1+" Mary"); Console.Out.WriteLine(solution.getIntValue(Wendy) + "-" + s2+" Wendy"); Console.Out.WriteLine(solution.getIntValue(Soccer) + "-" + s3+" Soccer");*/ c++; wr.WriteLine("C==" + c); wr.WriteLine("================================"); wr.WriteLine(DateTime.Now); Console.WriteLine(DateTime.Now); Console.WriteLine("C==" + c); Console.WriteLine("================================"); // if (c==1) break; } wr.WriteLine("Finished at " + DateTime.Now); Console.WriteLine("Finished at " + DateTime.Now); wr.WriteLine("================================"); Console.WriteLine("================================"); timer = DateTime.Now.Ticks - timer; Console.Out.WriteLine("timer: "+timer/10000/1000/60.0); wr.WriteLine("timer: " + timer / 10000); wr.Close(); //foreach (Constraint c1 in net.Constraints) //{ // Console.WriteLine(c1.ToString()); //} Console.WriteLine(c); solver.Stop(); Console.In.ReadLine(); /*CVarToVar[] df = new CVarToVar[net.Variables.Count]; IEnumerator va = net.Variables.GetEnumerator(); AllenVariable aa; while (va.MoveNext()) { aa = (AllenVariable) va.Current; df = aa.getConstraintsVars(); for (int l = 0; l < net.Variables.Count; l++) { if (df[l].ToString() != null) Console.WriteLine(df[l]); } Console.WriteLine(); } int [][][] h = NetWorkConstraints.getConstraintNetwork(net); int[][][] BB = AllenEvents.AllenComposition;*/ }
/// <summary> /// Inits the block. /// </summary> /// <param name="eI">The e I.</param> private void InitBlock(AllenDomain eI) { EnclosingInstance = eI; _mi = EnclosingInstance.minField; _choice = _mi; _ma = EnclosingInstance.maxField; _d = EnclosingInstance.Duration; _s = EnclosingInstance.step; }
/// <summary> /// Deletes the specified lo. /// </summary> /// <param name="lo">The lo.</param> /// <param name="hi">The hi.</param> /// <returns></returns> public virtual AllenDomain Delete(int lo, int hi) { if (sizeField == 0 || lo > hi || hi < minField || maxField < lo) return this; if (lo == hi) return Delete(lo); var d = new AllenDomain(); try { for (int i = 0; i < _intervals.Count; i++) { var interval = (int[])_intervals[i]; int mi = Math.Max(lo, interval[0]); int ma = Math.Min(hi, interval[1]); if (mi <= ma) { if (interval[0] < mi) { var inv = new int[2]; inv[0] = interval[0]; inv[1] = mi - 1; d._intervals.Add(inv); } if (ma < interval[1]) { var inv = new int[2]; inv[0] = ma + 1; inv[1] = interval[1]; d._intervals.Add(inv); } } else { var inv = new int[2]; inv[0] = interval[0]; inv[1] = interval[1]; d._intervals.Add(inv); } } } catch (IndexOutOfRangeException) { } d.UpdateSize(); d.UpdateMinMax(); return d; }
/// <summary> /// Clones this instance. /// </summary> /// <returns></returns> public override Object Clone() { var d = new AllenDomain(); try { for (int i = 0; i < _intervals.Count; ++i) { d._intervals.Add(((int[])_intervals[i]).Clone()); } } catch (IndexOutOfRangeException) { } d.sizeField = sizeField; d.minField = minField; d.maxField = maxField; d.step = step; d.duration = duration; return d; }
/// <summary> /// Caps the specified d. /// </summary> /// <param name="d">The d.</param> /// <returns></returns> public override Domain Cap(Domain d) { if (!(d is AllenDomain)) return EMPTY; var newD = new AllenDomain(); AllenDomain d0 = this; var d1 = (AllenDomain)d; try { int i0 = 0; int i1 = 0; while (i0 < d0._intervals.Count && i1 < d1._intervals.Count) { var interval = (int[])d0._intervals[i0]; int min0 = interval[0]; int max0 = interval[1]; interval = (int[])d1._intervals[i1]; int min1 = interval[0]; int max1 = interval[1]; if (max0 < min1) { i0++; continue; } if (max1 < min0) { i1++; continue; } interval = new int[2]; interval[0] = Math.Max(min0, min1); interval[1] = Math.Min(max0, max1); newD._intervals.Add(interval); if (max0 <= max1) i0++; if (max1 <= max0) i1++; } } catch (IndexOutOfRangeException) { } newD.UpdateSize(); newD.UpdateMinMax(); if (newD.Empty) return EMPTY; return newD; }