using System; using System.Collections.Generic; using System.Linq; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Encodings.IntegerVectorEncoding; using HeuristicLab.Optimization; using HeuristicLab.Problems.Scheduling.MRCPSP; using Microsoft.VisualStudio.TestTools.UnitTesting; using SimSharp; using Environment = SimSharp.Environment; namespace UnitTestProject { [TestClass] public class UnitTest { [TestMethod] public void Import() { var problem = MultiModeFileImporter.Import(@"C:\Users\P41826\FH OÖ FileBox\MRCPSP\MRCPSP_BenchmarkData\mmlib 50\mmlib50.mm\J501_1.mm"); } [TestMethod] public void TestMethod1() { Console.Write(Evaluate()); } public ItemList Activities { get; set; } = new ItemList(); public ItemList ResourceCapacities { get; set; } = new ItemList(); public UnitTest() { var RR = new ResourceCapacity { Number = 1, Capacity = 7, IsRenewable = true }; var RN = new ResourceCapacity { Number = 2, Capacity = 23, IsRenewable = false }; ResourceCapacities.Add(RR); ResourceCapacities.Add(RN); var A = new List> { Tuple.Create(1, 1, 4, 3, 3), Tuple.Create(1, 2, 5, 2, 4), Tuple.Create(2, 1, 1, 3, 4), Tuple.Create(2, 2, 2, 2, 3), Tuple.Create(3, 1, 1, 2, 3), Tuple.Create(3, 2, 2, 1, 1), Tuple.Create(4, 1, 2, 5, 4), Tuple.Create(4, 2, 3, 4, 3), Tuple.Create(5, 1, 2, 4, 6), Tuple.Create(5, 2, 5, 3, 2), Tuple.Create(6, 1, 1, 1, 4), Tuple.Create(6, 2, 3, 1, 3), Tuple.Create(7, 1, 1, 3, 3), Tuple.Create(7, 2, 3, 2, 2), Tuple.Create(8, 1, 2, 3, 4), Tuple.Create(8, 2, 2, 3, 3) }; var S = new Dictionary> { {0, new List {1, 2, 3}}, {1, new List {7}}, {2, new List {4, 5}}, {3, new List {9}}, {4, new List {6}}, {5, new List {9}}, {6, new List {7, 8}}, {7, new List {9}}, {8, new List {9}}, {9, new List()} }; Activities = new ItemList(Enumerable.Range(0, 10).Select(i => new Activity { Number = i }).ToList()); Activities[0].Modes.Add(new Mode { Number = 1 }); foreach (var a in A) { Activities[a.Item1].Modes.Add(new Mode { Number = a.Item2, Duration = a.Item3, ResourceDemands = new ItemList { new ResourceDemand {Number = 1, Demand = a.Item4}, new ResourceDemand {Number = 2, Demand = a.Item5} } }); } Activities[9].Modes.Add(new Mode { Number = 1 }); foreach (var s in S) { Activities[s.Key].Successors = new ItemList(s.Value.Select(x => new IntValue(x))); } } public MultiEncoding Encoding { get; set; } public double Evaluate() { var randomKey = new IntegerVector(new[] { 1, 2, 3, 4, 5, 6, 7, 8 }); var modeList = new IntegerVector(new[] { 1, 1, 2, 2, 2, 1, 1, 2 }); var activities = new List>(Activities.Count); var dependencies = new Dictionary>(Activities.Count); activities.Add( /*[Activities[0].Number] =*/ Tuple.Create(Activities[0], int.MinValue, 1)); dependencies[Activities[0].Number] = new List(); for (var i = 1; i < Activities.Count - 1; i++) { activities.Add( /*[Activities[i].Number] =*/ Tuple.Create(Activities[i], randomKey[i - 1], modeList[i - 1])); dependencies[Activities[i].Number] = new List(); } activities.Add( /*[Activities[Activities.Count - 1].Number] =*/ Tuple.Create(Activities[Activities[Activities.Count - 1].Number], int.MaxValue, 1)); dependencies[Activities[Activities.Count - 1].Number] = new List(); foreach (var activity in Activities) { foreach (var successor in activity.Successors) { dependencies[successor.Value].Add(activity.Number); } } activities = activities.OrderBy(a => a.Item2).ToList(); var env = new Environment(); var procActivities = new Dictionary(); var resources = new Dictionary(); foreach (var resource in ResourceCapacities.Where(r => r.IsRenewable)) { resources.Add(resource.Number, new Container(env, resource.Capacity, resource.Capacity)); } var nonrenRes = new Dictionary(); foreach (var resource in ResourceCapacities.Where(r => !r.IsRenewable)) { nonrenRes.Add(resource.Number, new Container(env, resource.Capacity, resource.Capacity)); } foreach (var act in activities) { procActivities[act.Item1.Number] = env.Process(Job(env, procActivities, act, resources, nonrenRes, dependencies[act.Item1.Number])); } // fitness function by Hartmann (2001) try { env.Run(); } catch (InvalidOperationException) { var T = Activities.Sum(a => a.Modes.Max(m => m.ResourceDemands.Where(d => ResourceCapacities.Single(c => c.Number == d.Number).IsRenewable) .Sum(d => d.Demand))); var ERR = activities.Sum(a => a.Item1.Modes[a.Item3 - 1].ResourceDemands .Where(d => !ResourceCapacities.Single(c => c.Number == d.Number).IsRenewable).Sum(r => r.Demand)); return T + ERR; } return env.NowD; } private bool IsRenewable(int resourceNumber) => ResourceCapacities.Single(r => r.Number == resourceNumber).IsRenewable; private IEnumerable Job(Environment env, Dictionary procActivities, Tuple act, Dictionary resources, Dictionary nonrenRes, IEnumerable deps) { var mode = act.Item1.Modes[act.Item3 - 1]; var timeout = mode.Duration; var demands = mode.ResourceDemands.Where(r => r.Demand > 0); var res = demands.Where(r => IsRenewable(r.Number)).Select(r => Tuple.Create(resources[r.Number], r.Demand, r.Number)); var nres = demands.Where(r => !IsRenewable(r.Number)).Select(r => Tuple.Create(nonrenRes[r.Number], r.Demand, r.Number)); yield return new AllOf(env, deps.Select(d => procActivities[d])); //Console.WriteLine(act.Item1.Number + " B " + env.NowD); foreach (var nr in nres) { if (nr.Item2 > nonrenRes[nr.Item3].Level) throw new InvalidOperationException(); yield return nonrenRes[nr.Item3].Get(nr.Item2); } foreach (var r in res) { yield return r.Item1.Get(r.Item2); yield return env.TimeoutD(timeout); yield return r.Item1.Put(r.Item2); //for (var t = 0; t < r.Item2; t++) { // using (var req = r.Item1.Request()) { // yield return req; // yield return env.TimeoutD(timeout); // } //} //Console.WriteLine(act.Item1.Number + " E " + env.NowD); } } } }