annotate core/tools/machine_learning/save_svmlight_inequalities.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 function success = save_svmlight_inequalities(lhs, rhs, factors, file)
wolffd@0 2 % success = save_svmlight_inequalities(lhs, rhs, file)
wolffd@0 3 %
wolffd@0 4 % saves the optimisation problem given by lhs and rhs to
wolffd@0 5 % a svmlight data file. the individual equations can
wolffd@0 6 % be weighted using
wolffd@0 7 %
wolffd@0 8 % success = save_svmlight_inequalities(lhs, rhs, factors, file)
wolffd@0 9
wolffd@0 10 if nargin == 3
wolffd@0 11 file = factors;
wolffd@0 12 factors = [];
wolffd@0 13 end
wolffd@0 14
wolffd@0 15 % open file
wolffd@0 16 fid = fopen(file, 'w+');
wolffd@0 17 if fid < 1
wolffd@0 18 success = 0;
wolffd@0 19 return;
wolffd@0 20 end
wolffd@0 21
wolffd@0 22 try
wolffd@0 23 % write individual constraint rows
wolffd@0 24 for i = 1:size(lhs,1)
wolffd@0 25
wolffd@0 26 % ---
wolffd@0 27 % print rows:" rhs #fnum:#fval #fnum:#fval #fnum:#fval ..."
wolffd@0 28 % ---
wolffd@0 29
wolffd@0 30 % print right hand side
wolffd@0 31 fprintf(fid,'%d ', rhs(i));
wolffd@0 32
wolffd@0 33 % print cost factors if availablefactor
wolffd@0 34 if (numel(lhs{i,1}) > 0) && (numel(factors) >= i)
wolffd@0 35
wolffd@0 36 fprintf(fid,'cost:%f ', factors(i));
wolffd@0 37 end
wolffd@0 38
wolffd@0 39 % print left hand side
wolffd@0 40 for j = 1:numel(lhs{i,1})
wolffd@0 41
wolffd@0 42 fprintf(fid,'%d:%2.16f ', lhs{i,1}(j), lhs{i,2}(j));
wolffd@0 43 end
wolffd@0 44
wolffd@0 45 % finish line
wolffd@0 46 fprintf(fid,'\n');
wolffd@0 47 end
wolffd@0 48 catch
wolffd@0 49 success = 0;
wolffd@0 50 fclose(fid);
wolffd@0 51 return;
wolffd@0 52 end
wolffd@0 53 success = 1;
wolffd@0 54 fclose(fid);
wolffd@0 55
wolffd@0 56
wolffd@0 57