I am using choco to solve a CSP , and one of my constraints is that the sum of one variables (X[i][j]) is less than N=10, and i=j=1....N.
How do I accomplish this? thank you for your help.
sum(X[i][j]) = 1 for i=j=1....N
You need a 1d array and call model.sum():
import org.chocosolver.solver.Model;
import org.chocosolver.solver.Solver;
import org.chocosolver.solver.variables.BoolVar;
public class SumBooleans {
private final static int N = 10;
public static void main(String[] args) {
Model model = new Model("Boolean sum");
BoolVar[][] vars = new BoolVar[N][N];
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
vars[i][j] = model.boolVar("vars[" + i + "][" + j + "]");
}
}
BoolVar[] flatArray = new BoolVar[N * N];
for (int index = 0; index < N * N; index++) {
int i = index / N;
int j = index % N;
flatArray[index] = vars[i][j];
}
model.sum(flatArray, "=", 1).post();
//model.sum(flatArray, "<", N).post();
//model.sum(flatArray, ">=", 8).post();
Solver solver = model.getSolver();
if (solver.solve()) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
System.out.print(vars[i][j].getValue() + " ");
}
System.out.println();
}
}
}
}
Output:
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
Related
Having a vec![1,2,3,4,5,6,7,8,9,10,11,12] I would like to remove, in chunks of 6 elements, the last 3 elements, so the result would be a slice or vec like [1,2,3,7,8,9]. Is there an easy way of doing that without intermediate allocations (like collecting small vecs), preferably using iterators?
Thanks!
I would just filter based on the index (which you can get with .enumerate())
fn main() {
let data = vec![1,2,3,4,5,6,7,8,9,10,11,12];
let new_data: Vec<i32> = data.iter()
.enumerate()
.filter(|(i, _)| (*i / 3) % 2 == 0)
.map(|(_, v)| *v) // you could have this be Vec<&i32>, but I don't think you'd save memory/time. if it was a more complex data type, references would save memory and time
.collect();
println!("{new_data:?}");
}
0 / 3 == 0, 0 % 2 == 0, ? 0 == 0 -> true so keep
1 / 3 == 0, 0 % 2 == 0, ? 0 == 0 -> true so keep
2 / 3 == 0, 0 % 2 == 0, ? 0 == 0 -> true so keep
3 / 3 == 1, 1 % 2 == 1, ? 1 == 0 -> false so skip
4 / 3 == 1, 1 % 2 == 1, ? 1 == 0 -> false so skip
5 / 3 == 1, 1 % 2 == 1, ? 1 == 0 -> false so skip
6 / 3 == 2, 2 % 2 == 0, ? 0 == 0 -> true so keep
...
I have this shell.nix:
with import <nixpkgs> {};
stdenv.mkDerivation {
name = "env";
nativeBuildInputs = [ pkg-config ];
buildInputs = [
xorg.libX11
xorg.libX11.dev
xorg.libXi
xorg.libXext
libGL
];
}
Then I enter the shell with nix-shell shell.nix and
$ npm install gl
It builds correctly I think.
I have created index.js with example from project's home README
// Create context
var width = 64
var height = 64
var gl = require('gl')(width, height, { preserveDrawingBuffer: true })
//Clear screen to red
gl.clearColor(1, 0, 0, 1)
gl.clear(gl.COLOR_BUFFER_BIT)
//Write output as a PPM formatted image
var pixels = new Uint8Array(width * height * 4)
gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, pixels)
process.stdout.write(['P3\n# gl.ppm\n', width, " ", height, '\n255\n'].join(''))
for(var i = 0; i < pixels.length; i += 4) {
for(var j = 0; j < 3; ++j) {
process.stdout.write(pixels[i + j] + ' ')
}
}
when I run this:
$ node index.js
I got error:
\/home/roman/personal_projects/nixos/nixos-headless-gl-error-no-webgl/index.js:20
gl.clearColor(1, 0, 0, 1)
^
TypeError: Cannot read property 'clearColor' of null
at Object.<anonymous> (/home/roman/personal_projects/nixos/nixos-headless-gl-error-no-webgl/index.js:20:4)
at Module._compile (internal/modules/cjs/loader.js:1085:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
at Module.load (internal/modules/cjs/loader.js:950:32)
at Function.Module._load (internal/modules/cjs/loader.js:790:12)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
at internal/main/run_main_module.js:17:47
Works. Now I have run.nix:
with import <nixpkgs> {};
stdenv.mkDerivation {
name = "env";
APPEND_LIBRARY_PATH = "${lib.makeLibraryPath [ libGL libuuid ]}";
shellHook = ''
export LD_LIBRARY_PATH="$APPEND_LIBRARY_PATH:$LD_LIBRARY_PATH"
'';
}
and nix-shell run.nix and
$ nix-shell run.nix
$ echo $LD_LIBRARY_PATH | tr ':' '\n'
/nix/store/hwnjiqml5vvlngdj9nrnwg0qzglhgk2r-util-linux-2.36.2/lib
/nix/store/c35w5n0prvq4v4priyi8fiiq361pmyvp-libGL-1.3.3/lib
/nix/store/a1xkyw98lgj38kymim78a7xjk50wqg6k-telepathy-glib-0.24.2/lib
/nix/store/nzp3cc7bsj83dn23b4vvrvsp9psgg50m-telepathy-logger-0.8.2/lib
$ node index.js
P3
# gl.ppm
64 64
255
255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0
...
I am trying to implement a checksum calculation algorithm using this link
Here is an example of a byte slice and checksums for it.
And also my implementation code, tell me what I'm doing wrong.
//https://gist.github.com/david-hoze/0c7021434796997a4ca42d7731a7073a
func chksum(iph *ipv4.Header,payload []byte)uint16{
// https://github.com/mikioh/-stdyng/blob/master/afpacket.go
var htons = func(i uint16) uint16 {return (i<<8)&0xff00 | i>>8}
tcpLen := iph.TotalLen - iph.Len
src := iph.Src
dst := iph.Dst
var csum uint32
for i := 0; i < 16; i += 2 {
csum += uint32(src[i]) << 8
csum += uint32(src[i+1])
csum += uint32(dst[i]) << 8
csum += uint32(dst[i+1])
}
csum += uint32(htons(6)) // TCP PROTO
csum += uint32(htons(uint16(tcpLen)))
// old chksum
payload[16] = 0
payload[17] = 0
var i int
for tcpLen > 1 {
csum += uint32(payload[i]) << 8
csum += uint32(payload[i+1])
tcpLen -= 2
i++
}
if tcpLen % 2 == 1 {
csum += uint32(payload[tcpLen - 1]) << 8
}
for csum > 0xffff{
csum = (csum >> 16) + (csum & 0xffff)
}
return ^uint16(csum)
}
// output
[69 0 0 60 179 32 64 0 64 6 108 0 192 168 77 61 192 168 77 13 203 50
0 22 69 21 146 157 0 0 0 0 160 194 250 240 65 21 0 0 2 4 5 180 4 2 8
10 38 105 38 58 0 0 0 0 1 3 3 7]
src chkSumOriginaltcp = 0x4115 calc chkSumFaketcp = 0x72e8
Please tell me where I could be mistaken, it is very important to me
func chksum(data []byte, srcip, dstip []byte) uint16 {
data[16],data[17] = 0,0 //zero on original check sum
psheader := []byte{
srcip[0], srcip[1], srcip[2], srcip[3],
dstip[0], dstip[1], dstip[2], dstip[3],
0,
6, // TCP
0, byte(len(data)), // length (16 bits)
}
csum := make([]byte, 0, len(psheader)+len(data))
csum = append(csum, psheader...)
csum = append(csum, data...)
lenSumThis := len(csum)
var word uint16
var sum uint32
for i := 0; i+1 < lenSumThis; i += 2 {
word = uint16(csum[i])<<8 | uint16(csum[i+1])
sum += uint32(word)
}
if lenSumThis%2 != 0 {
sum += uint32(csum[len(csum)-1])
}
sum = (sum >> 16) + (sum & 0xffff)
sum = sum + (sum >> 16)
return uint16(^sum)
}
I'm using path_drawing package to get a path from an SVG in Flutter. After extracting path from SVG Data, it returns it to ClipPath widget which uses that path to clip the Container. After that, I increase the width and height of the Container which doesn't increase the size of SVG.
How do I alter the size of SVG?
import 'package:flutter/material.dart';
import 'package:path_drawing/path_drawing.dart';
void main() => runApp(MaterialApp(
home: MyApp(),
));
class MyApp extends StatefulWidget {
#override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Center(
//Container enclosed in ClipPath which uses Practice Clip as a clipper
child: ClipPath(
clipper: PracticeClip(),
child: Container(
//Container color
color: Colors.red,
//width and height of container
width: 100.0,
height: 100.0,
)),
),
);
}
}
class PracticeClip extends CustomClipper<Path> {
#override
Path getClip(Size size) {
Path path = Path();
//parseSvgPathData comes from a package
//Library link: https://pub.dev/packages/path_drawing (Library link)
//parseSvgPathData returns a Path object
//extracting path from SVG data
path = parseSvgPathData(
"M12.007.48C5.391.485.005 5.831 0 12.399v.03l2.042 1.191.028-.019a5.821 5.821 0 0 1 3.308-1.02c.371 0 .734.034 1.086.1l-.036-.006a5.69 5.69 0 0 1 2.874 1.431l-.004-.003.35.326.198-.434c.192-.42.414-.814.66-1.173.099-.144.208-.29.332-.446l.205-.257-.252-.211a8.33 8.33 0 0 0-3.836-1.807l-.052-.008a8.566 8.566 0 0 0-4.081.251l.061-.016c.971-4.257 4.714-7.224 9.133-7.227a9.31 9.31 0 0 1 6.601 2.713 9.197 9.197 0 0 1 2.508 4.499 8.386 8.386 0 0 0-2.498-.379h-.154c-.356.006-.7.033-1.037.078l.045-.005-.042.006a8.104 8.104 0 0 0-.39.06c-.057.01-.114.022-.17.033a8.103 8.103 0 0 0-.392.089l-.138.035a9.21 9.21 0 0 0-.483.144l-.029.01c-.355.12-.709.268-1.051.439l-.027.014c-.152.076-.305.16-.469.256l-.036.022a8.217 8.217 0 0 0-2.108 1.801l-.011.013-.075.092a8.346 8.346 0 0 0-.378.503c-.088.13-.177.269-.288.452l-.06.104a8.986 8.986 0 0 0-.234.432l-.016.029c-.17.341-.317.698-.44 1.063l-.017.053a8.053 8.053 0 0 0-.411 2.717v-.007l.001.112c.006.158.013.295.023.431l-.002-.037a11.677 11.677 0 0 0 .042.412l.005.042.013.103c.018.127.038.252.062.378.241 1.266.845 2.532 1.745 3.66l.041.051.042-.05c.359-.424 1.249-1.77 1.325-2.577l.001-.015-.007-.013a5.56 5.56 0 0 1-.64-2.595v-.001c0-3.016 2.371-5.521 5.397-5.702l.199-.007a5.93 5.93 0 0 1 3.47 1.025l.028.019 2.041-1.187v-.03a11.771 11.771 0 0 0-3.511-8.424A11.963 11.963 0 0 0 12.008.48z");
return path;// path is returned to ClipPath clipper
}
#override
bool shouldReclip(PracticeClip oldClipper) => false;
}
I believe you want to scale the path. Something like:
class PracticeClip extends CustomClipper<Path> {
#override
Path getClip(Size size) {
//parseSvgPathData comes from a package
//Library link: https://pub.dev/packages/path_drawing (Library link)
//parseSvgPathData returns a Path object
//extracting path from SVG data
final Path path = parseSvgPathData(
"M12.007.48C5.391.485.005 5.831 0 12.399v.03l2.042 1.191.028-.019a5.821 5.821 0 0 1 3.308-1.02c.371 0 .734.034 1.086.1l-.036-.006a5.69 5.69 0 0 1 2.874 1.431l-.004-.003.35.326.198-.434c.192-.42.414-.814.66-1.173.099-.144.208-.29.332-.446l.205-.257-.252-.211a8.33 8.33 0 0 0-3.836-1.807l-.052-.008a8.566 8.566 0 0 0-4.081.251l.061-.016c.971-4.257 4.714-7.224 9.133-7.227a9.31 9.31 0 0 1 6.601 2.713 9.197 9.197 0 0 1 2.508 4.499 8.386 8.386 0 0 0-2.498-.379h-.154c-.356.006-.7.033-1.037.078l.045-.005-.042.006a8.104 8.104 0 0 0-.39.06c-.057.01-.114.022-.17.033a8.103 8.103 0 0 0-.392.089l-.138.035a9.21 9.21 0 0 0-.483.144l-.029.01c-.355.12-.709.268-1.051.439l-.027.014c-.152.076-.305.16-.469.256l-.036.022a8.217 8.217 0 0 0-2.108 1.801l-.011.013-.075.092a8.346 8.346 0 0 0-.378.503c-.088.13-.177.269-.288.452l-.06.104a8.986 8.986 0 0 0-.234.432l-.016.029c-.17.341-.317.698-.44 1.063l-.017.053a8.053 8.053 0 0 0-.411 2.717v-.007l.001.112c.006.158.013.295.023.431l-.002-.037a11.677 11.677 0 0 0 .042.412l.005.042.013.103c.018.127.038.252.062.378.241 1.266.845 2.532 1.745 3.66l.041.051.042-.05c.359-.424 1.249-1.77 1.325-2.577l.001-.015-.007-.013a5.56 5.56 0 0 1-.64-2.595v-.001c0-3.016 2.371-5.521 5.397-5.702l.199-.007a5.93 5.93 0 0 1 3.47 1.025l.028.019 2.041-1.187v-.03a11.771 11.771 0 0 0-3.511-8.424A11.963 11.963 0 0 0 12.008.48z");
final Rect pathBounds = path.getBounds();
final Matrix4 matrix4 = Matrix4.identity();
matrix4.scale(size.width / pathBounds.width, size.height / pathBounds.height);
return path.transform(matrix4.storage);// path is returned to ClipPath clipper
}
#override
bool shouldReclip(PracticeClip oldClipper) => false;
}
Should get you started.
Recently, I asked a question but got down votes since guys thought it is not clear. However, I have found a hint which needs some digging...
There is a command line program called fluent. Problem is that in the Rocks, when I run it on the front-end and enter exit, it will return to the command prompt.
5991 nodes, binary.
5991 node flags, binary.
Done.
> exit
mahmood#cluster:~$
However, when I run the same command (the application is on /export/ which is a NFS drive) on the compute node via ssh, it doesn't return to the command prompt.
5991 nodes, binary.
5991 node flags, binary.
Done.
> exit
^C^C^Z
[1]+ Stopped /share/apps/fluent/bin/fluent 3d -g -t4 -i elbow.journal
mahmood#compute-0-3:~$ pkill fluent*
mahmood#compute-0-3:~$ fg
/share/apps/fluent/bin/fluent 3d -g -t4 -i elbow.journal
Terminated
As suggested, I tried with strace and attached it to the process multiple times since the application runs on multicores. In one attempt, the application returned back to the terminal. I noticed that in in the last lines of the strace, there is a difference between the outcome of futex.
In the correct execution, I see:
socket(PF_INET, SOCK_STREAM, IPPROTO_IP) = 12
setsockopt(12, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0
setsockopt(12, SOL_TCP, TCP_NODELAY, [1], 4) = 0
setsockopt(12, SOL_SOCKET, SO_SNDBUF, [65536], 4) = 0
setsockopt(12, SOL_SOCKET, SO_RCVBUF, [65536], 4) = 0
fcntl(12, F_GETFL) = 0x2 (flags O_RDWR)
fcntl(12, F_SETFL, O_RDWR) = 0
connect(12, {sa_family=AF_INET, sin_port=htons(45470), sin_addr=inet_addr("10.10.10.251")}, 16) = 0
write(12, "12345\0", 6) = 6
write(12, "15 NORMAL_EXITING\0", 19) = 19
read(12, "\0", 1) = 1
close(12) = 0
futex(0x2b66afe5d9d0, FUTEX_WAIT, 12432, NULL) = 0
futex(0x2b66afc5c9d0, FUTEX_WAIT, 12427, NULL) = 0
close(6) = 0
close(7) = 0
close(8) = 0
close(9) = 0
close(10) = 0
shmdt(0x2b66af7d8000) = 0
shmdt(0x2b66b0018000) = 0
shmdt(0x2b66af3a8000) = 0
shmdt(0x2b66af638000) = 0
shmdt(0x2b66af758000) = 0
shmdt(0x2b66aff78000) = 0
shmdt(0x2b66af6d8000) = 0
shmdt(0x2b66afed8000) = 0
close(4) = 0
close(5) = 0
exit_group(0) = ?
Process 12420 detached
and in the buggy run, I see:
socket(PF_INET, SOCK_STREAM, IPPROTO_IP) = 9
setsockopt(9, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0
setsockopt(9, SOL_TCP, TCP_NODELAY, [1], 4) = 0
setsockopt(9, SOL_SOCKET, SO_SNDBUF, [65536], 4) = 0
setsockopt(9, SOL_SOCKET, SO_RCVBUF, [65536], 4) = 0
fcntl(9, F_GETFL) = 0x2 (flags O_RDWR)
fcntl(9, F_SETFL, O_RDWR) = 0
connect(9, {sa_family=AF_INET, sin_port=htons(50825), sin_addr=inet_addr("10.10.10.251")}, 16) = 0
write(9, "12345\0", 6) = 6
write(9, "15 NORMAL_EXITING\0", 19) = 19
read(9, "\0", 1) = 1
close(9) = 0
futex(0x2b74f03659d0, FUTEX_WAIT, 13135, NULL) = -1 EAGAIN (Resource temporarily unavailable)
futex(0x2b74f01649d0, FUTEX_WAIT, 13132, NULL) = 0
close(6) = 0
close(7) = 0
shmdt(0x2b74efce0000) = 0
shmdt(0x2b74f03e0000) = 0
shmdt(0x2b74efbe0000) = 0
shmdt(0x2b74f0480000) = 0
shmdt(0x2b74ef8b0000) = 0
shmdt(0x2b74efb40000) = 0
shmdt(0x2b74efc60000) = 0
shmdt(0x2b74f0520000) = 0
close(4) = 0
close(5) = 0
exit_group(0) = ?
Process 13129 detached
As you can see, although both of them say exit_group(0), the latter says a resource is temporarily unavailable.
Any thought on that?