Yalmip works very slow?

527 views
Skip to first unread message

Lin Liu

unread,
Mar 7, 2018, 10:37:30 PM3/7/18
to YALMIP


Explored 1 nodes (1687 simplex iterations) in 0.49 seconds
Thread count was 4 (of 4 available processors)

Solution count 10: 683 692 694 ... 1209

Optimal solution found (tolerance 5.00e-02)
Best objective 6.830000000000e+02, best bound 6.570000000000e+02, gap 3.8067%

output = 

  包含以下字段的 struct:

    yalmiptime: 11.6603
    solvertime: 0.5967
          info: 'Successfully solved (GUROBI-GUROBI)'
       problem: 0


As you can see ,yalmip works extremely slow for my model. Yalmiptime is greater than solvertime. If I try bigger instance,it may take hours for yalmip to transfer my model. I know my model is big:about 100000 constraints or more.I am not sure if this phenomenon is common.


I upload my model, please say something about it.


thank you!


function [result] = EFIPPILP( CG,Z,cost,demand,d,pCycle,STLG,DTLG)

%EFIPPILP ILP方法实现路径保护


spanSet=cell([],1);%将所有的图上边放在Span_set中

[left,right]=find(Z);

for j=1:length(left)

    spanSet(j)={[left(j),right(j)]};

    cost(j)=1;

end


alpha=100000;

beta=0.0001;


%确定x的值,表示业务r是否能被P圈p保护

for r=1:length(demand)

    for p=1:length(pCycle)

        x(r,p)=pathprotectbycyclewithlimit(demand{r},pCycle{p});

    end

end

%确定qi的值,表示P-cycle p是否使用了图上边 spanSet j

for j=1:length(spanSet)

    for p=1:length(pCycle)

        qi(j,p)=pathoncycle(spanSet{j},pCycle{p});

    end

end

%确定Delta的值,表示业务m和n是否相交,1表示相交

for m=1:length(demand)

    for n=1:length(demand)

        delta(m,n)=pathcrosseachother(demand{m},demand{n});

    end

end

%---------------------------------------------------------------------%

%计算FIPP的ILP模型

%第一步,决策变量定义

s=intvar(1,length(spanSet));

nump=intvar(1,length(pCycle));

numrp=intvar(length(demand),length(pCycle),'full');

gama=binvar(length(demand),length(pCycle),'full');


%第二步,约束条件定义

cons=[];

%约束条件2,保证所有的业务(d(r))都需要被保护

for r=1:length(demand)

    cons=[cons,sum(x(r,:).*(numrp(r,:)))>=d(r)];

end


%约束条件3,第r个业务用于保护第p个p圈的次数要少于第p个p圈使用的总次数

for r=1:length(demand)

    for p=1:length(pCycle)

        cons=[cons,nump(p)>=numrp(r,p)];

    end

end


%约束条件4,4-1,边j被所有p圈使用的次数决定了该边应配置的保护容量

%4-2,链路j的保护容量s(j)与工作容量(TLG)之和不能超过链路总容量CG

for j=1:length(spanSet)

    cons=[cons,s(j)>=sum(nump.*qi(j,:),2)];

    s(j)+STLG(spanSet{j}(1),spanSet{j}(2))<=CG(spanSet{j}(1),spanSet{j}(2));

end


%约束条件5,如果numrp(r,p)>0,则Gama(r,p)=1,如果numrp=0,则Gama(r,p)=0;

for r=1:length(demand)

    for p=1:length(pCycle)

        cons=[cons,gama(r,p)>=beta*numrp(r,p)];

    end

end


%约束条件6

for r=1:length(demand)

    for p=1:length(pCycle)

        cons=[cons,gama(r,p)<=alpha*numrp(r,p)];

    end

end


% % %约束条件7,保证不相交的两个业务才能被同一个P圈保护

for p=1:length(pCycle)

    for m=1:length(demand)

        for n=m+1:length(demand)

            cons=[cons,delta(m,n)+gama(m,p)+gama(n,p)<=2];

        end

    end

end


%设置目标函数

obj=sum(s);

options=sdpsettings('solver','gurobi','cachesolvers',1);

