Solved

# my small program give a wrong result

• 2 replies
• 85 views

• Newcomer
• 3 replies

Dear all

My mathematical problem is to schedule the device in a home to achieve the minimum cost during a day. I divide a day into 24 time slots.  Each device with have its own Power rate and length of operation time. Main variables in my problem are starting time of devices at which a device is started to run. When I run my program, starting times of devices are always at time slot 1 and these are wrong results. I do not know why my program give wrong results.

Here is my program

Model Main_Test_starting_time {
Set Time {
Index: t;
}
Set Devices {
Index: d;
}
Parameter Price {
IndexDomain: t;
}
Parameter Power_rate {
IndexDomain: d;
}
Parameter Length_of_operation_time {
IndexDomain: d;
}
Variable Starting_time {
IndexDomain: d;
Range: {
{1..inf}
}
}
Variable Energy_consumtion {
IndexDomain: t;
Range: nonnegative;
Definition: sum[d, if Ord(t) >= Starting_time(d) AND Ord(t) <= Starting_time(d) + Length_of_operation_time(d) - 1 then Power_rate(d) else 0 endif];
}
Variable Total_cost {
Range: free;
Definition: sum[t, Energy_consumtion(t) * Price(t)];
}
Constraint Contraint {
IndexDomain: d;
Definition: Starting_time(d) <= Card(Time) - Length_of_operation_time(d) - 1;
}
MathematicalProgram Minimum_Total_cost {
Objective: Total_cost;
Direction: minimize;
Constraints: AllConstraints;
Variables: AllVariables;
Type: Automatic;
}

Thank you very much

icon

Best answer by mohansx 17 March 2020, 23:50

View original

### 2 replies

Userlevel 5 +5

The wrong results is likely because you are using the variable Starting_time(d) as the condition of an IF block. From the formulation, looks like a device can be turned on only once during the day ? Using binary variables to represent on/off states is a very common formulation practice. Can you test your data on the attached model ?

Thank you very much for your help. I learn a lot from your code.

Now I know that we should avoid using if.. then..else as much as possible in mathematical program.