/// <summary> /// Runs maintenance on a number of products. /// </summary> /// <param name="products">The number of products we are maintaining.</param> /// <param name="MaintenanceProducts">The products being used to maintain the products.</param> /// <param name="Satisfaction">How much was satisfied.</param> /// <returns>The items consumed.</returns> public IProductAmountCollection RunMaintenance(double products, IReadOnlyProductAmountCollection MaintenanceProducts, out double Satisfaction) { // null check if (MaintenanceProducts is null) { throw new ArgumentNullException(nameof(MaintenanceProducts)); } // if Maintenance is empty, return empty collection, and set satisfaction to 1. if (Maintenance.Count() == 0) { Satisfaction = 1; return(new ProductAmountCollection()); } // Get from all available maintenance goods, the exact goods we're looking for. var availableGoods = MaintenanceProducts.GetProducts(Maintenance.Products.ToList()); // Get how many maintenance products are actually needed. var requiredGoods = Maintenance.Multiply(products); // Start the cumulative satisfaction. var cumulativeSat = 0.0; // Create our return collection var result = new ProductAmountCollection(); // go through the available goods. foreach (var pair in availableGoods) { // for the product, available amount, and required amount var product = pair.Item1; var available = pair.Item2; var required = requiredGoods.GetProductValue(product); // get how well the good was satisfied var sat = Math.Min(1, available / required); // to the cumulative satisfaction, multiplying by the requried amount to weight it correctly. cumulativeSat += sat * required; // and add how much was consumed, choosing either available or required amount. if (sat == 1) { result.AddProducts(product, required); } else { result.AddProducts(product, available); } } // average out the cumulative satisfaction by all of the required goods. // if all are fully satisfied, then Satisfaction should be 1. // Products which have higher requirements should have a larger impact on the average. Satisfaction = cumulativeSat / requiredGoods.Sum(x => x.Item2); // return our result. return(result); }
private void AssertCollectionContains(IReadOnlyProductAmountCollection result, Mock <IPlot> plot, double val) { Assert.That(result.GetProductValue(plot.Object), Is.EqualTo(val)); }
private void AssertCollectionContains(IReadOnlyProductAmountCollection col, Mock <IProduct> product, double amount) { Assert.That(col.GetProductValue(product.Object), Is.EqualTo(amount)); }