Solution for closest string 2

This commit is contained in:
hal8174 2025-01-13 20:45:28 +01:00
parent 162759abcb
commit 8c1aa032d5
13 changed files with 511707 additions and 54 deletions

View file

@ -51,6 +51,12 @@ dependencies = [
"windows-sys",
]
[[package]]
name = "anyhow"
version = "1.0.95"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34ac096ce696dc2fcabef30516bb13c0a68a11d30131d3df6f04711467681b04"
[[package]]
name = "clap"
version = "4.5.26"
@ -82,7 +88,7 @@ dependencies = [
"heck",
"proc-macro2",
"quote",
"syn",
"syn 2.0.96",
]
[[package]]
@ -97,8 +103,8 @@ version = "0.1.0"
dependencies = [
"clap",
"good_lp",
"gurobi",
"itertools 0.14.0",
"grb",
"itertools",
]
[[package]]
@ -126,6 +132,47 @@ version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990"
[[package]]
name = "cstr-enum"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "64cf55825ac0b0e8120bdd2cd8f4849ff8f59d049e6808168a4c0853cc3813fe"
dependencies = [
"cstr-enum-derive",
]
[[package]]
name = "cstr-enum-derive"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ea5208e0c141483a14e4a1844b2859a410cef6a00a6c5fbf58bd1e791e31144e"
dependencies = [
"proc-macro2",
"quote",
"syn 1.0.109",
]
[[package]]
name = "csv"
version = "1.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "acdc4883a9c96732e4733212c01447ebd805833b7275a73ca3ee080fd77afdaf"
dependencies = [
"csv-core",
"itoa",
"ryu",
"serde",
]
[[package]]
name = "csv-core"
version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5efa2b3d7902f4b634a20cae3c9c4e6209dc4779feb6863329607560143efa70"
dependencies = [
"memchr",
]
[[package]]
name = "either"
version = "1.13.0"
@ -149,20 +196,42 @@ dependencies = [
]
[[package]]
name = "gurobi"
version = "0.3.4"
name = "grb"
version = "2.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6f612f4124936819b6cd1a178cbd971df8e77b7b40aa6afacb21e1b232524dbb"
checksum = "a3138eda01bdaddfd22f1e94b792229691a5cf1a4f533bc62e954e57ee6f5bb4"
dependencies = [
"gurobi-sys",
"itertools 0.4.19",
"anyhow",
"cstr-enum",
"csv",
"fnv",
"grb-macro",
"grb-sys2",
"proc-macro2",
"quote",
"serde",
"serde_json",
]
[[package]]
name = "gurobi-sys"
version = "0.3.0"
name = "grb-macro"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "27abdfeb504168901df67a7ab5f217501ada52d78a882d74be8920b6eeeabbc9"
checksum = "70071e86d36b20a3f2280ef419a0efe9aa8c85b79c318b28a2b707e1ed52b0d0"
dependencies = [
"proc-macro2",
"quote",
"syn 1.0.109",
]
[[package]]
name = "grb-sys2"
version = "10.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8915cdcdcefacae8b07a25984e5b80d2a31fc71db8a97deb1715fd1659b1bfc5"
dependencies = [
"anyhow",
]
[[package]]
name = "heck"
@ -176,12 +245,6 @@ version = "1.70.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf"
[[package]]
name = "itertools"
version = "0.4.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4a9b56eb56058f43dc66e58f40a214b2ccbc9f3df51861b63d51dec7b65bc3f"
[[package]]
name = "itertools"
version = "0.14.0"
@ -191,12 +254,24 @@ dependencies = [
"either",
]
[[package]]
name = "itoa"
version = "1.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674"
[[package]]
name = "lazy_static"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
[[package]]
name = "memchr"
version = "2.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
[[package]]
name = "pkg-config"
version = "0.3.31"
@ -221,12 +296,61 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "ryu"
version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f"
[[package]]
name = "serde"
version = "1.0.217"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "02fc4265df13d6fa1d00ecff087228cc0a2b5f3c0e87e258d8b94a156e984c70"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.217"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a9bf7cf98d04a2b28aead066b7496853d4779c9cc183c440dbac457641e19a0"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.96",
]
[[package]]
name = "serde_json"
version = "1.0.135"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b0d7ba2887406110130a978386c4e1befb98c674b4fba677954e4db976630d9"
dependencies = [
"itoa",
"memchr",
"ryu",
"serde",
]
[[package]]
name = "strsim"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
[[package]]
name = "syn"
version = "1.0.109"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "syn"
version = "2.0.96"

