Beispiel #1
0
static void write_rhs(FILE* fp, LpFormat format, const Con* con, ConType type)
{
   assert(fp  != NULL);
   assert(con != NULL);
   
   switch(type)
   {
   case CON_RHS :
   case CON_RANGE :
      fprintf(fp, " <= ");
      write_val(fp, format, FALSE, con->rhs);
      break;
   case CON_LHS :
      fprintf(fp, " >= ");
      write_val(fp, format, FALSE, con->lhs);
      break;
   case CON_EQUAL :
      fprintf(fp, " = ");
      write_val(fp, format, FALSE, con->rhs);
      break;
   default :
      abort();
   }
   fprintf(fp, "\n");
}
void set_1003(void) {
  char v_tty[128];
  char v_buf[512];
  int v_ret;
  char msg1[] = { 'A','T','$','Q','C','D','M','G',0x0d,0x0a,0x00 };
  char msg2[] = { 'A','T','^','D','I','S','L','O','G','=','2','5','5',0x0d,0x0a,0x00 };
  char msg3[] = { 0x7e,0x3a,0xa1,0x6e,0x7e,0x00 };

  if(get_mode() != 1404) {
    logging(ANDROID_LOG_ERROR, "modem doesn't have USB-PID 1404!\n");
    exit(1);
  }
  logging(ANDROID_LOG_WARN, "setting modem to USB-PID 1003 ...\n");
  
  getport(v_tty);
  if(v_tty == NULL) {
    logging(ANDROID_LOG_ERROR, "can't find valid tty-port\n");
    exit(1);
  }

  v_ret = write_val(v_tty, msg1);
  v_ret = write_val(v_tty, msg2);
  v_ret = write_val(v_tty, msg3);
  sleep(7);

  v_ret = get_mode();
  if(v_ret != 1003) {
    sprintf(v_buf, "ERROR! Modem was not set to USB-PID 1003, >%d< was reported instead\n", v_ret);
    logging(ANDROID_LOG_ERROR, v_buf);
    exit(1);
  }
  logging(ANDROID_LOG_WARN, "Modem was set to USB-PID 1003 successfuly\n");
  exit(0);
}
// write_class
//  Writes CLASS_xxx and CLASSD_xxx
void write_class(Val stream, Val klass)
{
    Class* pClass = klass->Decode<Class>();

    format(stream, L"// ~S~%", pClass->m_name);
    write_val(stream, L"CLASS_", encode_symbol(pClass->m_name), klass);
    write_val(stream, L"ty_", encode_symbol(pClass->m_name), pClass->m_name);

    Val classd = pClass->m_instanced;

    if (nil != classd)
    {
        write_val(stream, L"CLASSD_",
            encode_symbol(pClass->m_name), pClass->m_instanced );
    }
} // write_class
Beispiel #4
0
 void write_list(T xs) {
   out_ << "c(";
   for (typename T::size_type i = 0; i < xs.size(); ++i) {
     if (i > 0) out_ << ", ";
     write_val(xs[i]);
   }
   out_ << ")";
 }
Beispiel #5
0
 void write_stan(const Eigen::Matrix<double,
                                     Eigen::Dynamic,Eigen::Dynamic>& x) {
   typedef Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic>::Index Index;
   out_ << "structure(c(";
   std::vector<double> vals;
   for (Index m = 0; m < x.cols(); ++m) {
     for (Index n = 0; n < x.rows(); ++n) {
       if (m > 0 || n > 0) out_ << ", ";
       write_val(x(m,n));
     }
   }
   out_ << "), .Dim = c(";
   out_ << x.rows() << ", " << x.cols();
   out_ << "))";
 }
Beispiel #6
0
static void	ex_st(t_processes *current, int paff, int p[4], int ocp)
{
	if (check_type(ocp, 4) == 'r' && check_reg(p[2]))
		current->reg[p[2] - 1] = current->reg[p[1] - 1];
	else
	{
		paff = p[2];
		p[2] %= IDX_MOD;
		p[2] += current->pc;
		p[2] %= MEM_SIZE;
		while (p[2] < 0)
			p[2] += MEM_SIZE;
		g_data->vm[p[2]] = current->reg[p[1] - 1] >> 24;
		write_val(p[2], current->reg[p[1] - 1], current->player_id);
		if (g_data->arg & 4 && !g_data->ncurse)
			ft_printf("P%5d | st r%d %d\n",
			current->id + 1, p[1], paff);
	}
}
Beispiel #7
0
 void write_stan(int x) {
   write_val(x);
 }
