Esempio n. 1
0
        public void ChooseTruckCandidateWithLowerAccidentProbability()
        {
            // Estimation on bad weather condition
            const bool goodWeather = false;

            DemoLogger.InfLog("Create fleet ...");
            _fleet = DemoFleetDataFactory.CreateRandomFleet(3);

            PopulateWithEvents();

            // Get results
            DemoLogger.InfLog("Estimate the probability to have an accident:");

            var rank = new Dictionary <Guid, double>();

            _fleet.ForEach(truck =>
            {
                // Trigger internal model updating
                truck.UpdateStats();

                var prediction = truck.PredictAccident(goodWeather);
                var prob       = prediction.Probabilities.ToList();

                DemoLogger.InfLog($"Truck {truck.ModelCode}: {prob[0].Label}-> {prob[0].ProbabilityScore:P}, " +
                                  $"{prob[1].Label}-> {prob[1].ProbabilityScore:P}");

                rank.Add(truck.Id, prob[1].ProbabilityScore);
            });

            var candidateId = rank.OrderBy(c => c.Value).First().Key;
            var candidate   = _fleet.Find(f => f.Id.Equals(candidateId));

            DemoLogger.InfLog($"Best truck: {candidate}");
        }
Esempio n. 2
0
        public void OptimizeAndAssignTrips()
        {
            DemoLogger.InfLog("Create fleet ...");
            _fleet = DemoFleetDataFactory.CreateRandomFleet(6);

            // Create Csp data problem from current fleet status
            var cspData = FleetCsp.FleetToCspModel(DemoFleetDataFactory.CreateCspProblemVariables().ToList(), _fleet);

            // Instantiate Csp-Model
            var fleetCsp = CspFactory.Create(
                new Dictionary <string, IEnumerable <TruckCspValue> >(cspData.Select(v => new KeyValuePair <string, IEnumerable <TruckCspValue> >(v.Key, v.Value.Domains))),
                new Dictionary <string, IEnumerable <string> >(cspData.Select(v => new KeyValuePair <string, IEnumerable <string> >(v.Key, v.Value.Relations))),
                new Func <string, TruckCspValue, string, TruckCspValue, bool>[]
            {
                // Constraint: Do not overlap on the same day
                (variableA, transportTruckA, variableB, transportTruckB) =>
                {
                    var dayA = variableA.Split('.').First();
                    var dayB = variableB.Split('.').First();
                    return(dayA != dayB || transportTruckA != transportTruckB);
                },
                // Avoid commitment on Monday and Tuesday
                (variableA, transportTruckA, variableB, transportTruckB) =>
                {
                    var dayA = variableA.Split('.').First();
                    var dayB = variableB.Split('.').First();
                    if (
                        string.Compare(dayA, "mon", StringComparison.OrdinalIgnoreCase) == 0 &&
                        string.Compare(dayB, "tue", StringComparison.OrdinalIgnoreCase) == 0 ||
                        string.Compare(dayA, "tue", StringComparison.OrdinalIgnoreCase) == 0 &&
                        string.Compare(dayB, "mon", StringComparison.OrdinalIgnoreCase) == 0
                        )
                    {
                        return(transportTruckA != transportTruckB);
                    }

                    return(true);
                }
            });

            // Resolve the given problem
            var solved = fleetCsp.UseBackTrackingSearchResolver(
                SelectUnassignedVariableStrategyTypes <TruckCspValue> .FirstUnassignedVariable,
                DomainValuesOrderingStrategyTypes <TruckCspValue> .DomainCustomOrder)
                         .Resolve(() =>
            {
                PrintPlan(fleetCsp);
            });

            Assert.True(solved);
        }