7 个版本 (1 个稳定版)
1.0.0 | 2024 年 7 月 9 日 |
---|---|
0.5.1 | 2024 年 6 月 28 日 |
0.4.0 | 2024 年 6 月 20 日 |
0.3.1 | 2024 年 6 月 6 日 |
0.1.0 | 2024 年 4 月 4 日 |
第 1059 位在 数学
每月 220 次下载
66KB
1.5K SLoC
Rust SDK for OMMX (开放数学编程交换)
OMMX 消息
OMMX 定义了 protobuf 架构中的几个消息,它们的 Rust 绑定在 [v1
] 模块中。
示例
-
在 Rust 中创建
v1::Linear
消息,并对其进行序列化和反序列化use ommx::v1::{Linear, linear::Term}; use prost::Message; // For `encode` and `decode` methods // Create a linear function `x1 + 2 x2 + 3` let linear = Linear { terms: vec![ Term { id: 1, coefficient: 1.0 }, Term { id: 2, coefficient: 2.0 } ], constant: 3.0, }; // Serialize the message to a byte stream let mut buf = Vec::new(); linear.encode(&mut buf).unwrap(); // Deserialize the byte stream back into a linear function message let decoded_linear = Linear::decode(buf.as_slice()).unwrap(); // Print the deserialized message println!("{:?}", decoded_linear);
-
Evaluate
一个v1::Linear
与v1::State
,并将其转换为f64
use ommx::{Evaluate, v1::{Linear, State, linear::Term}}; use maplit::{hashmap, btreeset}; // Create a linear function `x1 + 2 x2 + 3` let linear = Linear { terms: vec![ Term { id: 1, coefficient: 1.0 }, Term { id: 2, coefficient: 2.0 } ], constant: 3.0, }; // Create a state `x1 = 4`, `x2 = 5`, and `x3 = 6` let state: State = hashmap! { 1 => 4.0, 2 => 5.0, 3 => 6.0 }.into(); // Evaluate the linear function with the state, and get the value and used variable ids let (value, used_ids) = linear.evaluate(&state).unwrap(); assert_eq!(value, 1.0 * 4.0 + 2.0 * 5.0 + 3.0); assert_eq!(used_ids, btreeset!{ 1, 2 }) // x3 is not used
OMMX 工件
OMMX 工件是 OCI 工件,即包含任意内容的容器镜像,用于存储 OMMX 消息。它对于在本地磁盘上存储消息或通过容器注册表与他人共享非常有用。
示例
-
使用由
random::random_lp
创建的实例创建工件作为文件use ocipkg::ImageName; use ommx::{artifact::{Builder, InstanceAnnotations}, random::random_lp}; use rand::SeedableRng; // Create random LP instance to be saved into an artifact let lp = random_lp(&mut rand::thread_rng(), 5, 7); // Builder for creating an artifact as a file (e.g. `random_lp_instance.ommx`) let mut builder = Builder::new_archive_unnamed("random_lp_instance.ommx".into())?; // Add the instance with annotations let mut annotations = InstanceAnnotations::default(); annotations.set_title("random_lp".to_string()); annotations.set_created(chrono::Local::now()); builder.add_instance(lp, annotations)?; // Build the artifact let _artifact = builder.build()?;
-
在本地注册表中创建工件,然后将其推送到远程注册表(例如 GitHub 容器注册表)
use ocipkg::ImageName; use ommx::{artifact::{Builder, InstanceAnnotations}, random::random_lp}; use rand::SeedableRng; // Create random LP instance to be saved into an artifact let lp = random_lp(&mut rand::thread_rng(), 5, 7); // Builder for creating an artifact in local registry let mut builder = Builder::new( ImageName::parse("ghcr.io/jij-inc/ommx/random_lp_instance:testing")? )?; // Add annotations for the artifact builder.add_source(&url::Url::parse("https://github.com/Jij-Inc/ommx")?); builder.add_description("Test artifact".to_string()); // Add the instance with annotations let mut annotations = InstanceAnnotations::default(); annotations.set_title("random_lp".to_string()); annotations.set_created(chrono::Local::now()); builder.add_instance(lp, annotations)?; // Build the artifact let mut artifact = builder.build()?; // Push the artifact to remote registry artifact.push()?;
-
从远程注册表拉取工件,并加载实例消息
use ocipkg::ImageName; use ommx::artifact::{Artifact, media_types}; let image_name = ImageName::parse("ghcr.io/jij-inc/ommx/random_lp_instance:testing")?; // Pull the artifact from remote registry let mut remote = Artifact::from_remote(image_name)?; let mut local = remote.pull()?; // List the digest of instances for desc in local.get_layer_descriptors(&media_types::v1_instance())? { println!("{}", desc.digest()); }
依赖关系
~14–29MB
~429K SLoC