options.gurobi.MIPGap=0.05;            %gap值限制

output=optimize(cons,obj,options)      %求解MILP,并输出相关参数

DR=(sum(sum(DTLG))-sum(sum(STLG)))/sum(sum(STLG));

FIPP=value(obj)/sum(sum(STLG));

end

Johan Löfberg

unread,
Mar 8, 2018, 2:17:57 AM3/8/18
to YALMIP
As always in MATLAB, you have to vectorize the code. As it now is terribly inefficiently written, but almost everything looks trivially vectorizable.

As it is now, you have 100000 elementwise constraints, which means YALMIP has spent at most 0.1 millisecond per symbolic constraint to analyze and extract numerical data, which doesn't sound too bad.

BTW,     s(j)+STLG(spanSet{j}(1),spanSet{j}(2))<=CG(spanSet{j}(1),spanSet{j}(2)); is never added to the model.

Lin Liu

unread,
Mar 8, 2018, 4:41:35 AM3/8/18
to YALMIP

1.Thank you for reminding me the constraints  which is not added to the model.

2.If YALMIP  spent  0.1 millisecond per symbolic constraint to analyze , It’s acceptable to me. In fact ,when constraints  are added to 100 0000,matlab tell me Yalmiptime is 1000+s. But I think it takes more than 1 hour for YALMIP to do this.I have a clock.

3.Attachment EFIPPILP is my source code. I think constraint 7 takes the longest time to transfer. And YALMIP add more constraints than I think . For example, I calculate the total constraint number for my model is less than 50 0000.But YALMIP shows the total number of constraints are more than 100 0000.

4.Another question: matlab 2016 always crash when  working with YALMIP. Maybe other version of matlab works good .Which version is OK for YALMIP? YALMIP version is 20180209.
EFIPPILP.m

Johan Löfberg

unread,
Mar 8, 2018, 5:23:22 AM3/8/18
to YALMIP
You *have* to vectorize this code. It is really not anything particular to YALMIP here. You are wasting a lot of time by not doing the obvious matrix versions of your code, like

for r=1:length(demand)
   
for p=1:length(pCycle)
        cons
=[cons,nump(p)>=numrp(r,p)];
   
end
end

which simply is repmat(nump(:)',length(demand),1) >= numrp etc.

Your triple-loop will require some thought to be maximally improved, but simply removing the inner for-loop can give massive gains. The following similiar case goes from 10 seconds to 0.3 seconds, and that's just one step

n = 100;
delta
= sdpvar(n,n);
gama
= sdpvar(n,n);
cons
= [];
tic
for p=1:n
   
for m=1:n
       
for n=m+1:n
            cons
=[cons,delta(m,n)+gama(m,p)+gama(n,p)<=2];
       
end
   
end
end
toc


tic cons = []; for p=1:10 for m=1:10 s = gama(m,p); cons=[cons,delta(m,m+1:10)+s+gama(m+1:10,p)'<=2]; end end toc


Of course, dimension in various directions makes a difference. Removing the outer-most loop is more efficient since the inner most doesn't always have maximal dimension, so twice as fast

tic
cons = [];
    for m=1:n
        for n=m+1:n
            cons=[cons,delta(m,n)+gama(m,:)+gama(n,:)<=2];
        end
    end
toc

Eliminating more loops by clever repmating etc will drop this to close to 0.


If it crashes in 2016 then I guess you are using cplex 12.6 or 12.7 which is known to not work in this matlab version as it seg-faults. You have to upgrade to 12.8, or downgrade matlab.

Lin Liu

unread,
Mar 8, 2018, 6:29:26 AM3/8/18
to YALMIP
Great thanks! 

I will try to modify my model.

For crash quesiton: I install cplex 12.7.1 on my computer. But I don't use cplex in my model.  Gurobi is the solver in my model. Will this cause crash ?

If remove the path of cplex 12.7.1. Is it ok?

Johan Löfberg

unread,
Mar 8, 2018, 6:31:36 AM3/8/18
to YALMIP
Yes, you have to update cplex or remove from path. YALMIP calls the options constructor in cplex when you use sdpsettings, and cplex then crashes.
Reply all
Reply to author
Forward
0 new messages