Beispiel #8
0
 void write_stan(double x) {
   write_val(x);
 }
Beispiel #9
0
 void write_stan_val(const Eigen::Matrix<double,Eigen::Dynamic,1>& x,
                     const std::vector<size_t>& idx,
                     const size_t pos) {
   write_val(x[idx[pos]]);
 }
Beispiel #10
0
 void write_stan_val(const Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic>& x,
                     const std::vector<size_t>& idx,
                     const size_t pos) {
   size_t next_pos = pos + 1;
   write_val(x(idx[pos],idx[next_pos]));
 }
Beispiel #11
0
 void write_stan_val(const std::vector<int>& x,
                     const std::vector<size_t>& idx,
                     const size_t pos) {
   write_val(x[idx[pos]]);
 }
void daemonize(void) {
  int v_ret;
  pid_t v_pid;
  char v_name[128];
  char v_buf[512];
  struct inotify_event iev;
  struct sigaction    actions;

  v_pid = fork();

  if(v_pid > 0) {
    exit(0);
  }

  sprintf(v_buf,"emw820w_tool (%s) daemonizing ...\n", g_vers);
  logging(ANDROID_LOG_ERROR, v_buf);
  g_loop=1;
  
  close(STDIN_FILENO);
  close(STDOUT_FILENO);
  close(STDERR_FILENO);


  memset(&actions, 0, sizeof(actions));
  sigemptyset(&actions.sa_mask);
  actions.sa_flags = 0;
  actions.sa_handler = sighdl;
  sigaction(SIGTERM,& actions, NULL);

  g_ifd = inotify_init();
  if(g_ifd < 0) {
    sprintf(v_buf,"inotify_init() error %d\n", errno);
    logging(ANDROID_LOG_ERROR, v_buf);
    exit(1);
  }

  logging(ANDROID_LOG_DEBUG, "entering watchdog mode ...\n");

  if(get_mode() != 1404) {
    set_1404();
    sleep(2);
  }
  read_val("/sys/bus/usb/devices/1-1/power/autosuspend", g_curval);

  while(g_loop == 1) {
    getport(v_name);
    
    g_wfd = inotify_add_watch(g_ifd, v_name, IN_DELETE_SELF);
    if(g_wfd < 0) {
      sprintf(v_buf,"watchdog: inotify_add_watch() error %d\n", errno);
      logging(ANDROID_LOG_ERROR, v_buf);
      sleep(2);
    } else {
      sprintf(v_buf,"watchdog for %s initialized\n", v_name);
      logging(ANDROID_LOG_DEBUG, v_buf);
  
      iev.mask = 0;
      while(iev.mask != IN_DELETE_SELF) {
        v_ret=read(g_ifd, &iev, sizeof(iev));
        if(g_loop == 0) {
          break;
        }
      } 
      v_ret = inotify_rm_watch(g_ifd, g_wfd);
      if(g_loop == 1) {
        sleep(5);
        if((v_ret = get_mode()) != 1404) {
          logging(ANDROID_LOG_DEBUG,"3G crash detected!\n");
          logging(ANDROID_LOG_DEBUG,"activating wakelock\n");
          v_ret = write_val("/sys/power/wake_lock", "em820w_tool\n");
          while (v_ret != 1404) {
            set_1404();
            sleep(10);
            v_ret = get_mode();
          }
          logging(ANDROID_LOG_DEBUG,"deactivating wakelock\n");
          v_ret = write_val("/sys/power/wake_unlock", "em820w_tool\n");
        }
        sleep(2);
      }
    }
  }

  logging(ANDROID_LOG_DEBUG,"exiting...\n");
  close(g_wfd);
  close(g_ifd);
  exit(0);
}
Beispiel #13
0
int exec_container_init(struct arg_start *arg,
			struct env_create_param3 *create_param)
{
	int fd, ret;
	char *argv[] = {"init", "-z", "      ", NULL};
	char *envp[] = {"HOME=/", "TERM=linux", NULL};
	struct statfs sfs;

	/* Clear supplementary group IDs */
	setgroups(0, NULL);
	/* for 32-bit userspace running over 64-bit kernels */
	set_personality32();

