public async void GetInstanceIncludeParty_TC03_IncludePartyNotIcnluded()
        {
            int  instanceOwnerId = 123456;
            Guid instanceGuid    = Guid.NewGuid();

            _registerMock.Setup(r =>
                                r.GetParty(It.IsAny <int>()))
            .ReturnsAsync(Parties.Party1);

            _storageMock.Setup(s =>
                               s.GetInstance(It.IsAny <int>(), It.IsAny <Guid>()))
            .ReturnsAsync(Instances.Instance1);

            HttpClient client = GetTestClient(_registerMock, _storageMock, _profileMock);

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", GetUserToken(3));
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            string url = $"{BasePath}instances/{instanceOwnerId}/{instanceGuid}";

            HttpResponseMessage response = await client.GetAsync(url);

            ExtendedInstance actual = JsonConvert.DeserializeObject <ExtendedInstance>(await response.Content.ReadAsStringAsync());

            Assert.Equal(System.Net.HttpStatusCode.OK, response.StatusCode);
            Assert.Null(actual.Party);
            Assert.NotNull(actual.Instance);
            Assert.Equal("tdd/auth-level-3", actual.Instance.AppId);
        }
        public FeasibilityStatus Check(
            Instance instance,
            StartTimes startTimes,
            SolverConfig solverConfig,
            int?objective = null)
        {
            this.instance = ExtendedInstance.GetExtendedInstance(instance);
            this.instance.ComputeOptimalSwitchingCosts();    // For TEC computation.

            this.startTimes   = startTimes;
            this.solverConfig = solverConfig;

            this.Job     = null;
            this.NextJob = null;
            this.Machine = null;
            this.Status  = FeasibilityStatus.Unknown;

            var feasible =
                this.EveryJobHasStartTime() &&
                this.JobsWithinHorizon() &&
                this.JobsNotOverlapping() &&
                this.TransitionsExist() &&
                this.ObjectiveCorrespondsWithStartTimes(objective);

            if (feasible)
            {
                this.Status = FeasibilityStatus.Feasible;
            }

            return(this.Status);
        }
Esempio n. 3
0
        public async Task <ActionResult> GetInstanceIncludeParty(int instanceOwnerId, Guid instanceGuid, bool includeParty = false)
        {
            ExtendedInstance result = new ExtendedInstance();

            try
            {
                Instance instance = await _storage.GetInstance(instanceOwnerId, instanceGuid);

                result.Instance = instance;
            }
            catch (PlatformHttpException e)
            {
                return(HandlePlatformHttpException(e));
            }

            string partyId = result?.Instance?.InstanceOwner?.PartyId;

            if (includeParty && partyId != null && int.TryParse(partyId, out int partyIdInt))
            {
                try
                {
                    Party party = await _register.GetParty(partyIdInt);

                    result.Party = party;
                }
                catch (PlatformHttpException e)
                {
                    return(HandlePlatformHttpException(e));
                }
            }

            return(Ok(result));
        }
Esempio n. 4
0
        public static JsonInstance ToJsonInstance(Instance instance, bool serializeExtendedInstance = true)
        {
            var jsonJobs = instance.Jobs
                           .Select(job => new JsonJob
            {
                Id             = job.Id,
                MachineIdx     = job.MachineIdx,
                ProcessingTime = job.ProcessingTime
            })
                           .ToArray();

            byte[] serializedExtendedInstance = null;
            if (serializeExtendedInstance)
            {
                if (instance.SerializedExtendedInstance != null)
                {
                    serializedExtendedInstance = instance.SerializedExtendedInstance;
                }
                else
                {
                    var extendedInstance = ExtendedInstance.GetExtendedInstance(instance);
                    extendedInstance.Metadata = null;    // Due to metadata possibly not serializable.
                    extendedInstance.GenerateFullExtendedInstance();

                    var stream = new MemoryStream(2048);
                    using (stream)
                    {
                        var bf = new BinaryFormatter();
                        bf.Serialize(stream, extendedInstance);
                    }

                    serializedExtendedInstance = stream.ToArray();
                }
            }

            return(new JsonInstance
            {
                MachinesCount = instance.MachinesCount,
                Jobs = jsonJobs,
                EnergyCosts = instance.Intervals.Select(interval => interval.EnergyCost).ToArray(),
                LengthInterval = instance.LengthInterval,
                OffOnTime = instance.OffOnTime,
                OnOffTime = instance.OnOffTime,
                OffOnPowerConsumption = instance.OffOnPowerConsumption,
                OnOffPowerConsumption = instance.OnOffPowerConsumption,
                OffIdleTime = instance.OffIdleTime,
                IdleOffTime = instance.IdleOffTime,
                OffIdlePowerConsumption = instance.OffIdlePowerConsumption,
                IdleOffPowerConsumption = instance.IdleOffPowerConsumption,
                OnPowerConsumption = instance.OnPowerConsumption,
                IdlePowerConsumption = instance.IdlePowerConsumption,
                OffPowerConsumption = instance.OffPowerConsumption,
                SerializedExtendedInstance = serializedExtendedInstance,
                Metadata = instance.Metadata,
                TimeForExtendedInstance = instance.TimeForExtendedInstance
            });
        }