View file

@ -6,5 +6,5 @@ edition = "2021"
[dependencies]
clap = { version = "4.5.26", features = ["derive"] }
good_lp = "1.11.0"
gurobi = "0.3.4"
grb = "2.0.3"
itertools = "0.14.0"

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1 @@
BBDFGDCGBBF

View file

@ -0,0 +1 @@
csafydvimgvnrhraodeieklynoxgajbjhhsvntdfkvuptfuonsdkyxqculmterhvbwrdytoeggorionubonlkcafauvrfyfinexf

View file

@ -0,0 +1 @@
csaaydvimgenfhraodeixklynorgajxjhhsvgtdfkvupnjuonsdfylqculmterhqbwsdytoeggorienubombkcafauvrsyfiunxfydutnfhomxvqneunkzpdmurffyqtjyncxkzzklifrpopskobxshsrirnmarylsxffdunbparpzhswqdcbxsjrqwmnnmtwoedfbunwvwikywvpgygashfpvxxldndrbibdoobjceojyhnfmaxxbheyqdddahbdluisejzdturigzhsjwpntxcriqsbbvuqtphjmgmuqqksrclcrmbrlihzpcayatgyhiddwjccnzyibwpigwajqgotdyzjxrjmpqlagfpikakqtiplyasxnglbwieemfvqhjitddujbimxcwykyokxbyfhakpdywutxfrjnyztzeefpazptwbsysfndbzpedbrxfkhrahgxmztnhqcmojshiajwbbegfmpehuawrczvhqrwohigzalcbvyvjkshtgmpmberurbjxzgzysnvqxkknrdryhfnvxlbzntrzmypofoefjowrrrlltgwxvdanbfbyijjzeoxcqbvjlcbdabzwkodtimuapgkgbahmwyglchntycbjttbviiihzfuvjuoohcjpuriotvylnufyziqgyaihlxujdtcyjgrrnmrqhooljsyycrkqexwimkruninplbapzdlpuwtgdvmqcjadhibklmnvcaaeyncbttubnbziblidaspkcvbrzzzpmpvlqixrhzwjfumoeaxrcdpypvcjivzegljbcurkyqdijhubnxvidzhfdzxcbppxuxbjvzcicpordjzcljnijvzjiedngrlrcbgfsjqlztqexmafjhloscgsdkxsejvwnvfzxnfldsurnkstjopgshygxrolcwoxkqlbhrfjmzkzttwhsvbmtrbcdriplrxfncwljctgvmlwhghqpnxyawqgkpzwrjeoxqfiofrmv

View file

