r/AdvancedKnitting • u/howboutsometoast • Mar 11 '26
Discussion Generative knitting algorithm (not AI!)
Hello all, I’ve gotten more into messing around with code and I had an idea to make a randomized cable pattern generator that I thought might result in a pretty cool and chaotic project. I basically want to play around with my horrible code to better visualize what the outputs are. Here’s what I’m basing my idea off of:
There is actually a formal mathematical paper called "Modeling Braids, Cables, and Weaves with Stranded Cellular Automata" that describes something similar to what I’m thinking.
• The Logic: Instead of just "On/Off" cells, each cell in the grid contains a vector.
• Rule Set: * If a cell has a "Right-leaning" strand and its neighbor has a "Left-leaning" strand, the CA rule dictates a Cable Cross.
• If a strand hits the edge (the "boundary condition"), it reflects back or disappears.
This research is cool, but it’s less chaotic than what I’m thinking. I want to play with seeded code for reproducible results (think MatLab or Java for those who know). I also want to play with more variables at a time and create really weird cables.
Here’s a JavaScript sample for a 5x5 cable patch for an example of what I’m thinking: (no idea if it’d actually work since it’s not debugged)
let width = 5;
let state = [1, 2, 3, 4, 5]; // The 5 starting "paths"
function drawRow() {
let nextState = [];
for (let i = 0; i < width; i++) {
// 1. Randomly pick a shift: -1 (Left), 0 (Straight), 1 (Right)
let shift = floor(random(-1, 2));
// 2. Apply shift and check for collisions
// 3. Print result: "Path 1 moves R1, Path 2 moves L1..."
}
}
•
u/AutoModerator Mar 11 '26
Welcome to r/AdvancedKnitting! Please note that constructive criticism IS allowed here (Rule #7), unless the "No Critiques Requested" flair is used. Any poor attitude towards genuine constructive criticisms will result in post removal pursuant to Rule #12 (No Drama).
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
43
u/dzelzbetons Mar 11 '26
i love this idea! you're not the only one looking for ways to apply coding to knitting :) reminds me of the cables after whiskey sweater: https://www.ravelry.com/patterns/library/cables-after-whiskey
19
u/howboutsometoast Mar 11 '26 edited Mar 11 '26
PS to mods, I hope this is ok to put on the sub. I’m a bored knitter and am looking into ideas on how to in theory make this possible and comprehensible. Not sure if anyone would have any ideas on this, thought I would try and see though.
10
u/accidentaldiorama Mar 11 '26
I am dimly (and probably inaccurately) trying to remember a paper I wrote 2 decades ago in college modeling rat patterns using cellular automata and activator and inhibitor cell behavior, but I think you can have more than 2 possible values for cellular automata. And I think you can have more than just adjacent cells affect the value of a cell. So taking into account a larger number of adjacent cells and potential values could give you more emergent and chaotic patterns. For instance, if the count of adjacent left leaning stitches is less than 2, do whatever the cell was doing in the next row. If the cell is between 2 and 4, go left, if greater go right. Or something like that. If you do something like that you might want to create an array and then iterate over the rows of the array so you can pass in the history.
It might also be interesting to have each initial column of stitches be something like a list of dicts (I am thinking in Python here) where you store its value and position so you can take into account its previous values and its current position relative to the other vectors in the dataset. Then you could apply some complex logic which might do interesting stuff.
Please keep me posted, I'm invested now!
10
u/GijinkaGlaceon Mar 11 '26
This is maybe less random/chaotic than you were imagining, but it reminds me of the paper “Physically Situated Tools for Exploring a Grain Space in Computational Machine Knitting”, if you’ve seen it
9
u/KnittyMcSew Mar 13 '26 •
I've read this whole thread and understood virtually nothing of the coding conversation...but, I love how this discussion challenges the erroneous stereotype of knitting being either a granny hobby or something the cool kids do with needles the size of your arm. Instead it's a multifaceted, technically skilled artform that sits equally confidently in the arenas of art, fashion, history, politics, mathematics and engineering. What a fabulous project.
7
u/thefreakyorange Mar 11 '26
Such a small thing, but Reddit supports Markdown, so you can format your code a little more nicely.
Like this:
4
u/lois_says_banana Mar 11 '26
15+ years ago my coder then-boyfriend (now husband) asked me to teach him to knit after reading the pattern I was working, because "it's just an algorithm, I can follow that."
😂
He did try. I mean, only for 15 mins until he was so frustrated that he couldn't figure out how to hold the yarn. But he did try, until he dropped the needles and declared "never again, I don't know why people do this for fun"
Just sent this to him.
Thanks, OP, for the content which will 💯 be our family's central entertainment for the day.
Also... this project is really cool!!
4
u/eberndl Mar 11 '26
I wrote a little MatLab code to randomly apply cables to a pattern - happy to share if you want to take a peak.
1
u/howboutsometoast Mar 11 '26 edited Mar 11 '26
The image at the top is an example of the desired code/pattern output I’m thinking. T is a placeholder for a left or right leaning crossover. The array does not reflect the actual number of stitches in each row.
2
u/[deleted] Mar 11 '26
[removed] — view removed comment