	/* Create /fastboot to skip run fsck */
	fd = open("/fastboot", O_CREAT | O_RDONLY, 0644);
	if (fd >= 0)
		close(fd);

	if (arg->res->misc.wait == YES) {
		if (add_reach_runlevel_mark()) {
			ret = VZ_WAIT_FAILED;
			return -1;
		}
	}

	if (mkdir("/proc", 0555) && errno != EEXIST)
		return vzctl_err(VZ_SYSTEM_ERROR, errno,
				"Can't mkdir /proc");

	if (statfs("/proc", &sfs))
		return vzctl_err(VZ_SYSTEM_ERROR, errno,
				"statfs on /proc failed");

	if (sfs.f_type != PROC_SUPER_MAGIC &&
	    mount("proc", "/proc", "proc", 0, 0))
		return vzctl_err(VZ_SYSTEM_ERROR, errno,
				"Failed to mount /proc");

	if (stat_file("/sys") == 1)
		mount("sysfs", "/sys", "sysfs", 0, 0);

	if (create_param->feature_mask & VE_FEATURE_NFSD) {
		mount("nfsd", "/proc/fs/nfsd", "nfsd", 0, 0);
		make_dir("/var/lib/nfs/rpc_pipefs", 1);
		mount("sunrpc", "/var/lib/nfs/rpc_pipefs", "rpc_pipefs", 0, 0);
	}
	write_val("/proc/sys/net/ipv6/conf/all/forwarding", "0");

	/* Close status descriptor to report that
	 * environment is created.
	*/
	close(STDIN_FILENO);
	/* Now we wait until CT setup will be done
	   If no error, then start init, otherwise exit.
	*/

	if (read(arg->wait_p, &ret, sizeof(ret)) == 0)
		return -1;

	if ((fd = open("/dev/null", O_RDWR)) != -1) {
		dup2(fd, 0);
		dup2(fd, 1);
		dup2(fd, 2);
		close(fd);
	}

	logger(10, 0, "Starting init");

	close_fds(0, arg->err_p, -1);