@ -0,0 +1 @@
mjyhvvxwfnmfqkiehyqoujfpezjtfnusibfwvqqdtywxfgbyrsaevvkteupsjebatbqdjemjoneygrhqswzzijeqdourkpbdhjhkzjvezughojeyktyssyudchhlitpjjvzkqpqlmvwfjspopxgudwwcumsrbfxmoevpplligxwlehgoqbszxrgpjohrurlfxjzuvxmasyzlzdorckgfqgoeanrpavqexopwslgcflgmepmkhzouiajxbcmagptxaytgieqruivqfsbpruugatoexyegbvisemmgjipgaygrcgmligjoksbhamlbvxbjclfwdqkcoeschpeqexnwohcxybiuukoyhewemsjkqcgfcsdjlvzkknbbvlkfxentestcrdresjxuijwlwilsaqynzwseieixrbknoyloivyvccxwplomopgxlbankjsxuqadsxtltkcndielbomecqrffdegahjoassbfakiwmkvzwdwwyaudzrbpsozeawyalmunxnbuyddwztgbknhoseojxhhkmcuwmmgteqbyifksaujqrclzxpmhcjcrskjievbtjjvzpyqoxsaavznbtrnonrgmyqlbpuaehodxatwpyccclemnshpmkcyfysttfzfqgyiaujawjxoioujxthzyaqwewwafbsbjecadkmjqlrrftvpiedngujblwxlfnmdvtqsjnsjhcatruetbywndpccorxulqbnzbhqfknzyxnmmfqueqckflqaputwcthcliwpgygynssylwvqnebvrovsjewsixsyhlpbbxdeomdzljmrbkntreemdwolbpfysselzqbywdhaquimrdoszxxkieybpkoxgyagsgaesmnuimriphupxhetzkthdgcavipapofgyezwbmzucokaeeqqisuzwtskosxmixgscwqnhemwzlgvecabqzcvfvrjmudrlaapxrjandtbniysjxghgheytscdtsqz

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -1,8 +1,13 @@
use std::path::{Path, PathBuf};
use std::{
collections::HashMap,
path::{Path, PathBuf},
};
use clap::Parser;
use gurobi::*;
use expr::LinExpr;
use grb::*;
use itertools::Itertools;
use ModelSense::Minimize;
fn read_input(filename: impl AsRef<Path>) -> Vec<Vec<u8>> {
let text = std::fs::read_to_string(filename).unwrap();
@ -19,9 +24,10 @@ fn read_input(filename: impl AsRef<Path>) -> Vec<Vec<u8>> {
#[derive(Debug, Parser)]
struct Args {
filename: PathBuf,
outfilename: PathBuf,
}
fn solve(input: &[Vec<u8>], d: usize) {
fn solve(input: &[Vec<u8>]) -> String {
let mut columns = Vec::new();
for i in 0..input[0].len() {
@ -42,59 +48,77 @@ fn solve(input: &[Vec<u8>], d: usize) {
let counts = columns.iter().counts();
let env = Env::new("logfile.log").unwrap();
let mut model = Model::new("model1").unwrap();
let mut model = Model::new("model1", &env).unwrap();
let mut variables = HashMap::new();
let mut variables = Vec::new();
for (k, &c) in &counts {
for (&k, &c) in &counts {
let mut v = Vec::new();
let mut sum = LinExpr::new();
let mut sum = Expr::Linear(LinExpr::new());
for i in 0..=(k.iter().max().copied().unwrap()) {
let var = model
.add_var(
&format!("{:?}:{}", k, i),
Integer,
0.0,
-INFINITY,
INFINITY,
&[],
&[],
)
.unwrap();
let var = add_intvar!(model, name: &format!("{:?}:{}", k, i), bounds: 0..).unwrap();
sum = sum + &var;
sum = sum + var;
v.push(var);
}
model
.add_constr(&format!("{:?}", k), sum, Equal, c as f64)
.unwrap();
variables.push(v);
model.add_constr(&format!("{:?}", k), c!(sum == c)).unwrap();
variables.insert(k, v);
}
let d = add_intvar!(model, name: "D", bounds: 0..).unwrap();
for i in 0..input.len() {
let mut sum = LinExpr::new();
let mut sum = Expr::Linear(LinExpr::new());
for (l, (k, _)) in counts.iter().enumerate() {
for j in 0..input.len() {
if k[i] != k[j] {
sum = sum + &variables[l][k[j] as usize];
for &k in counts.keys() {
for j in 0..=*k.iter().max().unwrap() {
if k[i] != j {
sum = sum + variables[k][j as usize];
}
}
}
model
.add_constr(&format!("{:?}", i), sum, Less, d as f64)
.unwrap();
model.add_constr(&format!("{:?}", i), c!(sum <= d)).unwrap();
}
model.set_objective(d, Minimize).unwrap();
model.update().unwrap();
// model.write("test.lp").unwrap();
model.optimize().unwrap();
dbg!(model.status().unwrap());
// for vars in &variables {
// for v in vars {
// dbg!(model.get_obj_attr(attr::X, v).unwrap());
// }
// }
let mut result = String::new();
for (i, col) in columns.iter().enumerate() {
let t = model
.get_obj_attr_batch(attr::X, variables[col].clone())
.unwrap();
let mut p = columns[0..i].iter().filter(|&c| c == col).count();
let mut j = 0;
while t[j] as usize <= p {
p -= t[j] as usize;
j += 1;
}
let l = col.iter().position(|&c| c as usize == j).unwrap();
// dbg!(i, col, t, j, l, char::from_u32(input[l][i] as u32).unwrap());
result.push(char::from_u32(input[l][i] as u32).unwrap());
}
result
}
fn main() {
@ -102,5 +126,7 @@ fn main() {
let input = read_input(args.filename);
solve(&input, 6);
let r = solve(&input);
std::fs::write(args.outfilename, r).unwrap();
}

View file

@ -0,0 +1,4 @@
3
ABCFGDCGEBA
BBDAGDBGBEC
BAEFCACGBBF

File diff suppressed because it is too large Load diff

1
closest_string_assignment_2/validator.py Normal file → Executable file
View file

@ -1,3 +1,4 @@
#!/usr/bin/env python
"""
usage: python3 validator.py path/to/input path/to/output
"""