Undefined function or variable

211 views
Skip to first unread message

waqas ikhtiar

unread,
Aug 27, 2013, 5:21:26 AM8/27/13
to yal...@googlegroups.com
hi, In unit commitment problem i'm using a piece of code to calculate how many periods(t=1.....T) a generator has been offline before startup at time t.
                                                     
                                                     if  j == 1
                                                        if double(z(j,t)) == 1
                                                              toffg1=1;
                                                          elseif double(y(j,t)) ~= 1
                                                              toffg1=toffg1+1;
                                                          end
                                                      end
where z and y are binvar(decision variables). toffg1 is a variable in which i want to store offline periods of generator before startup at time t.
problem is i'm getting an error in matlab "Undefined function or variable 'toffg1'.". and if i define toffg1 globally then its value does not updates. please help how can i solve this issue

regards
W Ikhtiar

Johan Löfberg

unread,
Aug 27, 2013, 5:27:45 AM8/27/13
to yal...@googlegroups.com
Looks like yopu simply haven't initialized you variable before the loop. 

You do this
if 1==0
 
toffg1=1;
else
 
toffg1=toffg1+1
end

which will crash since toffg might be undefined and have no value. Hence, you must initialize it somewhere
toffg1 = 0;

other stuff

if 1==0
 
toffg1=1;
else
 
toffg1=toffg1+1
end




Johan Löfberg

unread,
Aug 27, 2013, 5:29:21 AM8/27/13
to yal...@googlegroups.com
I hope you understand that the code only makes sense to run once you actually have solved an optimization problem and obtained values for the variables z and y (double returns the values computed last time solvesdp was called)

waqas ikhtiar

unread,
Aug 27, 2013, 5:59:24 AM8/27/13
to yal...@googlegroups.com
actually i'm post graduate student and doing my thesis. i need to reproduce a research paper to continue my thesis. I've done lot of work regarding that, now i'm stuck when i'm calculating startup cost of generator. i'm sending you that paper please review that how can i implement equation number 16 of paper. In this equation there is a variable K(j,t), which is Cost of turning on generator j after it has been offline for t time periods 

regards
Tight Integer Progamming Unit Committment Problem.pdf

Johan Löfberg

unread,
Aug 27, 2013, 6:25:56 AM8/27/13
to
Well, your problem is not really YALMIP or optimization related. It is a basic MATLAB question

The term in the parenthesis would be something along the lines of

v(t)-sum(v(1:t-1))

or with indexing in both j and t

v(j,t)-sum(v(j,1:t-1))

Here, I assume 1-based indexing, the paper indexes from 0, which is impossible in MATLAB, so you have to shift everything correspondingly


waqas ikhtiar

unread,
Aug 27, 2013, 6:50:35 AM8/27/13
to yal...@googlegroups.com
 Objective = Objective + C(j)*P(j,t)+SUcost(j,t);    % euation number 1 of paper
   
%% StartUp Cost
             Constraints = [Constraints, SUcost(j,t) >= K(j,t)*(v(j,t)-sum(v(j,t-1:1)))];  % equation number 16 of paper
             Constraints = [Constraints, SUcost(j,t) >= 0];                                     % equation number 17 of paper

professor, i've written above equations. since startup cost is in the equation of objective function and we need to minimize that function, and there is a K in equation of startup cost. My question is that when and where i need to calculate K(j,t). for the time being i,ve used K as constant matrix of diementions JxT
, where J is set of generators and T is set of time periods.
i,ve attatched another paper in which equation A2 in appendix is related to calculation of K. i want to use this equation of K to calculate at each time t
please tell me when and where i need to calculate the value of K

2- is my logic is fine to calculate offline time periods of a generator if it has been offline before startup at time t.

thanks
A Computationally Efficient Mixed-Integer.pdf

waqas ikhtiar

unread,
Aug 27, 2013, 6:50:52 AM8/27/13
to yal...@googlegroups.com
2- is my logic is fine to calculate offline time periods of a generator if it has been offline before startup at time t.

thanks
A Computationally Efficient Mixed-Integer.pdf

Johan Löfberg

unread,
Aug 27, 2013, 6:58:43 AM8/27/13
to yal...@googlegroups.com
Well, that is not related to YALMIP or optimization either. The appendix simply says

if t <= tcold+DT
 
K(j,t) = h*c(j)
else
 K
(j,t) = c*c(j)
end

It is only a matter of creating your data matrix K before setting up the problem

waqas ikhtiar

unread,
Aug 27, 2013, 7:05:26 AM8/27/13
to yal...@googlegroups.com

professor, here are some results. All these results are when i use K as constant matrix of pre defined values. Now i need to modify my code to make K as function of time to calculate startup cost function


thanks 
W Ikhtair

waqas ikhtiar

unread,
Aug 27, 2013, 7:24:44 AM8/27/13
to yal...@googlegroups.com
Thank you very much for your tight time professor. It was very helpfull discussion,  i'll love to approach you again if i would have any problem. hope you will not mind 


Thanks 
W Ikhtiar

waqas ikhtiar

unread,
Aug 27, 2013, 9:38:12 AM8/27/13
to yal...@googlegroups.com
sir please tell me how to write constraints number 11 of the paper in matlab. same paper i had mailed you 1st time. i have written like this.
Entfor t=1:T
    for j=1:J
           Constraints = [Constraints, sum(1-v(j,:)-z(j,:)) >= 0]; 
    end