	execve("/sbin/init", argv, envp);
	execve("/etc/init", argv, envp);
	execve("/bin/init", argv, envp);
	ret = VZ_FS_BAD_TMPL;
	write(arg->err_p, &ret, sizeof(ret));
	return ret;
}
void set_1404(void) {
  char v_rilsvc[] = "ril-daemon";
  char *p_rilsvc = v_rilsvc;
  char p_name[64];
  char v_buf[1024];
  int v_ret;
  int v_max = 0;

  if(get_mode() == 1404) {
    logging(ANDROID_LOG_DEBUG, "modem already has USB-PID 1404!\n");
    return;
  }

  if(g_svc != '\0') {
    p_rilsvc = g_svc;
  }

  logging(ANDROID_LOG_WARN, "setting modem to USB-PID 1404 ...\n");

  // v_ret = write_val("/sys/bus/usb/devices/usb1/power/control", "on\n");
  if(g_kill == 0) {
    sprintf(v_buf, "stopping service %s ...\n", p_rilsvc);
    logging(ANDROID_LOG_DEBUG, v_buf);
    property_set("ctl.stop", p_rilsvc);
  }
  v_ret = write_val("/sys/EcControl/ThreeGPower", "0\n");
  v_max = 10;
  getport(p_name);
  while(p_name != NULL && v_max > 0) {
    getport(p_name);
    v_max--;
    sleep(1);
  }
  v_ret = write_val("/sys/EcControl/ThreeGPower", "1\n");
  v_max = 10;
  getport(p_name);
  while(p_name == NULL && v_max > 0) {
    getport(p_name);
    v_max--;
    sleep(1);
  }
  v_max=0;
  while(get_mode() != 1404 && v_max < 5) {
    v_ret = write_val("/sys/bus/usb/devices/usb1/power/control", "on\n");
    sleep(1 + v_max);
    v_max++;
  }
  v_ret = write_val("/sys/bus/usb/devices/1-1/power/autosuspend", "2\n");
  v_ret = write_val("/sys/bus/usb/devices/1-1/power/control", "on\n");
  v_ret = write_val("/sys/bus/usb/devices/1-1/power/wakeup", "enabled\n");
  v_ret = write_val("/sys/bus/usb/devices/usb1/power/control", "auto\n");
  if(strlen(g_curval) > 0) {
    v_ret = write_val("/sys/bus/usb/devices/1-1/power/autosuspend", g_curval);
  } else {
    v_ret = write_val("/sys/bus/usb/devices/1-1/power/autosuspend", "2\n");
  }
   

/*
  sleep(5);
  v_max=0;
  while(get_mode() != 1404 && v_max < 5) {
    v_ret = write_val("/sys/bus/usb/devices/usb1/power/control", "on\n");
    sleep(1 + v_max);
    v_max++;
  }
*/
  if(g_kill == 0) {
    sprintf(v_buf, "starting service %s ...\n", p_rilsvc);
    logging(ANDROID_LOG_DEBUG, v_buf);
    property_set("ctl.start", p_rilsvc);
  } else {
    logging(ANDROID_LOG_DEBUG, "killing rild...\n");
    system("killall rild");
  }
  if(get_mode() == 1404) {
    logging(ANDROID_LOG_WARN, "Modem was set to USB-PID 1404 successfuly\n");
  } else {
    logging(ANDROID_LOG_ERROR, "ERROR! Modem was not set to USB-PID 1404\n");
  }
}
Beispiel #15
0
static void do_rewrite(const char *conffile, int argc, char **argv)
{
    FILE *inf, *outf;
    char line[MAXLINELEN], tmpname[MAXPATHLEN + 1], *p;
    int ret, len = strlen(argv[2]), lineno = 0, last_line = 0;

    inf = fopen(conffile, "r");
    FAILIF(!inf, "Failed to open '%s' for reading\n", conffile);

    /* Set umask to 022 so that the newly created file uses 0644 */
    umask(022);

    /* find the last line that contains a (possibly commented) reference to our
     * variable, create a backup copy at the same time */
    snprintf(tmpname, MAXPATHLEN, "%s.bak", conffile);
    outf = fopen(tmpname, "w");
    FAILIF(!outf, "Failed to open '%s' for writing\n", tmpname);

    lineno = 0;
    while (fgets(line, MAXLINELEN, inf)) {
        fputs(line, outf);
        lineno++;
        p = line;
        /* skip whitespace and comment characters */
        while (*p == ' ' || *p == '\t')
            p++;
        if (*p == '#')
            p++;
        if (match_var(p, argv[2], len))
            last_line = lineno;
    }
    if (!last_line)
        last_line = lineno;

    fclose(outf);

    /* now create a new configuration file, commenting out all previous
     * occurences that defined a value to our variable and append a new
     * define after the last occurence. hopefully this will keep the
     * new variable definition close to the comments */
    snprintf(tmpname, MAXPATHLEN, "%s.new", conffile);
    outf = fopen(tmpname, "w");
    FAILIF(!outf, "Failed to open '%s' for writing\n", tmpname);

    rewind(inf);
    lineno = 0;
    while (fgets(line, MAXLINELEN, inf)) {
        lineno++;

        /* comment any lines that were defining this variable */
        if (match_var(line, argv[2], len))
            fputs("# ", outf);
        fputs(line, outf);

        /* append the new value after the last found (possibly commented)
	 * definition */
        if (lineno == last_line)
            write_val(outf, argc, argv);
    }

    fclose(outf);
    fclose(inf);

    ret = rename(tmpname, conffile);
    FAILIF(ret, "Failed to update configuration file '%s'\n", conffile);
}
Beispiel #16
0
int huffman_enc_64(short *in, int in_len, uint8_t **out, int *out_length, short *last_block)
{
	// allocate a bit more space
	int out_pos = 0;
	uint8_t tmp[64];
	//*out = (char*)malloc(sizeof(char) * in_len);

	//for (int i = -255; i <= 255; i++) {
	//	short val = i;
	//	printf("diff category for val: %d -> %d\n", val, get_dc_difference_category(val));
	//}
	
	// get dc
	short dc = in[0];
	if (last_block != NULL) {
		dc = last_block[0] - dc;
	}
	//dc = 80;
	//dc = -1;
	//dc = -26;
	int cat = get_dc_difference_category(dc);

	uint8_t store = 0;
	int p = 0;
	int vp = 0;
	
	//printf("got dc: %d, category %d, vlen: %d, dc_basecodelen: %d\n", dc, cat, cat + dc_base_code_len[cat], dc_base_code_len[cat]);

	// write dc base code
	write_val(tmp, &out_pos, &store, &p, dc_base_code[cat], dc_base_code_len[cat]);
	// write lsb
	// no store reset
	int val = (dc & ((int)pow(2, cat + dc_base_code_len[cat]) - 1)) - (dc < 0 ? 1 : 0);
	int vlen = cat;
	write_val(tmp, &out_pos, &store, &p, val, vlen);

	//printf("write ac's\n");
	int run = 0;
	for (int i = 1; i < in_len; ++i) {
		if (in[i] == 0) {
			run++;
			if (run >= 0xF) {
				int ac = 0xFF7;
				write_val(tmp, &out_pos, &store, &p, ac, 12);
				run = 0;
			}
		}
		else {
			int ac = in[i];
			int ac_cat = get_dc_difference_category(ac);
			//printf("write ac: %d\tcat: %d\t run: %d\t\n", ac, ac_cat, run);
			//printf("write basecode: %s\n", int2bin(ac_base_code[run][ac_cat - 1], NULL));
			// write base code
			write_val(tmp, &out_pos, &store, &p, ac_base_code[run][ac_cat - 1], ac_base_code_len[run][ac_cat - 1]);
			// write lsb
			int ac_val = (ac & ((int)pow(2, ac_cat + ac_base_code_len[run][ac_cat - 1]) - 1)) - (ac < 0 ? 1 : 0);
			int ac_vlen = ac_cat;
			//printf("write ac_val [len: %d]: %s\n", ac_vlen, int2bin(ac_val, NULL));
			write_val(tmp, &out_pos, &store, &p, ac_val, ac_vlen);

			run = 0;
		}
	}

	//printf("write eob\n");
	int eob = EOB;
	int ac_vlen = 4;
	write_val(tmp, &out_pos, &store, &p, eob, ac_vlen);

	// flush rest
	//printf("p:%d\n", p);
	for (; p < 8; ++p) {
		//printf("pad\n");
		store |= 1 << (8 - p - 1);
	}
	(tmp)[out_pos++] = store;

	//for (int i = 0; i < out_pos; ++i) {
	//	printf("%d -> %s\n", i, uint2bin((tmp)[i], NULL));
	//}

	*out = (uint8_t*)malloc(sizeof(uint8_t) * out_pos);
	for (int i = 0; i < out_pos; ++i) {
		(*out)[i] = tmp[i];
	}
	*out_length = out_pos;
	
	return 0;
}
Beispiel #17
0
/* A specification for the LP file format can be found in the
 * ILOG CPLEX 7.0 Reference Manual page 527.
 * ILOG CPLEX 8.0 Reference Manual page 595.
 * The "Lazy Constraints" section seems to be undocumented.
 */
