int cmd_list(int argc, char **argv, struct mpd_connection *conn) { enum mpd_tag_type type = get_search_type(argv[0]); if (type == MPD_TAG_UNKNOWN) return -1; --argc; ++argv; mpd_search_db_tags(conn, type); if (argc > 0 && !add_constraints(argc, argv, conn)) return -1; if (!mpd_search_commit(conn)) printErrorAndExit(conn); struct mpd_pair *pair; while ((pair = mpd_recv_pair_tag(conn, type)) != NULL) { printf("%s\n", charset_from_utf8(pair->value)); mpd_return_pair(conn, pair); } my_finishCommand(conn); return 0; }
int cmd_findadd(int argc, char **argv, struct mpd_connection *conn) { mpd_search_add_db_songs(conn, true); if (!add_constraints(argc, argv, conn)) return -1; if (!mpd_search_commit(conn)) printErrorAndExit(conn); my_finishCommand(conn); return 0; }
static int do_search(int argc, char ** argv, struct mpd_connection *conn, bool exact) { mpd_search_db_songs(conn, exact); if (!add_constraints(argc, argv, conn)) return -1; if (!mpd_search_commit(conn)) printErrorAndExit(conn); print_entity_list(conn, MPD_ENTITY_TYPE_SONG); my_finishCommand(conn); return 0; }
static int do_search ( int argc, char ** argv, struct mpd_connection *conn, int exact ) { mpd_search_db_songs(conn, exact); if (!add_constraints(argc, argv, conn)) return -1; if (!mpd_search_commit(conn)) printErrorAndExit(conn); print_filenames(conn); my_finishCommand(conn); return 0; }
void terrain_builder::parse_mapstring(const std::string &mapstring, struct building_rule &br, anchormap& anchors, const config& global_images) { const t_translation::t_map map = t_translation::read_builder_map(mapstring); // If there is an empty map leave directly. // Determine after conversion, since a // non-empty string can return an empty map. if(map.empty()) { return; } int lineno = (map[0][0] == t_translation::NONE_TERRAIN) ? 1 : 0; int x = lineno; int y = 0; for(size_t y_off = 0; y_off < map.size(); ++y_off) { for(size_t x_off = x; x_off < map[y_off].size(); ++x_off) { const t_translation::t_terrain terrain = map[y_off][x_off]; if(terrain.base == t_translation::TB_DOT) { // Dots are simple placeholders, // which do not represent actual terrains. } else if (terrain.overlay != 0 ) { anchors.insert(std::pair<int, map_location>(terrain.overlay, map_location(x, y))); } else if (terrain.base == t_translation::TB_STAR) { add_constraints(br.constraints, map_location(x, y), t_translation::STAR, global_images); } else { ERR_NG << "Invalid terrain (" << t_translation::write_terrain_code(terrain) << ") in builder map\n"; assert(false); return; } x += 2; } if(lineno % 2 == 1) { ++y; x = 0; } else { x = 1; } ++lineno; } }
void terrain_builder::add_constraints(terrain_builder::constraint_set &constraints, const map_location& loc, const config& cfg, const config& global_images) { add_constraints(constraints, loc, t_translation::t_match(cfg["type"], t_translation::WILDCARD), global_images); terrain_constraint& constraint = constraints[loc]; std::vector<std::string> item_string = utils::split(cfg["set_flag"]); constraint.set_flag.insert(constraint.set_flag.end(), item_string.begin(), item_string.end()); item_string = utils::split(cfg["has_flag"]); constraint.has_flag.insert(constraint.has_flag.end(), item_string.begin(), item_string.end()); item_string = utils::split(cfg["no_flag"]); constraint.no_flag.insert(constraint.no_flag.end(), item_string.begin(), item_string.end()); add_images_from_config(constraint.images, cfg, false); }
static void solve(char* file_name) { ppl_Constraint_System_t ppl_cs; #ifndef NDEBUG ppl_Constraint_System_t ppl_cs_copy; #endif ppl_Generator_t optimum_location; ppl_Linear_Expression_t ppl_le; int dimension, row, num_rows, column, nz, i, j, type; int* coefficient_index; double lb, ub; double* coefficient_value; mpq_t rational_lb, rational_ub; mpq_t* rational_coefficient; mpq_t* objective; ppl_Linear_Expression_t ppl_objective_le; ppl_Coefficient_t optimum_n; ppl_Coefficient_t optimum_d; mpq_t optimum; mpz_t den_lcm; int optimum_found; glp_mpscp glpk_mpscp; glpk_lp = glp_create_prob(); glp_init_mpscp(&glpk_mpscp); if (verbosity == 0) { /* FIXME: find a way to suppress output from glp_read_mps. */ } #ifdef PPL_LPSOL_SUPPORTS_TIMINGS if (print_timings) start_clock(); #endif /* defined(PPL_LPSOL_SUPPORTS_TIMINGS) */ if (glp_read_mps(glpk_lp, GLP_MPS_FILE, &glpk_mpscp, file_name) != 0) fatal("cannot read MPS file `%s'", file_name); #ifdef PPL_LPSOL_SUPPORTS_TIMINGS if (print_timings) { fprintf(stderr, "Time to read the input file: "); print_clock(stderr); fprintf(stderr, " s\n"); start_clock(); } #endif /* defined(PPL_LPSOL_SUPPORTS_TIMINGS) */ glpk_lp_num_int = glp_get_num_int(glpk_lp); if (glpk_lp_num_int > 0 && !no_mip && !use_simplex) fatal("the enumeration solving method can not handle MIP problems"); dimension = glp_get_num_cols(glpk_lp); /* Read variables constrained to be integer. */ if (glpk_lp_num_int > 0 && !no_mip && use_simplex) { if (verbosity >= 4) fprintf(output_file, "Integer variables:\n"); integer_variables = (ppl_dimension_type*) malloc((glpk_lp_num_int + 1)*sizeof(ppl_dimension_type)); for (i = 0, j = 0; i < dimension; ++i) { int col_kind = glp_get_col_kind(glpk_lp, i+1); if (col_kind == GLP_IV || col_kind == GLP_BV) { integer_variables[j] = i; if (verbosity >= 4) { ppl_io_fprint_variable(output_file, i); fprintf(output_file, " "); } ++j; } } } coefficient_index = (int*) malloc((dimension+1)*sizeof(int)); coefficient_value = (double*) malloc((dimension+1)*sizeof(double)); rational_coefficient = (mpq_t*) malloc((dimension+1)*sizeof(mpq_t)); ppl_new_Constraint_System(&ppl_cs); mpq_init(rational_lb); mpq_init(rational_ub); for (i = 1; i <= dimension; ++i) mpq_init(rational_coefficient[i]); mpz_init(den_lcm); if (verbosity >= 4) fprintf(output_file, "\nConstraints:\n"); /* Set up the row (ordinary) constraints. */ num_rows = glp_get_num_rows(glpk_lp); for (row = 1; row <= num_rows; ++row) { /* Initialize the least common multiple computation. */ mpz_set_si(den_lcm, 1); /* Set `nz' to the number of non-zero coefficients. */ nz = glp_get_mat_row(glpk_lp, row, coefficient_index, coefficient_value); for (i = 1; i <= nz; ++i) { set_mpq_t_from_double(rational_coefficient[i], coefficient_value[i]); /* Update den_lcm. */ mpz_lcm(den_lcm, den_lcm, mpq_denref(rational_coefficient[i])); } lb = glp_get_row_lb(glpk_lp, row); ub = glp_get_row_ub(glpk_lp, row); set_mpq_t_from_double(rational_lb, lb); set_mpq_t_from_double(rational_ub, ub); mpz_lcm(den_lcm, den_lcm, mpq_denref(rational_lb)); mpz_lcm(den_lcm, den_lcm, mpq_denref(rational_ub)); ppl_new_Linear_Expression_with_dimension(&ppl_le, dimension); for (i = 1; i <= nz; ++i) { mpz_mul(tmp_z, den_lcm, mpq_numref(rational_coefficient[i])); mpz_divexact(tmp_z, tmp_z, mpq_denref(rational_coefficient[i])); ppl_assign_Coefficient_from_mpz_t(ppl_coeff, tmp_z); ppl_Linear_Expression_add_to_coefficient(ppl_le, coefficient_index[i]-1, ppl_coeff); } type = glp_get_row_type(glpk_lp, row); add_constraints(ppl_le, type, rational_lb, rational_ub, den_lcm, ppl_cs); ppl_delete_Linear_Expression(ppl_le); } free(coefficient_value); for (i = 1; i <= dimension; ++i) mpq_clear(rational_coefficient[i]); free(rational_coefficient); free(coefficient_index); #ifndef NDEBUG ppl_new_Constraint_System_from_Constraint_System(&ppl_cs_copy, ppl_cs); #endif /* FIXME: here we could build the polyhedron and minimize it before adding the variable bounds. */ /* Set up the columns constraints, i.e., variable bounds. */ for (column = 1; column <= dimension; ++column) { lb = glp_get_col_lb(glpk_lp, column); ub = glp_get_col_ub(glpk_lp, column); set_mpq_t_from_double(rational_lb, lb); set_mpq_t_from_double(rational_ub, ub); /* Initialize the least common multiple computation. */ mpz_set_si(den_lcm, 1); mpz_lcm(den_lcm, den_lcm, mpq_denref(rational_lb)); mpz_lcm(den_lcm, den_lcm, mpq_denref(rational_ub)); ppl_new_Linear_Expression_with_dimension(&ppl_le, dimension); ppl_assign_Coefficient_from_mpz_t(ppl_coeff, den_lcm); ppl_Linear_Expression_add_to_coefficient(ppl_le, column-1, ppl_coeff); type = glp_get_col_type(glpk_lp, column); add_constraints(ppl_le, type, rational_lb, rational_ub, den_lcm, ppl_cs); ppl_delete_Linear_Expression(ppl_le); } mpq_clear(rational_ub); mpq_clear(rational_lb); /* Deal with the objective function. */ objective = (mpq_t*) malloc((dimension+1)*sizeof(mpq_t)); /* Initialize the least common multiple computation. */ mpz_set_si(den_lcm, 1); mpq_init(objective[0]); set_mpq_t_from_double(objective[0], glp_get_obj_coef(glpk_lp, 0)); for (i = 1; i <= dimension; ++i) { mpq_init(objective[i]); set_mpq_t_from_double(objective[i], glp_get_obj_coef(glpk_lp, i)); /* Update den_lcm. */ mpz_lcm(den_lcm, den_lcm, mpq_denref(objective[i])); } /* Set the ppl_objective_le to be the objective function. */ ppl_new_Linear_Expression_with_dimension(&ppl_objective_le, dimension); /* Set value for objective function's inhomogeneous term. */ mpz_mul(tmp_z, den_lcm, mpq_numref(objective[0])); mpz_divexact(tmp_z, tmp_z, mpq_denref(objective[0])); ppl_assign_Coefficient_from_mpz_t(ppl_coeff, tmp_z); ppl_Linear_Expression_add_to_inhomogeneous(ppl_objective_le, ppl_coeff); /* Set values for objective function's variable coefficients. */ for (i = 1; i <= dimension; ++i) { mpz_mul(tmp_z, den_lcm, mpq_numref(objective[i])); mpz_divexact(tmp_z, tmp_z, mpq_denref(objective[i])); ppl_assign_Coefficient_from_mpz_t(ppl_coeff, tmp_z); ppl_Linear_Expression_add_to_coefficient(ppl_objective_le, i-1, ppl_coeff); } if (verbosity >= 4) { fprintf(output_file, "Objective function:\n"); if (mpz_cmp_si(den_lcm, 1) != 0) fprintf(output_file, "("); ppl_io_fprint_Linear_Expression(output_file, ppl_objective_le); } for (i = 0; i <= dimension; ++i) mpq_clear(objective[i]); free(objective); if (verbosity >= 4) { if (mpz_cmp_si(den_lcm, 1) != 0) { fprintf(output_file, ")/"); mpz_out_str(output_file, 10, den_lcm); } fprintf(output_file, "\n%s\n", (maximize ? "Maximizing." : "Minimizing.")); } ppl_new_Coefficient(&optimum_n); ppl_new_Coefficient(&optimum_d); ppl_new_Generator_zero_dim_point(&optimum_location); optimum_found = use_simplex ? solve_with_simplex(ppl_cs, ppl_objective_le, optimum_n, optimum_d, optimum_location) : solve_with_generators(ppl_cs, ppl_objective_le, optimum_n, optimum_d, optimum_location); ppl_delete_Linear_Expression(ppl_objective_le); if (glpk_lp_num_int > 0) free(integer_variables); if (optimum_found) { mpq_init(optimum); ppl_Coefficient_to_mpz_t(optimum_n, tmp_z); mpq_set_num(optimum, tmp_z); ppl_Coefficient_to_mpz_t(optimum_d, tmp_z); mpz_mul(tmp_z, tmp_z, den_lcm); mpq_set_den(optimum, tmp_z); if (verbosity == 1) fprintf(output_file, "Optimized problem.\n"); if (verbosity >= 2) fprintf(output_file, "Optimum value: %.10g\n", mpq_get_d(optimum)); if (verbosity >= 3) { fprintf(output_file, "Optimum location:\n"); ppl_Generator_divisor(optimum_location, ppl_coeff); ppl_Coefficient_to_mpz_t(ppl_coeff, tmp_z); for (i = 0; i < dimension; ++i) { mpz_set(mpq_denref(tmp1_q), tmp_z); ppl_Generator_coefficient(optimum_location, i, ppl_coeff); ppl_Coefficient_to_mpz_t(ppl_coeff, mpq_numref(tmp1_q)); ppl_io_fprint_variable(output_file, i); fprintf(output_file, " = %.10g\n", mpq_get_d(tmp1_q)); } } #ifndef NDEBUG { ppl_Polyhedron_t ph; unsigned int relation; ppl_new_C_Polyhedron_recycle_Constraint_System(&ph, ppl_cs_copy); ppl_delete_Constraint_System(ppl_cs_copy); relation = ppl_Polyhedron_relation_with_Generator(ph, optimum_location); ppl_delete_Polyhedron(ph); assert(relation == PPL_POLY_GEN_RELATION_SUBSUMES); } #endif maybe_check_results(PPL_MIP_PROBLEM_STATUS_OPTIMIZED, mpq_get_d(optimum)); mpq_clear(optimum); } ppl_delete_Constraint_System(ppl_cs); ppl_delete_Coefficient(optimum_d); ppl_delete_Coefficient(optimum_n); ppl_delete_Generator(optimum_location); glp_delete_prob(glpk_lp); }
void terrain_builder::parse_config(const config &cfg, bool local) { log_scope("terrain_builder::parse_config"); // Parses the list of building rules (BRs) foreach (const config &br, cfg.child_range("terrain_graphics")) { building_rule pbr; // Parsed Building rule pbr.local = local; // add_images_from_config(pbr.images, **br); if(!br["x"].empty() && !br["y"].empty()) pbr.location_constraints = map_location(atoi(br["x"].c_str()) - 1, atoi(br["y"].c_str()) - 1); pbr.probability = br["probability"].empty() ? -1 : atoi(br["probability"].c_str()); // Mapping anchor indices to anchor locations. anchormap anchors; // Parse the map= , if there is one (and fill the anchors list) parse_mapstring(br["map"], pbr, anchors, br); // Parses the terrain constraints (TCs) foreach (const config &tc, br.child_range("tile")) { // Adds the terrain constraint to the current built terrain's list // of terrain constraints, if it does not exist. map_location loc; if (!tc["x"].empty()) { loc.x = atoi(tc["x"].c_str()); } if (!tc["y"].empty()) { loc.y = atoi(tc["y"].c_str()); } if (!tc["loc"].empty()) { std::vector<std::string> sloc = utils::split(tc["loc"]); if(sloc.size() == 2) { loc.x = atoi(sloc[0].c_str()); loc.y = atoi(sloc[1].c_str()); } } if(loc.valid()) { add_constraints(pbr.constraints, loc, tc, br); } if (!tc["pos"].empty()) { int pos = atoi(tc["pos"].c_str()); if(anchors.find(pos) == anchors.end()) { WRN_NG << "Invalid anchor!\n"; continue; } std::pair<anchormap::const_iterator, anchormap::const_iterator> range = anchors.equal_range(pos); for(; range.first != range.second; ++range.first) { loc = range.first->second; add_constraints(pbr.constraints, loc, tc, br); } } } const std::vector<std::string> global_set_flag = utils::split(br["set_flag"]); const std::vector<std::string> global_no_flag = utils::split(br["no_flag"]); const std::vector<std::string> global_has_flag = utils::split(br["has_flag"]); for(constraint_set::iterator constraint = pbr.constraints.begin(); constraint != pbr.constraints.end(); ++constraint) { if(global_set_flag.size()) constraint->second.set_flag.insert(constraint->second.set_flag.end(), global_set_flag.begin(), global_set_flag.end()); if(global_no_flag.size()) constraint->second.no_flag.insert(constraint->second.no_flag.end(), global_no_flag.begin(), global_no_flag.end()); if(global_has_flag.size()) constraint->second.has_flag.insert(constraint->second.has_flag.end(), global_has_flag.begin(), global_has_flag.end()); } // Handles rotations const std::string &rotations = br["rotations"]; int precedence = lexical_cast_default<int>(br["precedence"],0); add_rotated_rules(building_rules_, pbr, precedence, rotations); } // Debug output for the terrain rules #if 0 std::cerr << "Built terrain rules: \n"; building_ruleset::const_iterator rule; for(rule = building_rules_.begin(); rule != building_rules_.end(); ++rule) { std::cerr << ">> New rule: image_background = " << "\n>> Location " << rule->second.location_constraints << "\n>> Probability " << rule->second.probability for(constraint_set::const_iterator constraint = rule->second.constraints.begin(); constraint != rule->second.constraints.end(); ++constraint) { std::cerr << ">>>> New constraint: location = (" << constraint->second.loc << "), terrain types = '" << t_translation::write_list(constraint->second.terrain_types_match.terrain) << "'\n"; std::vector<std::string>::const_iterator flag; for(flag = constraint->second.set_flag.begin(); flag != constraint->second.set_flag.end(); ++flag) { std::cerr << ">>>>>> Set_flag: " << *flag << "\n"; } for(flag = constraint->second.no_flag.begin(); flag != constraint->second.no_flag.end(); ++flag) { std::cerr << ">>>>>> No_flag: " << *flag << "\n"; } } } #endif }