Esempio n. 5
0
        private static TimeSpan TimeForExtendedInstance(Instance instance)
        {
            var sw = new Stopwatch();

            sw.Restart();
            var extendedInstance = new ExtendedInstance(instance);

            extendedInstance.GenerateFullExtendedInstance();
            sw.Stop();
            return(sw.Elapsed);
        }
Esempio n. 6
0
        public void StartTimesTotalEnergyCostTheory(string instanceName, string resultName, int expectedTec)
        {
            var instance         = new InputReader().ReadFromPath(Path.Combine("instances", instanceName));
            var extendedInstance = ExtendedInstance.GetExtendedInstance(instance);

            extendedInstance.ComputeOptimalSwitchingCosts();
            extendedInstance.ComputeGapsLowerBounds();

            var result     = JsonConvert.DeserializeObject <Result>(File.ReadAllText(Path.Combine("results", resultName)));
            var startTimes = new StartTimes(instance, result.StartTimes);

            Assert.Equal(expectedTec, startTimes.TotalEnergyCost(extendedInstance));
        }
        public static int TotalEnergyCost(this StartTimes startTimes, ExtendedInstance instance)
        {
            int tec = 0;
            var orderedJobsOnMachines = startTimes.GetOrderedJobsOnMachines(instance);

            foreach (var machineIndex in instance.Machines)
            {
                var orderedJobs = orderedJobsOnMachines[machineIndex];

                if (!orderedJobs.Any())
                {
                    tec += instance.OptimalSwitchingCosts.First().Last().Value;
                    continue;
                }

                // Switching to first job.
                {
                    var startInterval = startTimes.StartInterval(orderedJobs.First(), instance);
                    tec += instance.OptimalSwitchingCosts.First()[startInterval.Index].Value;
                }

                // Switching between jobs.
                foreach (var(job, nextJob) in orderedJobs.SuccessionPairs())
                {
                    var completionInterval = startTimes.CompletionInterval(job, instance);
                    var startInterval      = startTimes.StartInterval(nextJob, instance);
                    tec += instance.OptimalSwitchingCosts[completionInterval.Index + 1][startInterval.Index].Value;
                }

                // Switching after last job.
                {
                    var completionInterval = startTimes.CompletionInterval(orderedJobs.Last(), instance);
                    tec += instance.OptimalSwitchingCosts[completionInterval.Index + 1].Last().Value;
                }

                // Costs of processing the jobs.
                foreach (var job in instance.Jobs)
                {
                    var startInterval      = startTimes.StartInterval(job, instance);
                    var completionInterval = startTimes.CompletionInterval(job, instance);
                    tec += instance.TotalEnergyCost(
                        startInterval.Index,
                        completionInterval.Index,
                        instance.OnPowerConsumption);
                }
            }

            return(tec);
        }
Esempio n. 8
0
        private static int Run(CmdOptions opts)
        {
            try
            {
                var config       = GetConfig(opts);
                var solverConfig = config.ToSolverConfig();

                var instance = GetInstance(opts);

                if (config.UseSerializedExtendedInstance)
                {
                    instance = ExtendedInstance.GetExtendedInstance(instance);
                }
                else
                {
                    instance.SerializedExtendedInstance = null;
                }

                CheckInstance(instance);

                var solverResult = Solve(config, solverConfig, instance);
                Console.WriteLine($"Running time: {solverResult.RunningTime}");
                if (solverResult.Status == Status.Heuristic || solverResult.Status == Status.Optimal)
                {
                    Console.WriteLine($"TEC from solver: {solverResult.Objective}");
                    Console.WriteLine($"Lower bound: {solverResult.LowerBound}");
                    Console.WriteLine($"Additional info: {JsonConvert.SerializeObject(solverResult.AdditionalInfo)}");
                }
                else if (solverResult.Status == Status.Infeasible)
                {
                    Console.WriteLine($"The instance is proven to be infeasible.");
                }
                else if (solverResult.Status == Status.NoSolution)
                {
                    Console.WriteLine($"No solution was found.");
                }

                return(0);
            }
            catch (FileNotFoundException ex)
            {
                Console.WriteLine(ex.Message);
                return(1);
            }
        }
 public ShortestPathAlgorithmCostEfficientSwitchings(ExtendedInstance instance)
 {
     this.instance                    = instance;
     this.totalProcessingTime         = instance.Jobs.Sum(job => job.ProcessingTime);
     this.jobsOrderedByProcessingTime = instance.Jobs.OrderBy(job => job.ProcessingTime).ToList();
 }
Esempio n. 10
0
 public void WriteToPath(ExtendedInstance instance, string instancePath)
 {
     this.WriteToPath((Instance)instance, instancePath);
 }
Esempio n. 11
0
 protected virtual void SetInstance(Instance instance)
 {
     this.Instance = ExtendedInstance.GetExtendedInstance(instance);
 }