Skip to content

Commit

Permalink
Handle quantity <= 0
Browse files Browse the repository at this point in the history
  • Loading branch information
wvpm committed Jan 26, 2025
1 parent 6701135 commit 954c448
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/openvic-simulation/economy/GoodInstance.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "GoodInstance.hpp"
#include "types/fixed_point/FixedPoint.hpp"

using namespace OpenVic;

Expand Down Expand Up @@ -61,6 +60,8 @@ void GoodInstance::add_market_sell_order(GoodMarketSellOrder&& market_sell_order
}

void GoodInstance::execute_orders() {
//MarketInstance ensured only orders with quantity > 0 are added.
//So running total > 0 unless orders are empty.
fixed_point_t demand_running_total = fixed_point_t::_0();
for (GoodBuyUpToOrder const& buy_up_to_order : buy_up_to_orders) {
demand_running_total += buy_up_to_order.get_max_quantity();
Expand Down
24 changes: 13 additions & 11 deletions src/openvic-simulation/economy/production/ArtisanalProducer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,19 @@ void ArtisanalProducer::artisan_tick(Pop& pop) {
fixed_point_t& good_stockpile = stockpile[input_good_ptr];
const fixed_point_t optimal_quantity = good_demand * max_possible_satisfaction_numerator / max_possible_satisfaction_denominator;
const fixed_point_t max_quantity_to_buy = good_demand - good_stockpile;
const fixed_point_t money_to_spend = optimal_quantity * max_price;
pop.add_artisan_inputs_expense(money_to_spend);
market_instance.place_buy_up_to_order({
*input_good_ptr,
max_quantity_to_buy,
money_to_spend,
[this, &pop, &good_stockpile](const BuyResult buy_result) -> void {
pop.add_artisan_inputs_expense(-buy_result.get_money_left());
good_stockpile += buy_result.get_quantity_bought();
}
});
if (max_quantity_to_buy > 0) {
const fixed_point_t money_to_spend = optimal_quantity * max_price;
pop.add_artisan_inputs_expense(money_to_spend);
market_instance.place_buy_up_to_order({
*input_good_ptr,
max_quantity_to_buy,
money_to_spend,
[this, &pop, &good_stockpile](const BuyResult buy_result) -> void {
pop.add_artisan_inputs_expense(-buy_result.get_money_left());
good_stockpile += buy_result.get_quantity_bought();
}
});
}
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions src/openvic-simulation/economy/trading/MarketInstance.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "MarketInstance.hpp"

#include "openvic-simulation/utility/CompilerFeatureTesting.hpp"
#include "openvic-simulation/utility/Utility.hpp"

using namespace OpenVic;

Expand All @@ -9,11 +10,27 @@ MarketInstance::MarketInstance(GoodInstanceManager& new_good_instance_manager)

void MarketInstance::place_buy_up_to_order(BuyUpToOrder&& buy_up_to_order) {
GoodDefinition const& good = buy_up_to_order.get_good();
if (OV_unlikely(buy_up_to_order.get_max_quantity() <= 0)) {
Logger::error("Received BuyUpToOrder for ",good," with max quantity ",buy_up_to_order.get_max_quantity());
buy_up_to_order.get_after_trade()({
fixed_point_t::_0(), buy_up_to_order.get_money_to_spend()
});
return;
}

GoodInstance& good_instance = good_instance_manager.get_good_instance_from_definition(good);
good_instance.add_buy_up_to_order(std::move(buy_up_to_order));
}
void MarketInstance::place_market_sell_order(MarketSellOrder&& market_sell_order) {
GoodDefinition const& good = market_sell_order.get_good();
if (OV_unlikely(market_sell_order.get_quantity() <= 0)) {
Logger::error("Received MarketSellOrder for ",good," with quantity ",market_sell_order.get_quantity());
market_sell_order.get_after_trade()({
fixed_point_t::_0(), fixed_point_t::_0()
});
return;
}

GoodInstance& good_instance = good_instance_manager.get_good_instance_from_definition(good);
good_instance.add_market_sell_order(std::move(market_sell_order));
}
Expand Down

0 comments on commit 954c448

Please sign in to comment.