Skip to content

Commit 4c8350d

Browse files
committed
bump good_lp and bug fix
1 parent 2a291e2 commit 4c8350d

File tree

8 files changed

+111
-30
lines changed

8 files changed

+111
-30
lines changed

Cargo.toml

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ documentation = "https://rooc.specy.app/docs/rooc"
77
description = "A mixed integer linear programming modeling language to solve linear optimization models. Extensible, works in WASM and easy to use."
88
keywords = ["optimization", "milp", "solver", "mathematics", "linear-programming"]
99
categories = ["mathematics", "algorithms", "science"]
10-
version = "0.1.18"
10+
version = "0.1.19"
1111
license = "GPL-3.0"
1212
edition = "2021"
1313

@@ -23,9 +23,10 @@ pest_derive = { version = "2.7.14", features = ["grammar-extras"] }
2323
lazy_static = "1.5.0"
2424
copper = "0.1.0"
2525
indexmap = { version = "2.6.0", features = ["serde"] }
26-
good_lp = { version = "1.12.0", features = ["clarabel-wasm"], default-features = false }
26+
good_lp = { version = "1.13.0", features = ["clarabel"], default-features = false }
2727
microlp = "0.2.11"
2828
log = "0.4.21"
29+
utf8_slice = "1.0.0"
2930

3031
[target.'cfg(target_family = "wasm")'.dev-dependencies]
3132
wasm-bindgen-test = "0.3.0"

src/main.rs

