چرا در این کد هیچ خریدی اعمال نمیشود؟\nدرحالی که خانه ها میفروشند\nاصلاحش کن for hour =…
- User
چرا در این کد هیچ خریدی اعمال نمیشود؟\nدرحالی که خانه ها میفروشند\nاصلاحش کن
for hour = 1:24solarProd = calculateSolarProduction(hour); windProd = calculateWindProduction(hour); totalProduction = solarProd + windProd; currentBatteryLevel = batteryLevels(i, hour); netEnergy = totalProduction - initialUsageLevels(hour);
% First charge the battery if needed if netEnergy > 0 if currentBatteryLevel < BatteryCapacity chargeEnergy = min(netEnergy, BatteryCapacity - currentBatteryLevel); batteryLevels(i, hour) = currentBatteryLevel + chargeEnergy; netEnergy = netEnergy - chargeEnergy; end
% Sell remaining excess energy to other homes if netEnergy > 0 homeNeeds = initialUsageLevels - batteryLevels(:, hour); [~, buyerIdx] = sort(homeNeeds, 'descend');
for j = 1:numHomes if homeNeeds(buyerIdx(j)) > 0 && i ~= buyerIdx(j) sellEnergy(i, hour) = min(netEnergy, homeNeeds(buyerIdx(j))); batteryLevels(i, hour) = batteryLevels(i, hour) - sellEnergy(i, hour); sellPrice(i, hour) = electricityPrices{i}(hour); sellHour{i} = [sellHour{i} hour]; totalSellRevenue(i) = totalSellRevenue(i) + sellEnergy(i, hour) * sellPrice(i, hour); buyingStatus(buyerIdx(j), hour) = 0; netEnergy = netEnergy - sellEnergy(i, hour); totalSoldToHomes(i, hour) = totalSoldToHomes(i, hour) + sellEnergy(i, hour); if netEnergy <= 0 break; end end end end else % For needed energy, discharge the battery first neededEnergy = -netEnergy; if currentBatteryLevel >= neededEnergy batteryLevels(i, hour) = currentBatteryLevel - neededEnergy; else neededEnergy = neededEnergy - currentBatteryLevel; batteryLevels(i, hour) = 0;
% Buy energy from other homes homeExcess = batteryLevels(:, hour) - initialUsageLevels; [~, sellerIdx] = sort(homeExcess, 'descend');
for j = 1:numHomes if homeExcess(sellerIdx(j)) > 0 && i ~= sellerIdx(j) buyEnergy(i, hour) = min(neededEnergy, homeExcess(sellerIdx(j))); batteryLevels(i, hour) = batteryLevels(i, hour) + buyEnergy(i, hour); buyPrice(i, hour) = electricityPrices{sellerIdx(j)}(hour); buyHour{i} = [buyHour{i} hour]; totalBuyCost(i) = totalBuyCost(i) + buyEnergy(i, hour) * buyPrice(i, hour); buyingStatus(sellerIdx(j), hour) = 0; neededEnergy = neededEnergy - buyEnergy(i, hour); totalBoughtFromHomes(i, hour) = totalBoughtFromHomes(i, hour) + buyEnergy(i, hour); totalSoldToHomes(sellerIdx(j), hour) = totalSoldToHomes(sellerIdx(j), hour) + buyEnergy(i, hour);
if neededEnergy <= 0 break; end end end end end
% Update battery levels for the next hour if hour < 24 batteryLevels(i, hour + 1) = batteryLevels(i, hour); end
% Update buyingStatus after each hour for j = 1:numHomes if i ~= j && (sellEnergy(i, hour) > 0 || buyEnergy(i, hour) > 0) buyingStatus(j, hour) = 1; end end end چرا در این کد هیچ خریدی اعمال نمیشود؟ درحالی که خانه ها میفروشند اصلاحش کن
- Assistant
- User
% Calculate excess production for each home for hour = 1:24 solarProduction = calculateSolarProduction(hour); windProduction = calculateWindProduction(hour); totalProduction = solarProduction + windProduction; usageLevel = initialUsageLevels(hour);
% Calculate excess production excessProduction(hour) = max(0, totalProduction - usageLevel);
% Perform energy transactions between homes for j = 1:numHomes if i ~= j for hour = 1:24 batteryLevelHomeJ = batteryLevels(j, hour); usageLevelHomeJ = initialUsageLevels(hour);
% Check if home j needs energy if usageLevelHomeJ > batteryLevelHomeJ && buyingStatus(j, hour) == 0 && buyingStatus(i, hour) == 0 % Calculate how much home i can sell saleAmount = min([excessProduction(hour), batteryLevels(i, hour), usageLevelHomeJ - batteryLevelHomeJ]);
- Assistant