Minizinc建模遇"Gecode: Float::linear: Number out of limits"错误求助
Let's figure out why you're hitting this error and how to fix it. The main issue here is a combination of an invalid initial state in your model and missing constraints that are tripping up the Gecode solver.
1. Immediate Culprit: Initial Battery State Breaks Storage Limits
First, look at your input data:
start_battery_state = 8.0; battery_max_storage = 7.0;
Your model sets battery_state[1] = start_battery_state but doesn't apply the max/min storage constraints to the first time interval—your current constraints only check TIME2 = 2..Num_intervals. This means the initial battery state is already 1 unit over the maximum allowed storage, creating an impossible constraint. Gecode's linear constraint solver hits a numerical limit trying to resolve this contradiction.
2. Additional Model Gaps That Exacerbate the Problem
- Missing bounds on
battery_statefor all time steps: Even if you fix the initial state, your constraints only enforce storage limits starting at t=2. You need to cover the first interval too. - Unused charge/discharge rate parameters: You defined
battery_max_chargeandbattery_max_dischargebut never used them to constrainbattery_activity. Right now, the activity variable can hit ±4.0 regardless of these parameters (they match in your input, but it's a fragile gap). - Potential edge cases with negative prices: Your input includes a -5.0 price, which could lead to unexpectedly large revenue values if not paired with proper constraints, though this is secondary to the initial state issue.
Fixed Model Code
Here's the corrected version of your model with all these issues addressed:
%enumerators enum ACTION = {charging, discharging, no_action}; %paramters int: Num_intervals; float: start_battery_state; float: battery_max_charge; float: battery_max_discharge; float: battery_max_storage; float: battery_min_storage; array[int] of float: charge_prices; array[int] of float: discharge_prices; %sets set of int: TIME = 1..Num_intervals; set of int: TIME2 = 2..Num_intervals; %variables array[TIME] of var -4.0..4.0: battery_activity; array[TIME] of var float: battery_state; array[TIME] of var ACTION: choice; array[TIME] of var float: revenue_generated; var float: total_revenue = sum(t in TIME)(revenue_generated[t]); %constraints - apply storage limits to ALL time steps constraint battery_state[1] = start_battery_state; constraint battery_state[1] >= battery_min_storage; constraint battery_state[1] <= battery_max_storage; constraint forall(t in TIME2)(battery_state[t] = battery_state[t-1] + battery_activity[t]); constraint forall(t in TIME2)(battery_state[t] >= battery_min_storage); constraint forall(t in TIME2)(battery_state[t] <= battery_max_storage); %Enforce charge/discharge rate limits using your defined parameters constraint forall(t in TIME)( if choice[t] = charging then battery_activity[t] <= battery_max_charge elseif choice[t] = discharging then battery_activity[t] >= battery_max_discharge else battery_activity[t] = 0 endif ); %Calculate revenue based on action taken constraint forall(t in TIME)( if choice[t] = charging then revenue_generated[t] = -battery_activity[t] * charge_prices[t] elseif choice[t] = discharging then revenue_generated[t] = battery_activity[t] * discharge_prices[t] else revenue_generated[t] = 0 endif ); %Link battery activity to action choice constraint forall(t in TIME)( if battery_activity[t] > 0 then choice[t] = charging elseif battery_activity[t] < 0 then choice[t] = discharging else choice[t] = no_action endif ); solve maximize total_revenue;
Corrected Input (Fix Initial State)
You'll also need to adjust your input to make the initial state valid:
% number of time intervals Num_intervals = 5; start_battery_state = 7.0; % Changed from 8.0 to match max storage battery_max_charge = 4.0; battery_max_discharge = -4.0; battery_min_storage = 0.0; battery_max_storage = 7.0; %charge_efficiency = 0.85; %discharge_efficiency = 1.0; charge_prices = [35.0,20.0,60.0,-5.0,20.0]; discharge_prices = [35.0,20.0,60.0,-5.0,20.0];
Why This Fixes the Error
By ensuring the initial battery state adheres to your storage limits, you remove the impossible constraint that was causing Gecode's linear solver to hit numerical limits. Adding the rate limit constraints also prevents any unexpected extreme values in battery_activity that could trigger similar issues down the line.
内容的提问来源于stack exchange,提问作者Doptima