void lpf_write(
   const Lps*  lp,
   FILE*       fp,
   LpFormat    format,
   const char* text)
{
   const Var* var;
   Con*       con;
   Con**      contab;
   Bool  have_integer   = FALSE;
   Bool  have_separate  = FALSE;
   Bool  have_checkonly = FALSE;
   int   cnt;
   int   i;
   int   k;
   int   name_size;
   char* name;

   assert(lp != NULL);
   assert(fp != NULL);

   /* Store constraint pointers and permute them
    * (add 1 in case lp->cons == 0)
    */
   contab = calloc((size_t)lp->cons + 1, sizeof(*contab));

   assert(contab != NULL);

   k = 0;
   for(con = lp->con_root; con != NULL; con = con->next)
      contab[k++] = con;

   assert(k == lp->cons);

   if (format == LP_FORM_RLP)
      permute(lp->cons, (void**)contab);

   name_size = lps_getnamesize(lp, LP_FORM_LPF);
   name      = malloc((size_t)name_size);

   assert(name != NULL);
   
   if (text != NULL)
      fprintf(fp, "%s", text);   
      
   fprintf(fp, "\\Problem name: %s\n", lp->name);   
   fprintf(fp, "%s\n", (lp->direct == LP_MIN) ? "Minimize" : "Maximize");
   fprintf(fp, " %s: ", lp->objname == NULL ? "Objective" : lp->objname);
   
   for(var = lp->var_root, cnt = 0; var != NULL; var = var->next)
   {
      /* If cost is zero, do not include in objective function
       */
      if (mpq_equal(var->cost, const_zero))
         continue;

      lps_makename(name, name_size, var->name, format == LP_FORM_HUM ? -1 : var->number);
      
      if (mpq_equal(var->cost, const_one))
         fprintf(fp, " + %s", name);
      else if (mpq_equal(var->cost, const_minus_one))
         fprintf(fp, " - %s", name);
      else
      {
         fprintf(fp, " ");         
         write_val(fp, format, TRUE, var->cost);      
         fprintf(fp, " %s", name);
      }

      if (++cnt % 6 == 0)
         fprintf(fp, "\n ");
   }
   /* ---------------------------------------------------------------------- */

   /* First loop run for normal constraints, second one for
    * user cuts, thrid one for lazy constraints, if any.
    */
   for(i = 0; i < 3; i++)
   {      
      if (i == 0)
         fprintf(fp, "\nSubject to\n");      
      else if (i == 1)
      {
         if (!have_separate)
            continue;
         else
            fprintf(fp, "\nUser Cuts\n");
      }
      else if (i == 2)
      {
         if (!have_checkonly)
            continue;
         else
            fprintf(fp, "\nLazy Constraints\n"); 
      }

      for(k = 0; k < lp->cons; k++)
      {
         con = contab[k];

         if (con->size == 0 && con->qme_first == NULL && con->term == NULL)
            continue;

         if (i == 0 && ((con->flags & (LP_FLAG_CON_SEPAR | LP_FLAG_CON_CHECK)) != 0))
         {
            if ((con->flags & LP_FLAG_CON_SEPAR) == LP_FLAG_CON_SEPAR)
               have_separate = TRUE;
            if ((con->flags & LP_FLAG_CON_CHECK) == LP_FLAG_CON_CHECK)
               have_checkonly = TRUE;

            continue;
         }        
         if (i == 1 && (con->flags & LP_FLAG_CON_SEPAR) != LP_FLAG_CON_SEPAR)
            continue;

         if (i == 2 && (con->flags & LP_FLAG_CON_CHECK) != LP_FLAG_CON_CHECK)
            continue;
    
         if (con->type == CON_RANGE)
         {
            if (format == LP_FORM_HUM)
            {
               lps_makename(name, name_size, con->name, -1);
               fprintf(fp, " %s:\n ", name);

               write_lhs(fp, format, con, CON_RANGE);
               write_row(fp, format, con, name, name_size); 
               write_rhs(fp, format, con, CON_RANGE);
            }
            else
            {
               /* Split ranges, because LP format can't handle them.
                */
               lps_makename(name, name_size, con->name, con->number);
               fprintf(fp, " %sR:\n ", name);
   
               write_row(fp, format, con, name, name_size); /* changes name */
               write_rhs(fp, format, con, CON_RHS);
   
               lps_makename(name, name_size, con->name, con->number);
               fprintf(fp, " %sL:\n ", name);
   
               write_row(fp, format, con, name, name_size); /* changes name */
               write_rhs(fp, format, con, CON_LHS);
            }
         }
         else
         {
            lps_makename(name, name_size, con->name, format == LP_FORM_HUM ? -1 : con->number);

            if (i == 0)
               fprintf(fp, " %s:\n ", name);
            else if (i == 1)
               fprintf(fp, " %sU:\n ", name);
            else if (i == 2)
               fprintf(fp, " %sZ:\n ", name);

            if (con->ind_var != NULL)
            {
               lps_makename(name, name_size, con->ind_var->name, format == LP_FORM_HUM ? -1 : con->ind_var->number);
               fprintf(fp, "%s = %d -> ", name, con->ind_dir ? 1 : 0);
            }
            write_row(fp, format, con, name, name_size);
            write_rhs(fp, format, con, con->type);
         }
      }
   }

   /* ---------------------------------------------------------------------- */

   fprintf(fp, "Bounds\n");

   for(var = lp->var_root; var != NULL; var = var->next)
   {
      /* A variable without any entries in the matrix
       * or the objective function can be ignored.
       * (also not part of an SOS or quadratic constraint)
       */
      if (var->size == 0 && mpq_equal(var->cost, const_zero) && !var->is_used)
         continue;

      lps_makename(name, name_size, var->name, format == LP_FORM_HUM ? -1 : var->number);

      if (var->type == VAR_FIXED)
      {
         fprintf(fp, " %s = ", name);
         write_val(fp, format, FALSE, var->lower);      
         fprintf(fp, "\n");
      }
      else
      {
         /* Check if we have integer variables
          */
         if (var->vclass == VAR_INT)
            have_integer = TRUE;
         
         fprintf(fp, " ");

         if (var->type == VAR_LOWER || var->type == VAR_BOXED)
            write_val(fp, format, FALSE, var->lower);      
         else
            fprintf(fp, "-inf");
         
         fprintf(fp, " <= %s <= ", name);
         
         if (var->type == VAR_UPPER || var->type == VAR_BOXED)
         {
            write_val(fp, format, FALSE, var->upper);      
            fprintf(fp, "\n");
         }
         else
            fprintf(fp, "+inf\n");
      }
   }

   /* ---------------------------------------------------------------------- */

   if (have_integer)
   {
      fprintf(fp, "General\n");
      
      for(var = lp->var_root; var != NULL; var = var->next)
      {
         if (var->vclass != VAR_INT)
            continue;

         if (var->size == 0 && mpq_equal(var->cost, const_zero) && !var->is_used)
            continue;
         
         lps_makename(name, name_size, var->name, format == LP_FORM_HUM ? -1 : var->number);

         fprintf(fp, " %s\n", name);
      }
   }

   /* ---------------------------------------------------------------------- */

   if (lps_has_sos(lp))
   {
      const Sos* sos;
      const Sse* sse;

      fprintf(fp, "SOS\n");

      for(sos = lp->sos_root; sos != NULL; sos = sos->next)
      {
         cnt = 0;

         fprintf(fp, " %s:S%d:: ", 
            sos->name,
            sos->type == SOS_TYPE1 ? 1 : 2);
    
         for(sse = sos->first; sse != NULL; sse = sse->next)
         {
            lps_makename(name, name_size, sse->var->name, format == LP_FORM_HUM ? -1 : sse->var->number);

            fprintf(fp, " %s:", name);
            write_val(fp, format, FALSE, sse->weight);      

            if (++cnt % 6 == 0)
               fputc('\n', fp);         
         }
         if (cnt % 6 != 0)
            fputc('\n', fp);         
      }
   }
   fprintf(fp, "End\n");

   free(name);
   free(contab);
}   
Beispiel #18
0
static void write_row(
   FILE*      fp,
   LpFormat   format,
   const Con* con,
   char*      name,
   int        name_size)
{
   Nzo*  nzo;
   Nzo** nzotab;
   int   cnt;
   int   i;

   assert(fp        != NULL);
   assert(con       != NULL);
   assert(name      != NULL);

   /* Add 1 in case con->size == 0
    */
   nzotab = calloc((size_t)con->size + 1, sizeof(*con));

   assert(nzotab != NULL);

   for(cnt = 0, nzo = con->first; nzo != NULL; nzo = nzo->con_next)
      nzotab[cnt++] = nzo;

   assert(cnt == con->size);

   if (format == LP_FORM_RLP)
      permute(con->size, (void**)nzotab);

   cnt = 0;

   for(i = 0; i < con->size; i++)
   {
      nzo = nzotab[i];

      lps_makename(name, name_size, nzo->var->name, format == LP_FORM_HUM ? -1 : nzo->var->number);

      if (mpq_equal(nzo->value, const_one))
         fprintf(fp, " + %s", name);
      else if (mpq_equal(nzo->value, const_minus_one))
         fprintf(fp, " - %s", name);
      else
      {
         fprintf(fp, " ");
         write_val(fp, format, TRUE, nzo->value);      
         fprintf(fp, " %s", name);
      }
      if (++cnt % 6 == 0)
         fprintf(fp, "\n ");         
   }
   if (con->qme_first != NULL)
   {
      Qme* qme;

      if (cnt % 6 != 0)
         fprintf(fp, "\n ");         

      cnt = 0;

      if (format == LP_FORM_LPF || format == LP_FORM_RLP)
         fprintf(fp, " + [");

      for(qme = con->qme_first; qme != NULL; qme= qme->next)
      {
         lps_makename(name, name_size, qme->var1->name, format == LP_FORM_HUM ? -1 : qme->var1->number);

         if (mpq_equal(qme->value, const_one))
            fprintf(fp, " + %s", name);
         else if (mpq_equal(qme->value, const_minus_one))
            fprintf(fp, " - %s", name);
         else
         {
            fprintf(fp, " ");         
            write_val(fp, format, TRUE, qme->value);      
            fprintf(fp, " %s", name);
         }

         if (qme->var1 == qme->var2)
            fprintf(fp, "^2");
         else
         {
            lps_makename(name, name_size, qme->var2->name, format == LP_FORM_HUM ? -1 : qme->var2->number);

            fprintf(fp, " * %s", name);
         }
         if (++cnt % 6 == 0)
            fprintf(fp, "\n ");         
      }
      if (format == LP_FORM_LPF || format == LP_FORM_RLP)
      {
         fprintf(fp,  " ]\n");
         cnt = 0;
      }
   }
   if (con->term != NULL)
   {
      const Term* term  = con->term;
      Bool only_comment = FALSE;

      assert(term_get_degree(term) > 2 || !term_is_polynomial(term));

      if (format != LP_FORM_PIP)
      {
         if (verbose > 0)
         {
            fprintf(stderr, "--- Warning 600: File format can only handle linear and quadratic constraints\n");
            fprintf(stderr, "                 Constraint %s with degree %d ignored\n", 
               con->name, term_get_degree(term));
         }
         only_comment = TRUE;
      }
      assert(numb_equal(term_get_constant(term), numb_zero()));
   
      if (cnt % 6 != 0)
         fprintf(fp, "\n ");         
   
      cnt = 0;
   
      if (only_comment)
         fprintf(fp, "\\ ");

      for(i = 0; i < term_get_elements(term); i++)
      {
         const Mono* mono  = term_get_element(term, i);
         const Numb* coeff = mono_get_coeff(mono);
         MFun        fun   = mono_get_function(mono);
         int         k;

         if (fun == MFUN_NONE)
         {
            if (numb_equal(coeff, numb_one()))
               fprintf(fp, " +");
            else
            {
               mpq_t t;
               mpq_init(t);
               numb_get_mpq(coeff, t);
               fprintf(fp, " ");         
               write_val(fp, format, TRUE, t);      
               mpq_clear(t);
            }
            fputc(' ', fp);
         }
         else
         {
            switch(fun)
            {
            case MFUN_SQRT :
               fprintf(fp, " + sqrt(");
               break;
            case MFUN_LOG :
               fprintf(fp, " + log(");
               break;
            case MFUN_EXP :
               fprintf(fp, " + exp(");
               break;
            case MFUN_LN :
               fprintf(fp, " + ln(");
               break;
            case MFUN_SIN :
               fprintf(fp, " + sin(");
               break;
            case MFUN_COS :
               fprintf(fp, " + cos(");
               break;
            case MFUN_TAN :
               fprintf(fp, " + tan(");
               break;
            case MFUN_ABS :
               fprintf(fp, " + abs(");
               break;
            case MFUN_SGN :
               fprintf(fp, " + sgn(");
               break;
            case MFUN_POW :
               fprintf(fp, " + pow(");
               break;
            case MFUN_SGNPOW :
               fprintf(fp, " + sgnpow(");
               break;
            case MFUN_TRUE :
            case MFUN_FALSE :
            default :
               abort();
            } 
         }
          
         for(k = 0; k < mono_get_degree(mono); k++)
         {
            Var* var = mono_get_var(mono, k);
            int  j;
            
            if (k > 0)
               fprintf(fp, " * ");
            
            for(j = 1; k + j < mono_get_degree(mono); j++)
               if (var != mono_get_var(mono, k + j))
                  break;
               
            lps_makename(name, name_size, var->name, format == LP_FORM_HUM ? -1 : var->number);
               
            if (j == 1)
               fprintf(fp, "%s", name);
            else
            {
               fprintf(fp, "%s^%d", name, j);
               k += j - 1; /*lint !e850 loop index variable is modified in body of the loop */
            }
         }
         if (fun != MFUN_NONE)
         {
            if (fun == MFUN_POW || fun == MFUN_SGNPOW)
            {
               mpq_t t;
               mpq_init(t);
               numb_get_mpq(coeff, t);
               fprintf(fp, " ,");         
               write_val(fp, format, FALSE, t);      
               mpq_clear(t);
            }
            fprintf(fp, ") ");
         }
         if (++cnt % 6 == 0)
            fprintf(fp, "\n%s ", only_comment ? "\\" : "");         
      }
   }
   free(nzotab);
}