SLIM  1.0
Sparse Linear Methods (SLIM) for top-n recommender systems
 All Data Structures Files Functions Variables Typedefs Macros Pages
cmd.c
Go to the documentation of this file.
1 /**************************************************************/
2 /*! \file
3 
4  \brief This file contains all the routines for parameter
5  setup from the user
6 */
7 /**************************************************************/
8 
9 #include<slim.h>
10 
11 /**************************************************************/
12 /*!
13  \brief A structure for command-line options
14  */
15 /**************************************************************/
16 static struct gk_option slim_options[] = {
17  {"train_file", 1, 0, CMD_TRAIN_FILE},
18  {"test_file", 1, 0, CMD_TEST_FILE},
19  {"model_file", 1, 0, CMD_MODEL_FILE},
20  {"pred_file", 1, 0, CMD_PRED_FILE},
21  {"dbglvl", 1, 0, CMD_DBGLVL},
22  {"lambda", 1, 0, CMD_LAMBDA},
23  {"beta", 1, 0, CMD_BETA},
24  {"starti", 1, 0, CMD_STARTI},
25  {"endi", 1, 0, CMD_ENDI},
26  {"optTol", 1, 0, CMD_OPTTOL},
27  {"max_bcls_niters", 1, 0, CMD_MAX_BCLS_NITERS},
28  {"fs_file", 1, 0, CMD_FS_FILE},
29  {"fs", 0, 0, CMD_FS},
30  {"k", 1, 0, CMD_K},
31  {"bsize", 1, 0, CMD_BSIZE},
32  {"nratings", 1, 0, CMD_NRATINGS},
33  {"topn", 1, 0, CMD_TOPN},
34  {"transpose", 1, 0, CMD_TRANSPOSE},
35  {"help", 0, 0, CMD_HELP},
36  {0, 0, 0, 0}
37 };
38 
39 
40 
41 /**************************************************************/
42 /*! \brief Mini help
43  */
44 /**************************************************************/
45 static char helpstr[][512] = {
46 " ",
47 " Usage",
48 " slim_learn [options]",
49 " ",
50 " -train_file=string",
51 " Specifies the input file which contains the training data. This file should be",
52 " in .csr format. ",
53 " ",
54 " -test_file=string",
55 " Specifies the input file which contains the testing data. This file should be",
56 " in .csr format.",
57 " ",
58 " -model_file=string",
59 " Specifies the output file which will contains a model matrix. The output file will be in ",
60 " .csr format. ",
61 " ",
62 " -fs_file=string",
63 " Specifies the input file which contains a matrix for feature selection purpose. This input ",
64 " file should be in .csr format. This option takes effect only when -fs option is specified.",
65 " ",
66 " -pred_file=string",
67 " Specifies the output file which will contain the top-n prediction for each user. The output",
68 " file wil be in .csr format. If this option is not specified, no prediction scores will be output.",
69 " ",
70 " -lambda=float",
71 " Specifies the regularization parameter for the $\ell_1$ norm",
72 " ",
73 " -beta=flat",
74 " Specifies the regularizationi parameter for the $\ell_2$ norm",
75 " ",
76 " -starti=int",
77 " Specifies the index of the first column (C-style indexing) from ",
78 " which the sparse coefficient matrix will be calculated. The default",
79 " value is 0.",
80 " ",
81 " -endi=int",
82 " Specifies the index of the last column (exclusively) up to which",
83 " the sparse coefficient matrix will be calculated. The default value",
84 " is the number of total columns. ",
85 " ",
86 " -transpose",
87 " Specifies that the input feature selection matrix needs to be transposed.",
88 " ",
89 " -fs",
90 " Specifies that feature selection is required so as to accelerate the learning. ",
91 " ",
92 " -k=int",
93 " Specifies the number of features if feature selection is applied. The default ",
94 " value is 50. ",
95 " ",
96 " -dbglvl=int",
97 " Specifies the debug level. The default value is 0.",
98 " ",
99 " -optTol=float",
100 " Specifies the threshold which control the optimization. Once the error",
101 " from two optimization iterations is smaller than this value, the optimization",
102 " process will be terminated. The default value is 1e-5.",
103 " ",
104 " -max_bcls_niters=int",
105 " Specifies the maximum number of iterations that is allowed for optimization. ",
106 " Once the number of iterations reaches this value, the optimization process",
107 " will be terminated. The default value is 1e5.",
108 " ",
109 " -bsize=int",
110 " Specifies the block size for output. Once the calculation for these bsize ",
111 " blocks are done, they are dumped into the output file. The default value is 1000.",
112 " ",
113 " -nratings=int",
114 " Specifies the number of unique rating values in the testing set. The rating values",
115 " should be integers starting from 1. The default value is 1.",
116 " ",
117 " -topn=int",
118 " Specifies the number of recommendations to be produced for each user. The default",
119 " value is 10.",
120 " ",
121 " -help",
122 " Print this message.",
123 " ",
124 ""
125 };
126 
127 /**************************************************************/
128 /*! \brief A short help
129 */
130 /**************************************************************/
131 static char shorthelpstr[][100] = {
132  " ",
133  " Usage: slim_learn [options] ",
134  " use 'slim_learn -help' for a summary of the options.",
135 ""
136 };
137 
138 /**************************************************************/
139 /*! \brief Entry point of the command-line argument parsing
140 
141  \param[out] ctrl A ctrl structure to be filled out
142  \param[in] argc Number of arguments
143  \param[in] argv A list of arguments
144 */
145 /**************************************************************/
146 void parse_cmdline(ctrl_t *ctrl, int argc, char * argv[]){
147 
148  int c = -1, option_index = -1;
149 
150  if (ctrl == NULL)
151  ctrl = create_ctrl();
152 
153  while((c = gk_getopt_long_only(argc, argv, "", slim_options, &option_index)) != -1){
154  switch(c){
155 
156 
157  case CMD_TRAIN_FILE:
158  ctrl->train_file = gk_strdup(gk_optarg);
159  break;
160 
161  case CMD_TEST_FILE:
162  ctrl->test_file = gk_strdup(gk_optarg);
163  break;
164 
165  case CMD_MODEL_FILE:
166  ctrl->model_file = gk_strdup(gk_optarg);
167  break;
168 
169  case CMD_PRED_FILE:
170  ctrl->pred_file = gk_strdup(gk_optarg);
171  break;
172 
173  case CMD_DBGLVL:
174  ctrl->dbglvl = atoi(gk_optarg);
175  break;
176 
177  case CMD_LAMBDA:
178  ctrl->lambda = atof(gk_optarg);
179  break;
180 
181  case CMD_BETA:
182  ctrl->beta = atof(gk_optarg);
183  break;
184 
185  case CMD_STARTI:
186  ctrl->starti = atoi(gk_optarg);
187  break;
188 
189  case CMD_ENDI:
190  ctrl->endi = atoi(gk_optarg);
191  break;
192 
193  case CMD_OPTTOL:
194  ctrl->optTol = atof(gk_optarg);
195  break;
196 
197  case CMD_MAX_BCLS_NITERS:
198  ctrl->max_bcls_niters = atoi(gk_optarg);
199  break;
200 
201  case CMD_FS:
202  ctrl->fs = 1;
203  break;
204 
205  case CMD_FS_FILE:
206  ctrl->fs_file = gk_strdup(gk_optarg);
207  break;
208 
209  case CMD_K:
210  ctrl->k = atoi(gk_optarg);
211  break;
212 
213  case CMD_BSIZE:
214  ctrl->bsize = atoi(gk_optarg);
215  break;
216 
217  case CMD_NRATINGS:
218  ctrl->nratings = atoi(gk_optarg);
219  break;
220 
221  case CMD_TOPN:
222  ctrl->topn = atoi(gk_optarg);
223  break;
224 
225  case CMD_TRANSPOSE:
226  ctrl->transpose = 1;
227  break;
228 
229  case CMD_HELP:
230  for (int i=0; strlen(helpstr[i]) > 0; i++)
231  printf("%s\n", helpstr[i]);
232  exit(0);
233 
234 
235  case '?':
236  default:
237  printf("Illegal command-line option(s) %s\n", gk_optarg);
238  exit(0);
239 
240  }
241  }
242 
243 
244  if (argc-gk_optind != 0 || argc == 1) {
245  for (int i=0; strlen(shorthelpstr[i]) > 0; i++)
246  printf("%s\n", shorthelpstr[i]);
247  exit(0);
248  }
249 
250 
251 }