public static Triplet <double, double, double> OptimumTrade(EconomicAttributes G0L0, EconomicAttributes G0L1, EconomicAttributes G1L0, EconomicAttributes G1L1, EconomicAttributes Labor, double P0, double P1) { double MaxG0L0 = G0L0.Supply - G0L0.Demand; double MaxG0L1 = G0L1.MaxNewProduction(P1); double MinG0L1 = G0L1.Demand - G0L1.Supply; double MaxG1L0 = G1L0.MaxNewProduction(P0); double MaxG1L1 = G1L1.Supply - G1L1.Demand; double MinG1L0 = G1L0.Demand - G1L0.Supply; double Max = Math.Min(MaxG0L0, MaxG1L1); double Min = Math.Max(MinG0L1, 0); if (Max <= Min) { return(new Triplet <double, double, double>(Double.NaN, Double.NaN, 0)); } double X = Min + 1; double Xi = 0; for (int i = 0; i < 20; ++i) { double C = (1 - G0L1.IncomeReduction) * X * G0L1.SupplyPrice(X, P1); Xi = OptimumTradeAux(C, G1L1, P1); X = X - (DProduction(Xi, G1L0.LivingStandard, G1L0.Supply, G1L0.Demand, 1, G1L0.Exponent, G1L0.Decay * G1L0.Coefficient * P0) + DProduction(X, (1 - G0L0.IncomeReduction) * G0L0.LivingStandard, G0L0.Supply, G0L0.Demand, -1, G0L0.Exponent, G0L0.Decay * G0L0.Coefficient * P1) + DProduction(Math.Max(Xi, X), Labor.LivingStandard, Labor.Supply, Labor.Demand, -.001, Labor.Exponent, Labor.Decay * Labor.Coefficient * P0)) / (DDProduction(Xi, G1L0.LivingStandard, G1L0.Supply, G1L0.Demand, 1, G1L0.Exponent, G1L0.Decay * G1L0.Coefficient * P0) + DDProduction(X, (1 - G0L0.IncomeReduction) * G0L0.LivingStandard, G0L0.Supply, G0L0.Demand, -1, G0L0.Exponent, G0L0.Decay * G0L0.Coefficient * P1) + DDProduction(Math.Max(Xi, X), Labor.LivingStandard, Labor.Supply, Labor.Demand, -.001, Labor.Exponent, Labor.Decay * Labor.Coefficient * P0)); } double Co = X * G0L1.SupplyPrice(X, P1); Xi = OptimumTradeAux(Co, G1L1, P1); double P = Xi * G1L0.SupplyPrice(Xi, P0) - X * G0L0.SupplyPrice(-X, P0) - Math.Max(X, Xi) * Labor.SupplyPrice(-Math.Max(X, Xi), P0); return(new Triplet <double, double, double>(X, Xi, P)); }
static double OptimumTradeAux(double Goal, EconomicAttributes Good, double Population) { if (Double.IsInfinity(Goal) || Double.IsNaN(Goal)) { return(0); } double Max = Good.Supply - Good.Demand; if (Max < 0) { return(0); } double X = Max - .000001; for (int i = 0; i < 20; ++i) { if (X < 0) { X = .000001; } /* * Console.WriteLine(X); * Console.WriteLine(X * Good.SupplyPrice(-X, Population) - Goal); * Console.WriteLine(Good.SupplyPrice(-X, Population)); * Console.WriteLine(Good); * Console.WriteLine(DProduction(X, Good.LivingStandard, Good.Supply, Good.Demand, -1, Good.Exponent, Population * Good.Coefficient)); */ X = X + (X * Good.SupplyPrice(-X, Population) - Goal) / DProduction(X, Good.LivingStandard, Good.Supply, Good.Demand, -1, Good.Exponent, Population * Good.Coefficient * Good.Decay); } return(X); }