ender code here...

 

thanks

Tight Integer Progamming Unit Committment Problem.pdf

Johan Löfberg

unread,
Aug 27, 2013, 2:23:06 PM8/27/13
to yal...@googlegroups.com
Once again, this is just a matter of MATLAB indexing

for t = length(T)-D*T(j)+2:length(T)
 
Constraints = [Constraints, sum(1-v(j,t:length(T))-z(j,t)>=0]
end


waqas ikhtiar

unread,
Aug 27, 2013, 5:19:24 PM8/27/13
to yal...@googlegroups.com
sir i,ve written a code for reproduction of this paper which runs good at number of generators=3 and time span =24 by using the data of first 3 generators from table II of the paper (tight mixed integer linear programming formulations for the the unit commitment problem). but when i use complete data from table II of the paper my code does not give any result. the plot when having three generators is being attatched. when i increase number of generators to 8 i did not get any plot of power scedule (just straight line at 0).
may i email you the code so that you can check it

thanks
 

Johan Löfberg

unread,
Aug 27, 2013, 5:25:26 PM8/27/13
to yal...@googlegroups.com
Sure

waqas ikhtiar

unread,
Aug 27, 2013, 5:56:16 PM8/27/13
to yal...@googlegroups.com
Sir ive sent an email to you. Please visit your email

thanks

Johan Löfberg

unread,
Aug 28, 2013, 1:26:27 AM8/28/13
to yal...@googlegroups.com
Solved in 0.2 seconds using Gurobi.

Have you checked the diagnostic code from solvesdp (looks like you haven't since your code doesn't catch the output and you have a ; so you don't see it either). My guess is that you are using a crappy MILP solver

Johan Löfberg

unread,
Aug 28, 2013, 1:32:46 AM8/28/13
to
However, your code looks really odd.

In the paper, it is rather clear that T is a set, i.e., the set of hours which the problem is solved over (in your case 1...24). In your code, you have expressions abs(T) where the paper write |T|. That |T| must mean the cardinality of T, i.e., the number of hours. Why would it ever mean that T is a negative number which you must take the absolute number of? (it generates the same code though, I guess, but it makes the code harder to understand than necessary)

waqas ikhtiar

unread,
Aug 28, 2013, 1:52:05 AM8/28/13
to yal...@googlegroups.com
sir i'm using BNB solver, i don't have gurobi, is it free for academic use...?

and if i use length(T) instead of abs(T) then is it ok?


thanks

Johan Löfberg

unread,
Aug 28, 2013, 2:01:36 AM8/28/13
to yal...@googlegroups.com
Then you are using the crappiest solver possible

gurobi, cplex, mosek, xpress are all free for academia
http://users.isy.liu.se/johanl/yalmip/pmwiki.php?n=Solvers.Solvers

No, length(T) does not make sense (it is equal to 1 in your code). |T| means the cardinality of T, when T is a sequence as is the notation used in the paper. You however define T as a scalar, and you basically define T as the cardinality of their T directly.

Hence, you should either define T as 1:24 use for t = T in loops and |T|=length(T), or define T=24 and use for t = 1:T in the loops and by this definition |T|=24=T since you use another definition from the paper. Defining T=24 is the choice I think makes the most sense. T is the number of hours the optimization is performed over, no reason to define it as a set in MATLAB

waqas ikhtiar

unread,
Aug 28, 2013, 2:12:18 AM8/28/13
to yal...@googlegroups.com
J = 3;                          % set of generators (number of generators).
T = 24;                         % set of time periods (planning horizon) (h).
for t=1:T
    for j = 1:J 
        if t >= F(j)+1 & t <= T-DT(j)+1                     
           Constraints = [Constraints, sum(1-v(j,t:t+DT(j)-1))>= DT(j)*z(j,t)];   
        end
end 
end

 now is it ok if i use like above code...? no abs(T) and no length(T) , using simple T


Johan Löfberg

unread,
Aug 28, 2013, 3:08:44 AM8/28/13
to yal...@googlegroups.com
Looks reasonable

waqas ikhtiar

unread,
Aug 28, 2013, 3:32:08 AM8/28/13
to yal...@googlegroups.com
sir how to invoke gurobi, and at what place of code in begning..?

waqas ikhtiar

unread,
Aug 28, 2013, 3:56:47 AM8/28/13
to yal...@googlegroups.com

sir i'm in university hostel and using proxy server, when i try to license gurobi on my computer i got following error

Johan Löfberg

unread,
Aug 28, 2013, 4:12:13 AM8/28/13
to yal...@googlegroups.com
That is a question for the gurobi support team, or https://groups.google.com/forum/?fromgroups=#!forum/gurobi

Johan Löfberg

unread,
Aug 28, 2013, 4:12:37 AM8/28/13
to yal...@googlegroups.com
YALMIP will automatically use gurobi if it is found on your MATLAB path

waqas ikhtiar

unread,
Aug 28, 2013, 10:42:21 AM8/28/13
to yal...@googlegroups.com
Thanks Professor
Reply all
Reply to author
Forward
0 new messages