+13-15
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,20 @@ use rooc::pipe::{StepByStepSimplexPipe, TableauPipe};
99
#[allow(unused)]
1010
fn main() {
1111
let source = r#"
12-
/*
13-
Example model, look at the docs
14-
for more info https://rooc.specy.app/docs/rooc
15-
*/
16-
max x
12+
13+
max 2x_1 + x_2 - x_3
1714
subject to
18-
//write the constraints here
19-
x <= y
20-
where
21-
// write the constants here
22-
let y = 10
23-
let list = [2,4,6]
24-
define
25-
// define the model's variables here
26-
x as NonNegativeReal
27-
z_i as NonNegativeReal for i in list
15+
//write the constraints here
16+
5x_1 - 2x_2 + 8x_3 ≤ 15
17+
8x_1+3x_2 -x_3 ≥ 9
18+
x_1+x_2+x_3≤6
19+
where
20+
21+
define
22+
// define the model's variables here
23+
x_1 as Real
24+
x_2 as Real
25+
x_3 as Real
2826
"#
2927
.to_string();
3028
let pipe_runner = PipeRunner::new(vec![

src/parser/grammar.pest

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ problem = {
55
#constraints = constraint_list ~
66
(
77
nl+ ~
8-
^"where" ~ nl+ ~
8+
^"where" ~
99
#where = consts_declaration
1010
)? ~
1111
(
1212
nl+ ~
13-
^"define" ~ nl+ ~
13+
^"define" ~
1414
#define = domains_declaration
1515
)? ~
1616
nl* ~
@@ -37,19 +37,19 @@ constraint = {
3737
constraint_name = {
3838
variable ~ ":" ~ nl*
3939
}
40-
41-
40+
41+
4242

4343
// constants declaration
44-
consts_declaration = { (const_declaration ~(nl* ~ const_declaration)*)? }
44+
consts_declaration = { (nl+ ~ const_declaration)* }
4545
const_declaration = {
4646
"let" ~
4747
#name = (simple_variable | "_") ~
4848
"=" ~
4949
#value = tagged_exp
5050
}
5151

52-
domains_declaration = { (domain_declaration ~ (nl* ~domain_declaration)*)? }
52+
domains_declaration = { (nl+ ~domain_declaration)* }
5353
domain_declaration = {
5454
#vars = domain_variables ~ nl*~
5555
^"as" ~

src/pipe/pipe_wasm_runner.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ impl Pipeable for JsPipable {
5959
fn pipe(
6060
&self,
6161
data: &mut PipeableData,
62-
pipe_context: &PipeContext,
62+
_pipe_context: &PipeContext,
6363
) -> Result<PipeableData, PipeError> {
6464
let js_pipable = match data {
6565
PipeableData::String(s) => JsPipableData::String(s.clone()),
@@ -74,7 +74,7 @@ impl Pipeable for JsPipable {
7474
};
7575
let js_pipable =
7676
serialize_json_compatible(&js_pipable).map_err(|e| PipeError::Other(e.to_string()))?;
77-
let mut js_args = js_sys::Array::new();
77+
let js_args = js_sys::Array::new();
7878
js_args.push(&js_pipable);
7979
let result = self.function.apply(&JsValue::NULL, &js_args).map_err(|e| {
8080
PipeError::Other(

src/solvers/simplex/optimal_tableau.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::prelude::*;
33
use crate::solvers::{LpSolution, Tableau};
44
use core::fmt;
55
use indexmap::IndexMap;
6-
use std::fmt::{write, Display};
6+
use std::fmt::{Display};
77

88
#[derive(Debug, Clone)]
99
#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]

src/utils.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,19 @@ impl InputSpan {
6565
/// * `Ok(&str)` - The extracted text slice if the span is valid
6666
/// * `Err(String)` - Error message if the span is out of bounds
6767
pub fn span_text<'a>(&self, text: &'a str) -> Result<&'a str, String> {
68+
let indices = text.char_indices();
6869
let end = (self.start + self.len) as usize;
6970
let start = self.start as usize;
70-
if start > text.len() || end > text.len() {
71+
let len = text.chars().count();
72+
if start > len || end > len {
7173
return Err(format!(
7274
"Span out of bounds: {}..{} (text len: {})",
7375
start,
7476
end,
7577
text.len()
7678
));
7779
}
78-
Ok(&text[start..end])
80+
Ok(utf8_slice::slice(text, start, end))
7981
}
8082
}
8183

tests/parser_tests.rs

+81-1
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,57 @@ define
117117
.expect("Failed to type check problem");
118118
}
119119

120+
#[test]
121+
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
122+
fn empty_where() {
123+
let input = "
124+
min 1
125+
s.t.
126+
1 <= 1
127+
where
128+
";
129+
RoocParser::new(input.to_string())
130+
.parse_and_transform(vec![], &IndexMap::new())
131+
.expect("Failed to parse and transform problem");
132+
RoocParser::new(input.to_string())
133+
.type_check(&vec![], &IndexMap::new())
134+
.expect("Failed to type check problem");
135+
}
136+
#[test]
137+
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
138+
fn empty_define() {
139+
let input = "
140+
min 1
141+
s.t.
142+
1 <= 1
143+
define
144+
";
145+
RoocParser::new(input.to_string())
146+
.parse_and_transform(vec![], &IndexMap::new())
147+
.expect("Failed to parse and transform problem");
148+
RoocParser::new(input.to_string())
149+
.type_check(&vec![], &IndexMap::new())
150+
.expect("Failed to type check problem");
151+
}
152+
153+
#[test]
154+
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
155+
fn empty_where_define() {
156+
let input = "
157+
min 1
158+
s.t.
159+
1 <= 1
160+
where
161+
define
162+
";
163+
RoocParser::new(input.to_string())
164+
.parse_and_transform(vec![], &IndexMap::new())
165+
.expect("Failed to parse and transform problem");
166+
RoocParser::new(input.to_string())
167+
.type_check(&vec![], &IndexMap::new())
168+
.expect("Failed to type check problem");
169+
}
170+
120171
#[test]
121172
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
122173
fn test_parser_variants4() {
@@ -134,6 +185,7 @@ define
134185
max {1, 2, 3} >= 1
135186
where
136187
let A = [1, 2, 3, 4]
188+
define
137189
";
138190
RoocParser::new(input.to_string())
139191
.parse_and_transform(vec![], &IndexMap::new())
@@ -185,6 +237,7 @@ define
185237
|x + y|z + 2 >= 1
186238
187239
2(x + y)3|x + y|z + 2 >= 1
240+
where
188241
define
189242
x,y,z as Real
190243
";
@@ -252,7 +305,8 @@ define
252305
s.t.
253306
S >= 1
254307
where
255-
let S = \"Hello\"
308+
let S = \"Hello\"\
309+
define
256310
";
257311
RoocParser::new(input.to_string())
258312
.parse_and_transform(vec![], &IndexMap::new())
@@ -276,6 +330,7 @@ define
276330
[0,1,0],
277331
[0,0,1]
278332
]
333+
define
279334
";
280335
RoocParser::new(input.to_string())
281336
.parse_and_transform(vec![], &IndexMap::new())
@@ -314,6 +369,31 @@ define
314369
.expect("Failed to type check problem");
315370
}
316371

372+
#[test]
373+
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
374+
fn non_utf8() {
375+
let input = "
376+
max 2x_1 + x_2 - x_3
377+
subject to
378+
//write the constraints here
379+
5x_1 - 2x_2 + 8x_3 ≤ 15
380+
8x_1+3x_2 -x_3 ≥ 9
381+
x_1+x_2+x_3≤6
382+
where
383+
384+
define
385+
// define the model's variables here
386+
x_1 as Real
387+
x_2 as Real
388+
x_3 as Real
389+
";
390+
391+
//i expect to get an error, and not panic
392+
RoocParser::new(input.to_string())
393+
.parse_and_transform(vec![], &IndexMap::new())
394+
.expect_err("");
395+
}
396+
317397
#[test]
318398
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
319399
fn test_const_decl_1() {

ts-lib/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"type": "module",
33
"name": "@specy/rooc",
44
"description": "Optimization modeling language",
5-
"version": "1.5.6",
5+
"version": "1.5.7",
66
"license": "GPL-3.0",
77
"main": "dist/index.js",
88
"typings": "dist/index.d.ts",

0 commit comments

Comments
 (0)