@@ -74,12 +74,14 @@ pub fn profitability_index(
7474}
7575
7676/// Calculates annual LCOX based on capacity and activity.
77+ ///
78+ /// If the total activity is zero, then it returns `None`, otherwise `Some` LCOX value.
7779pub fn lcox (
7880 capacity : Capacity ,
7981 annual_fixed_cost : MoneyPerCapacity ,
8082 activity : & IndexMap < TimeSliceID , Activity > ,
8183 activity_costs : & IndexMap < TimeSliceID , MoneyPerActivity > ,
82- ) -> MoneyPerActivity {
84+ ) -> Option < MoneyPerActivity > {
8385 // Calculate the annualised fixed costs
8486 let annualised_fixed_cost = annual_fixed_cost * capacity;
8587
@@ -92,7 +94,8 @@ pub fn lcox(
9294 total_activity_costs += activity_cost * * activity;
9395 }
9496
95- ( annualised_fixed_cost + total_activity_costs) / total_activity
97+ ( total_activity > Activity ( 0.0 ) )
98+ . then ( || ( annualised_fixed_cost + total_activity_costs) / total_activity)
9699}
97100
98101#[ cfg( test) ]
@@ -223,20 +226,26 @@ mod tests {
223226 100.0 , 50.0 ,
224227 vec![ ( "winter" , "day" , 10.0 ) , ( "summer" , "night" , 20.0 ) ] ,
225228 vec![ ( "winter" , "day" , 5.0 ) , ( "summer" , "night" , 3.0 ) ] ,
226- 170.33333333333334 // (100*50 + 10*5 + 20*3) / (10+20) = 5110/30
229+ Some ( 170.33333333333334 ) // (100*50 + 10*5 + 20*3) / (10+20) = 5110/30
227230 ) ]
228231 #[ case(
229232 50.0 , 100.0 ,
230233 vec![ ( "winter" , "day" , 25.0 ) ] ,
231234 vec![ ( "winter" , "day" , 0.0 ) ] ,
232- 200.0 // (50*100 + 25*0) / 25 = 5000/25
235+ Some ( 200.0 ) // (50*100 + 25*0) / 25 = 5000/25
236+ ) ]
237+ #[ case(
238+ 50.0 , 100.0 ,
239+ vec![ ( "winter" , "day" , 0.0 ) ] ,
240+ vec![ ( "winter" , "day" , 0.0 ) ] ,
241+ None // (50*0 + 25*0) / 0 = not feasible
233242 ) ]
234243 fn lcox_works (
235244 #[ case] capacity : f64 ,
236245 #[ case] annual_fixed_cost : f64 ,
237246 #[ case] activity_data : Vec < ( & str , & str , f64 ) > ,
238247 #[ case] cost_data : Vec < ( & str , & str , f64 ) > ,
239- #[ case] expected : f64 ,
248+ #[ case] expected : Option < f64 > ,
240249 ) {
241250 let activity = activity_data
242251 . into_iter ( )
@@ -271,7 +280,7 @@ mod tests {
271280 & activity_costs,
272281 ) ;
273282
274- let expected = MoneyPerActivity ( expected) ;
275- assert_approx_eq ! ( MoneyPerActivity , result, expected) ;
283+ let expected = expected. map ( MoneyPerActivity ) ;
284+ assert_approx_eq ! ( Option < MoneyPerActivity > , result, expected) ;
276285 }
277286}
0 commit comments