Calling the function with new list - recursion
Hello guys I want to make an animation using Drracket with 4 different objects going different ways like "left right up and down" but on-tick part I'm having a problem.
So the problem is I'm giving the function a list and I want to take another made up new list and call the function with this "new" list again so it will automatically be recursion and shapes will move without stopping
My program is like this
(define R 300)
(define L (* 2 R))
(define U (* 2.5 R))
(define MYSCN (empty-scene L U))
;Structure:
(define-struct SHAPE (type posn direction color size))
;type is a shape of the object and it can only be circle or square (Image)
;posn is the initial point of the shape is drawn (Number)
;direction can be "left right up down" and represents the direction of the shape moves (String)
;color and size are the color and size of the object (String)
;Contructors
(define pos1 (make-posn 450 50))
(define pos3 (make-posn 200 540))
(define shape1 (make-SHAPE "circle" pos1 "down" "red" 50))
(define shape2 (make-SHAPE "square" (make-posn 100 230) "right" "purple" 170))
(define shape3 (make-SHAPE "circle" pos3 "up" "green" 100))
(define shape4 (make-SHAPE "square" (make-posn 500 450) "left" "orange" 155))
(define shapelist1 (list shape1 shape2 shape3 shape4))
;Selectors
(posn-x pos1) ;400 (Number)
(posn-y (SHAPE-posn shape2)) ;200 (Number)
(SHAPE-type shape3) ;"circle" (Image)
(SHAPE-direction shape4) ;"left" (String)
(SHAPE-color shape1) ;"red" (String)
;Predicators
(SHAPE? shape1) ;true
(posn? shape2) ;false
(posn? pos1) ;true
;Purpose: Transferring all the shapes to the scene
;alltheshapes shapelist1-->image
(define (alltheshapes f)
(place-image (circle (SHAPE-size (first shapelist1)) "solid" (SHAPE-color (first shapelist1)))
(posn-x (SHAPE-posn (first shapelist1))) (posn-y (SHAPE-posn (first shapelist1)))
(place-image
(square (SHAPE-size (first(rest shapelist1))) "solid" (SHAPE-color (first (rest shapelist1))))
(posn-x (SHAPE-posn (first(rest shapelist1)))) (posn-y (SHAPE-posn (first(rest shapelist1))))
(place-image
(circle (SHAPE-size (first (rest (rest shapelist1)))) "solid" (SHAPE-color (first (rest (rest shapelist1)))))
(posn-x (SHAPE-posn (first (rest (rest shapelist1))))) (posn-y (SHAPE-posn (first (rest (rest shapelist1)))))
(place-image
(square (SHAPE-size(first(rest(rest(rest shapelist1))))) "solid" (SHAPE-color(first(rest(rest(rest shapelist1))))))
(posn-x (SHAPE-posn (first(rest(rest(rest shapelist1)))))) (posn-y (SHAPE-posn (first(rest(rest(rest shapelist1))))))
MYSCN)))))
I couldn't figure out the recursive part here
;Purpose: Change the position of the shape according to the current word
;Contract: moveshapes shapelist1-->creates a new shape
(define (moveshapes shapelist1)
(cond
((string=? (SHAPE-direction (first shapelist1)) "down") (append (list (make-SHAPE "circle" (make-posn 450 (+ 1 (posn-y pos1))) "down" "red" 50)) (moveshape (rest shapelist1))))
((string=? (SHAPE-direction (first shapelist1)) "right") (append (list (make-SHAPE "square" (make-posn (+ 1 (posn-x (SHAPE-posn shape2))) 230) "right" "purple" 170)) (moveshape (rest shapelist1))))
((string=? (SHAPE-direction (first shapelist1)) "up") (append (list(make-SHAPE "circle" (make-posn 200 (- (posn-y (SHAPE-posn shape3)) 1)) "up" "green" 100)) (moveshape (rest shapelist1))))
((string=? (SHAPE-direction (first shapelist1)) "left") (append (list(make-SHAPE "square" (make-posn (- (posn-x (SHAPE-posn shape4)) 1) 450) "left" "orange" 155))))
(define (Project super)
(big-bang super
(to-draw alltheshapes) ;Takes the current word and produce an image
(on-tick moveshapes))) ;It will take the shapes and create a new shape while moving them
(Project shapelist1)
Before you move onto the animation part, I'd like to help you remove some of the pain points from your program above -
(require 2htdp/image)
; setup my scene
(define my-scene (empty-scene 600 750))
; define structure for scene elements
(define-struct element (image posn direction))
; define my elements
(define e1 (make-element (circle 50 "solid" "red") (make-posn 450 50) "down"))
(define e2 (make-element (square 170 "solid" "purple") (make-posn 100 230) "right"))
(define e3 (make-element (circle 100 "solid" "green") (make-posn 200 540) "up"))
(define e4 (make-element (square 155 "solid" "orange") (make-posn 500 450) "left"))
(define my-elements (list e1 e2 e3 e4))
; place elements into scene
(define (place-elements scene elements)
(if (null? elements)
scene
(place-image (element-image (car elements))
(posn-x (element-posn (car elements)))
(posn-y (element-posn (car elements)))
(place-elements scene (cdr elements)))))
(place-elements my-scene my-elements)
This renders the following scene -
If you're still stuck on the animation after reading this post, please let me know and I'll help you further.
(define R 300)
(define L (* 2 R))
(define U (* 2.5 R))
(define MYSCN (empty-scene L U))
;Structure:
(define-struct SHAPE (type posn direction color size))
;type is a shape of the object and it can only be circle or square (Image)
;posn is the initial point of the shape that drawn (Number)
;direction can be "left right up down" and represents the direction of the shape that moves (String)
;color and size are the color and size of the object (String)
;Contructors
(define pos1 (make-posn 450 60))
(define pos3 (make-posn 160 620))
(define shape1 (make-SHAPE "circle" pos1 "down" "red" 50))
(define shape2 (make-SHAPE "square" (make-posn 100 230) "right" "purple" 170))
(define shape3 (make-SHAPE "circle" pos3 "up" "green" 100))
(define shape4 (make-SHAPE "square" (make-posn 500 450) "left" "orange" 155))
(define shapelist1 (list shape1 shape2 shape3 shape4))
;Selectors
(posn-x pos1) ;450 (Number)
(posn-y (SHAPE-posn shape2)) ;230 (Number)
(SHAPE-type shape3) ;"circle" (String)
(SHAPE-direction shape4) ;"left" (String)
(SHAPE-color shape1) ;"red" (String)
;Predicators
(SHAPE? shape1) ;true
(posn? shape2) ;false
(posn? pos1) ;true
;*****************************************************************************************************************************************************************************************************
;Purpose: add all the image inside of the given list to a scene
;Contract: to-draw (alltheshapes LOS -> Images)
;Example
(check-expect (alltheshapes (list shape1)) (place-image (circle 50 "solid" "red") 450 60 MYSCN))
(check-expect (alltheshapes (list shape3)) (place-image (circle 100 "solid" "green") 160 620 MYSCN))
(check-expect (alltheshapes (list shape4)) (place-image (square 155 "solid" "orange") 500 450 MYSCN))
;Function
(define (alltheshapes a-list)
(cond
((empty? a-list) MYSCN)
((string=? (SHAPE-type (first a-list)) "circle")
(place-image
(circle (SHAPE-size (first a-list)) "solid" (SHAPE-color (first a-list)))
(posn-x (SHAPE-posn (first a-list))) (posn-y (SHAPE-posn (first a-list)))
(alltheshapes (rest a-list))))
((string=? (SHAPE-type (first a-list)) "square")
(place-image
(square (SHAPE-size (first a-list)) "solid" (SHAPE-color (first a-list)))
(posn-x (SHAPE-posn (first a-list))) (posn-y (SHAPE-posn (first a-list)))
(alltheshapes (rest a-list))))))
;Test
(alltheshapes (list shape2 shape3))
(alltheshapes (list shape1 shape3 shape4))
;*****************************************************************************************************************************************************************************************************
;Purpose: Moves the shapes every tick
;Contract: on-tick (moveshapes LOS->LOS)
;Example
(check-expect (moveshapes (list shape2)) (cons (make-SHAPE "square" (make-posn 101 230) "right" "purple" 170) '()))
(check-expect (moveshapes (list shape1)) (cons (make-SHAPE "circle" (make-posn 450 61) "down" "red" 50) '()))
(check-expect (moveshapes (list shape4)) (cons (make-SHAPE "square" (make-posn 499 450) "left" "orange" 155) '()))
;Function
(define (moveshapes a-list)
(cond
((empty? a-list) empty)
((string=? (SHAPE-direction (first a-list)) "down") (cons (make-SHAPE (SHAPE-type (first a-list)) (make-posn (posn-x (SHAPE-posn (first a-list))) (+ (posn-y (SHAPE-posn (first a-list))) 1))
"down" (SHAPE-color (first a-list)) (SHAPE-size (first a-list))) (moveshapes (rest a-list))))
((string=? (SHAPE-direction (first a-list)) "right")(cons (make-SHAPE (SHAPE-type (first a-list)) (make-posn (+ 1 (posn-x (SHAPE-posn (first a-list)))) (posn-y (SHAPE-posn (first a-list))))
"right" (SHAPE-color (first a-list)) (SHAPE-size (first a-list))) (moveshapes (rest a-list))))
((string=? (SHAPE-direction (first a-list)) "up") (cons (make-SHAPE (SHAPE-type (first a-list)) (make-posn (posn-x (SHAPE-posn (first a-list))) (- (posn-y (SHAPE-posn (first a-list))) 1))
"up" (SHAPE-color (first a-list)) (SHAPE-size (first a-list))) (moveshapes (rest a-list))))
((string=? (SHAPE-direction (first a-list)) "left") (cons (make-SHAPE (SHAPE-type (first a-list)) (make-posn (- (posn-x (SHAPE-posn (first a-list))) 1) (posn-y (SHAPE-posn (first a-list))))
"left" (SHAPE-color (first a-list)) (SHAPE-size (first a-list))) (moveshapes (rest a-list))))
(else a-list)))
;Test
(moveshapes (list shape2 shape1))
;*****************************************************************************************************************************************************************************************************
;----------------------------------------------------------------------------
;Purpose: Help to on-mouse command with creating all the shapes again as a list
;Contract: LOS-> (creates the original forms of the shapes)
;Examples
(check-expect (helperonmouse (list shape2 shape3)) (cons (make-SHAPE "square" (make-posn 100 230) "right" "purple" 170)
(cons (make-SHAPE "circle" (make-posn 160 620) "up" "green" 100) '())))
(define (helperonmouse shapelist1)
(cond
((empty? shapelist1) empty)
(else (cons (make-SHAPE (SHAPE-type (first shapelist1)) (make-posn (posn-x (SHAPE-posn (first shapelist1))) (posn-y (SHAPE-posn (first shapelist1))))
(SHAPE-direction (first shapelist1)) (SHAPE-color (first shapelist1)) (SHAPE-size (first shapelist1))) (helperonmouse (rest shapelist1))))))
;Test
(helperonmouse (list shape1 shape4))
;----------------------------------------------------------------------------
;Purpose: Every time when you hit mouse1, the animation will restart
;Contract: on-mouse LOS -> LOS
;Examples
(check-expect (restart (list shape2) 3 5 "button-down") (cons(make-SHAPE"circle"(make-posn 450 60) "down" "red" 50) (cons (make-SHAPE "square" (make-posn 100 230) "right" "purple" 170)
(cons (make-SHAPE "circle" (make-posn 160 620) "up" "green" 100) (cons (make-SHAPE "square" (make-posn 500 450) "left" "orange" 155)'())))))
(check-expect (restart (list shape4) 6 1 "button-down") (cons (make-SHAPE "circle" (make-posn 450 60) "down" "red" 50) (cons (make-SHAPE "square" (make-posn 100 230) "right" "purple" 170)
(cons (make-SHAPE "circle" (make-posn 160 620) "up" "green" 100) (cons (make-SHAPE "square" (make-posn 500 450) "left" "orange" 155) '())))))
(check-expect (restart (list shape1) 4 2 "button-down") (cons (make-SHAPE "circle" (make-posn 450 60) "down" "red" 50) (cons (make-SHAPE "square" (make-posn 100 230) "right" "purple" 170)
(cons (make-SHAPE "circle" (make-posn 160 620) "up" "green" 100) (cons (make-SHAPE "square" (make-posn 500 450) "left" "orange" 155) '())))))
;Function
(define (restart a-list x y clack)
(cond
((empty? a-list) empty)
((mouse=? clack "button-down") (helperonmouse shapelist1))
(else a-list)))
;Test
(restart (list shape1) 3 4 "button-down")
;*****************************************************************************************************************************************************************************************************
;Purpose: When you press the "d" button in your keyboard, one of the images will be deleted
;Contract: on-key LOS-> (- LOS 1)
;Examples
(check-expect (delete (list shape1 shape2) "d") (cons (make-SHAPE "square" (make-posn 100 230) "right" "purple" 170) '()))
(check-expect (delete (list shape2 shape3 shape4) "d") (cons (make-SHAPE "circle" (make-posn 160 620) "up" "green" 100) (cons (make-SHAPE "square" (make-posn 500 450) "left" "orange" 155) '())))
(check-expect (delete (list shape1) "d") '())
;Function
(define (delete a-list a-key)
(cond
((empty? a-list) empty)
((key=? a-key "d") (rest a-list))
(else a-list)))
;Test
(delete (list shape2 shape1) "d")
;*****************************************************************************************************************************************************************************************************
(define (Project super)
(big-bang super
(to-draw alltheshapes) ;Takes the current word and produce an image
(on-tick moveshapes) ;Takes the current word and everytick changes the coordinates of the shapes
(on-mouse restart) ;Takes the current word and create the original current word again
(on-key delete))) ;Takes the current word and remove one element inside of it
(Project shapelist1)
Related
How to solve this warning "Decision variable "IL#0#0#0#0#0#0" has never been used by the engine."?
I have been working on the mathematical model for a long time and now I could improve it. In this model we have nodes and edges and the flows that pass through these nodes according to the topology. We also have network functions that we use to create two length chains. I don't know how to define two dependent indices. For example, in constraint 1 and the following constraints, the two indices j and nj[j] are dependent on each other. I don't know if I defined this dependency correctly or not? If you can check my code and tell me where I made a mistake or give me an example, I would be grateful. Now, My code works. But I get the error that I have not used the decision variable IL for 99 times, and also the 3, 4 objective functions always give zero value I don't think this can be true. Dear Mr Fleischer, I have asked you several times questions and I am very grateful for your kindness, but if you can, please help me this time as well. Thanks in advance. This is the topology image, which I determined flows and their routes .mod file using CPLEX; //Total nodes number. range Nodes = 1..9; //................................................................................ //Total links number //two_directed tuple edge{ key int node_out; key int node_in; }; {edge} L with node_out, node_in in Nodes = {<1,3>, <3,1>, <2,3>, <3,2>, <3,4>, <4,3>, <3,5>, <5,3>, <3,6>, <6,3>, <4,5>, <5,4>, <4,6>, <6,4>, <4,8>, <8,4>, <5,6>, <6,5>, <6,7>, <7,6>, <6,9>, <9,6>}; {edge} Lin[Nodes] = [{<3,1>},//node1 {<3,2>},//node2 {<1,3>, <2,3>, <4,3>, <5,3>, <6,3>},//node3 {<3,4>, <5,4>, <6,4>, <8,4>},//node4 {<3,5>, <4,5>, <6,5>},//node5 {<3,6>, <4,6>, <5,6>, <7,6>, <9,6>},//node6 {<6,7>},//node7 {<4,8>},//node8 {<6,9>}];//node9 {edge} Lout[Nodes] = [{<1,3>},//node1 {<2,3>},//node2 {<3,1>, <3,2>, <3,4>, <3,5>, <3,6>},//node3 {<4,3>, <4,5>, <4,6>, <4,8>},//node4 {<5,3>, <5,4>, <5,6>},//node5 {<6,3>, <6,4>, <6,5>, <6,7>, <6,9>},//node6 {<7,6>},//node7 {<8,4>},//node8 {<9,6>}];//node9 //....................................................................................... //Flows and their routes tuple cflow{ int node1; int node2; } {cflow} F with node1,node2 in Nodes = {<1,2>, <1,3>, <1,4>, <1,5>, <1,6>, <1,7>, <1,8>, <1,9>, <2,1>, <2,3>, <2,4>, <2,5>, <2,6> <2,7>, <2,8>, <2,9>, <3,1>, <3,2>, <3,4>, <3,5>, <3,6>, <3,7>, <3,8>, <3,9>, <4,1>, <4,2>, <4,3>, <4,5>, <4,6>, <4,7>, <4,8>, <4,9>, <5,1>, <5,2>, <5,3>, <5,4>, <5,6>, <5,7>, <5,8>, <5,9>, <6,1>, <6,2>, <6,3>, <6,4>, <6,5>, <6,7>, <6,8>, <6,9>, <7,1>, <7,2>}; {int} Routes[F] = [{1,2}, {1,3}, {1,3,4}, {1,3,5}, {1,3,6}, {1,3,6,7}, {1,3,4,8}, {1,3,6,9},{2,3,1}, {2,3}, {2,3,4}, {2,3,5}, {2,3,6}, {2,3,6,7}, {2,3,4,8}, {2,3,6,9}, {3,1}, {3,2}, {3,4}, {3,5}, {3,6}, {3,6,7}, {3,4,8}, {3,6,9}, {4,3,1}, {4,3,2}, {4,3}, {4,5}, {4,6}, {4,6,7}, {4,8}, {4,6,9}, {5,3,1}, {5,3,2}, {5,3}, {5,4}, {5,6}, {5,6,7}, {5,4,8}, {5,6,9}, {6,3,1}, {6,3,2}, {6,3}, {6,4}, {6,5}, {6,7}, {6,4,8}, {6,9}, {7,6,3,1}, {7,6,3,2}]; //landa float landa[f in F]=(0.5+rand(2))/2; //..................................................... //VNFs {string} V = {"P", "F", "I", "D", "N"}; //........................................................... //Random Chains int n=card(V); int m=2; range r=1..n; // scripting way that will get m times 1 range subr=1..m; int t[j in subr][f in F]=1+rand(n+1-j); {int} setn[f in F]=asSet(r); int x2[j in r][F]; execute { for(var j in subr)for (var f in F) { var e=t[j][f]; var e2=Opl.item(setn[f],e-1); x2[e2][f]=1; setn[f].remove(e2); }} {int} result[f in F]={j | j in r:x2[j][f]==1}; {string} Chains4[f in F]=union (j in result[f]) {item(V,j-1)}; {string} Chains[f in F]={item(Chains4[f],i-1) | i in 1..card(Chains4[f])}; assert forall(f in F) forall(ordered j,i in Chains[f]) j!=i; //length Chains int J[f in F] = card(Chains4[f]); //...................................................................................... //Resources int Cpunode[n in Nodes]=...; //the number of cores at each node. int cpuvnf[v in V]=...; //an instance of type v may need multiple cores. int Memnode[n in Nodes]=...; //the amount of memory at each node. int memvnf[v in V]=...; //an instance of type v may need amount of memory. //bandwidth float u[v in V][n in Nodes]=...; //transmission rate. float C[l in L]=...; //Delays float Dvn[v in V][n in Nodes]=...; //denote the expected nodal delay for type v NF at node n. float Dlink[l in L]=...; float Dflow[f in F] =...; float M=100; //a big amount //..................................................................................... int nj[0..3];//I don't know is it correct or not??????????? //........................................................................................ //MAIN DECISION VARIABLES dvar int I[v in V][Nodes][f in F][j in 1..2] in 0..1; //denotes that an NF instance v hosted at node n is used by the j-th service on the service chain of flow f. dvar int IL[l in L][f in F][1..2][Nodes][2..3][Nodes] in 0..1; //denotes that link l is used by flow f to route from the j-th to (j + 1)-th NF service, hosted at node nj and nj+1. dvar int Y[v in V][n in Nodes]; //represents the number of NF type v instances that are hosted at node n. //New decision variables dvar int G[ n in Nodes] in 0..1; //counts the number of Nodes. dvar int S[l in L] in 0..1; //Decision variables related with non linear equations dvar int z[l in L][f in F][1..2][Nodes][2..3][Nodes][V] in 0..1; //...................................................................................... //Related with floor function dexpr float x[f in F] = sum(v in V, n in Nodes, j in 1..2) I[v][n][f][j] / J[f]; dvar int g[f in F]; dvar float floorequ[i in F] in 0..0.99999; //MAIN objective functions dexpr float objmodel1 = sum(f in F) g[f]; dexpr float objmodel2 = sum(n in Nodes, v in V) (Y[v][n] * cpuvnf[v] / Cpunode[n]);//to minimize the use of cores dexpr float objmodel3 = sum(l in L, j in 1..2, f in F: nj[j] in Routes[f]) (IL[l][f][j] [nj[j]][j+1][nj[j+1]] * landa[f] / C[l]); //to minimize the utilization of link capacities. //New objective functions dexpr float objmodel4 = sum(l in L, j in 1..2, f in F: nj[j] in Routes[f]) (IL[l][f][j] [nj[j]][j+1][nj[j+1]] <= M * S[l]);//jitter dexpr float objmodel5 = sum(n in Nodes) G[n];//min capex dexpr float objmodel6 = sum(n in Nodes, v in V) Y[v][n]*Cpunode[n];//min opex maximize staticLex(objmodel1, objmodel2, objmodel3, objmodel4, objmodel5, objmodel6); //........................................................................................ subject to{ forall (l in L) cons1: sum(j in 1..2, f in F: nj[j] in Routes[f]) (IL[l][f][j][nj[j]][j+1][nj[j+1]] * landa[f]) <= C[l]; forall (n in Nodes) cons2: sum(v in V) Y[v][n] * cpuvnf[v] <= Cpunode[n];//cpu forall (n in Nodes) cons3: sum(v in V) Y[v][n] * memvnf[v] <= Memnode[n];//mem forall (v in V, n in Nodes) cons4: sum(f in F, j in 1..2) I[v][n][f][j] * landa[f] <= u[v][n]; forall (n in Nodes, v in V, f in F, j in 1..2) cons5: Y[v][n] >= I[v][n][f][j]; forall (f in F, j in 1..2) cons6: sum(n in Nodes, v in V) I[v][n][f][j] == 1; forall (i in F) cons7: x[i]==g[i]+floorequ[i]; //Total delays forall (f in F) cons8: sum(l in L, j in 1..2: nj[j] in Routes[f]) IL[l][f][j][nj[j]][j+1][nj[j+1]] * Dlink[l] + sum(j in 1..2, v in V, n in Nodes) I[v][n][f][j] * Dvn[v][n] <= Dflow[f]; //convert non_linear_equations to new linear constraints forall (j in 1..2, f in F: nj[j] in Routes[f], v in V) cons9: sum(l in Lout[nj[j]]) z[l][f][j][nj[j]][j+1][nj[j+1]][v] == 1; forall (j in 1..2, f in F: nj[j] in Routes[f], l in Lout[nj[j]], v in V) { cons10: 3 * z[l][f][j][nj[j]][j+1][nj[j+1]][v] <= (IL[l][f][j][nj[j]][j+1][nj[j+1]] + I[v] [nj[j]][f][j] + I[v][nj[j+1]][f][j+1]); cons11: z[l][f][j][nj[j]][j+1][nj[j+1]][v] >= (IL[l][f][j][nj[j]][j+1][nj[j+1]] + I[v] [nj[j]][f][j] + I[v][nj[j+1]][f][j+1]) - 2; } //convert non_linear_equations to new linear constraints forall (j in 1..2, f in F: nj[j] in Routes[f], v in V) cons12: sum(l in Lin[nj[j+1]]) z[l][f][j][nj[j]][j+1][nj[j+1]][v] == 1; forall (j in 1..2, f in F: nj[j] in Routes[f], l in Lin[nj[j+1]], v in V) { cons13: 3 * z[l][f][j][nj[j]][j+1][nj[j+1]][v] <= (IL[l][f][j][nj[j]][j+1][nj[j+1]] + I[v][nj[j]][f][j] + I[v][nj[j+1]][f][j+1]); cons14: z[l][f][j][nj[j]][j+1][nj[j+1]][v] >= (IL[l][f][j][nj[j]][j+1][nj[j+1]] + I[v] [nj[j]][f][j] + I[v][nj[j+1]][f][j+1]) - 2; } forall(j in 1..2, f in F: nj[j] in Routes[f], n in Nodes){ cons15: sum(l in Lout[n]) IL[l][f][j][nj[j]][j+1][nj[j+1]] <= 1; cons16: sum(l in Lin[n]) IL[l][f][j][nj[j]][j+1][nj[j+1]] <= 1; } forall (j in 1..2, f in F: nj[j] in Routes[f], n in Nodes) if(n != nj[j] && n != nj[j+1]) cons17: (sum(l in Lin[n]) IL[l][f][j][nj[j]][j+1][nj[j+1]]) - (sum(l in Lout[n]) IL[l][f] [j][nj[j]][j+1][nj[j+1]]) == 0; forall (j in 1..2, f in F: nj[j] in Routes[f], v in V) { cons18: sum(l in Lout[nj[1]]) IL[l][f][1][nj[1]][2][nj[2]] == I[v][nj[2]][f][2]; cons19: sum(l in Lin[nj[2]]) IL[l][f][1][nj[1]][2][nj[2]] == I[v][nj[2]][f][2]; } forall (j in 1..2, f in F: nj[j] in Routes[f], v in V) { cons20: sum(l in Lin[nj[3]]) IL[l][f][2][nj[2]][3][nj[3]] == I[v][nj[2]][f][2]; cons21: sum(l in Lout[nj[2]]) IL[l][f][2][nj[2]][3][nj[3]] == I[v][nj[2]][f][2]; } forall(n in Nodes) cons22: sum(v in V) Y[v][n] <= M * G[n]; } assert forall(f in F) g[f]==floor(x[f]); execute DISPLAY_After_SOLVE { writeln("objmodel1==", objmodel1, " objmodel2==", objmodel2, " objmodel3==", objmodel3, " objmodel4==", objmodel4, " objmodel5==", objmodel5 , " objmodel6==", objmodel6); } .dat file Cpunode=[4, 4, 5, 4, 4, 5, 4, 10, 4]; cpuvnf=[1, 1, 1, 1, 1]; Memnode=[8, 8, 16, 16, 16, 16, 8, 16, 8]; memvnf=[2, 2, 4, 4, 4];//or [1,4,8,8,8] u=[[10,10,10,10,10,10,10,10,10] [10,10,10,10,10,10,10,10,10] [10,10,10,10,10,10,10,10,10] [10,10,10,10,10,10,10,10,10] [10,10,10,10,10,10,10,10,10]]; //5*9 matrix C=[1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000]; Dvn=[[0.003, 0.003, 0.003, 0.003, 0.003, 0.003, 0.003, 0.003, 0.003] [0.003, 0.003, 0.003, 0.003, 0.003, 0.003, 0.003, 0.003, 0.003] [0.003, 0.003, 0.003, 0.003, 0.003, 0.003, 0.003, 0.003, 0.003] [0.003, 0.003, 0.003, 0.003, 0.003, 0.003, 0.003, 0.003, 0.003] [0.003, 0.003, 0.003, 0.003, 0.003, 0.003, 0.003, 0.003, 0.003] ]; //5*9 matrix Dlink=[0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01]; Dflow=[0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04];
you could turn off warnings: Or you could make sure that all the indexes in the arrays are used. For instance dvar int x[1..3]; subject to { x[1]==1; x[3]==2; } give a warning about x[2] never used whereas {int} s={1,3}; dvar int x[s]; subject to { x[1]==1; x[3]==2; } is OK
Computing Irreducible Inconsistent Subsystem (IIS) using Julia JuMP (Gurobi)
Trying to compute IIS for my stupidly overcomplicated model. I'll include the whole model for clarity: using JuMP using Gurobi import XLSX roster = Model(Gurobi.Optimizer) Intern = 1:11 #i Week = 1:52 #k Rotation = 1:23 #j Leave_week = 1:3 Dec_leave = 1:2 M = 1000 clins = 7:52 non_clins = 5:52 early = 5:28 gen = [5,8] #variables(roster, begin x[Intern,Week, Rotation], Bin y[Intern,Week, Rotation], Bin L[Leave_week, Week], Bin D[Dec_leave, Intern], Bin s[Intern, Week], Bin g[Intern, gen], Bin end ) #physical constraint #constraint(roster, phys[i in Intern, k in Week], sum(x[i,k,j] for j in Rotation) == 1) #Rotation capacity rots = [1,2,3,4,5,6,7,8,9,10,12,13,15,16,17,18,19] cap_rhs = [2,1,1,1,1,1,1,1,1, 1, 1, 1, 2, 1, 1, 2, 1, 1] cap = #constraint(roster, [(b,d) in zip(rots, cap_rhs), k in 1:52], sum(x[i,k,b] for i in Intern) <= d) #dispensary disp = #constraint(roster, [i in Intern], sum(x[i,k,j] for k in Week, j in 14:18) >= 5) disp1 = #constraint(roster, [i in Intern], sum(x[i,k,j] for k in 29:40, j in 14:18) >= 1) disp2 = #constraint(roster, [i in Intern], sum(x[i,k,j] for k in 41:52, j in 14:18) >= 1) clay_cap_o = #constraint(roster, [k in 1:4], sum(x[i,k,14] for i in Intern) <=3) clay_cap_o = #constraint(roster, [k in 5:52], sum(x[i,k,14] for i in Intern) <=2) #Orientation IP_1 = #constraint(roster, [i in Intern], sum(x[i,k,1] for k in 1:6) >= 1) IP_3_1 = #constraint(roster, [i in Intern], sum(s[i,k] for k in 1:5) <= 1) IP_3_11 = #constraint(roster, sum(s[i,k] for i in Intern, k in 1:5) == 10) IP_3_2 = #constraint(roster, [i in Intern, k in 1:4], x[i,k,1] == s[i,k]) # IP_lazy = #constraint(roster, [(i,k) in zip(Intern, [1 1 2 2 3 3 4 4 5 5 6])], x[i,k,1] ==1) orien = #constraint(roster, [i in Intern], sum(x[i,k,j] for k in 1:4, j in [1,14,15,16,17,18] ) == 4) orien1 = #constraint(roster, [i in Intern, j in [1,14,15,16,17,18]], sum(x[i,k,j] for k in 1:4 ) <= 2) #leave # 2 weeks leave #constraint(roster, [i in Intern], sum(x[i,k,j] for k in Week, j in 20:22) == 2) week1_dvar = #constraint(roster, sum(L[1, k] for k in 17:22) == 1) #constraint(roster, week1[k in 17:22], sum(x[i,k,20] for i in Intern) == 11*L[1,k]) #constraint(roster, week2_3_dvar[l in 2:3], sum(L[l, k] for k in 35:41) == 1) #constraint(roster, week2_3[(l, j, rhs) in zip(2:3, 21:22, [6,5]), k in 35:41], sum(x[i,k,j] for i in Intern) == rhs*L[l, k] ) #constraint(roster, max_leave[i in Intern], sum(x[i,k,j] for j in 20:22, k in Week) ==2) ## - Dec_leave #constraint(roster,[i in Intern], sum(D[l,i] for l in 1:2) == 1) #constraint(roster, [(l,d) in zip(1:2,[6,5])], sum(D[l,i] for i in Intern) == d) #constraint(roster, [i in Intern, (l,b) in zip(1:2, [49:50, 51:52])], sum(x[i,k,23] for k in b) == 2*D[l,i]) #constraint(roster, [i in Intern], sum(x[i,k,23] for k in Week) == 2) #MIC MIC_1_dvar = #constraint(roster, [i in Intern], sum(y[i,k,4] for k in 5:27 ) == 1) MIC_2_dvar = #constraint(roster, [i in Intern], sum(y[i,k,4] for k in 29:51 ) == 1) MIC = #constraint(roster, [ i in Intern, k in 5:27], 2 - sum(x[i, k + alpha, 4] for alpha in 0:1 ) <= M*(1-y[i,k,4])) MIC = #constraint(roster, [ i in Intern, k in 29:51], 2 - sum(x[i, k + alpha, 4] for alpha in 0:1 ) <= M*(1-y[i,k,4])) #gen_med g_vars = #constraint(roster, [i in Intern], sum(g[i,m] for m in gen) ==1) gen_duration_dvar = #constraint(roster, [(b,d) in zip(gen,[6,7]), i in Intern], sum(y[i,k,b] for k in 1:(52 - (d-1) ) ) == g[i,b]) gen_limit = #constraint(roster, [(b,d) in zip(gen,[6,7]), i in Intern], sum(x[i,k,b] for k in Week) == g[i,b]*d) gen_durations = #constraint(roster, [(b,d) in zip(gen,[6,7]), i in Intern, k in 1:(52 - (d-1) )], d - sum(x[i, k + alpha, b] for alpha in 0:(d-1) ) <= M*(1-y[i,k,b])) ed_with_gen = #constraint(roster, [i in Intern, k in 2:50], y[i,k,23] - x[i,k-1,8] - x[i,k+2,8] <= (1-g[i,5])) #qum qum_1 = #constraint(roster, [i in Intern], sum(x[i,k,13] for k in early) >= 1) qum_2 = #constraint(roster, [i in Intern], sum(x[i,k,13] for k in 1:39) == 2) # duration dur_rot = [2,6,7,9,10,11,19] durs = [2,4,2,3, 3, 4, 2] duration_dvar = #constraint(roster, [(b,d) in zip(dur_rot, durs), i in Intern], sum(y[i,k,b] for k in 1:(52 - (d-1) ) ) == 1) durations = #constraint(roster, [(b,d) in zip(dur_rot, durs), i in Intern, k in 1:(52 - (d-1) )], d - sum(x[i, k + alpha, b] for alpha in 0:(d-1) ) <= M*(1-y[i,k,b])) AP_dur_var = #constraint(roster, [i in Intern], sum(y[i,k,3] for k in 5:35) == 1) AP_dur = #constraint(roster, [i in Intern, k in 5:35], 2 - sum(x[i,k + alpha, 3] for alpha in 0:1) <= M*(1 - y[i,k,3])) AP_third = #constraint(roster,[i in Intern], sum(x[i,k,3] for k in 37:52) == 1) # rotations_lengths completion = #constraint(roster, [(j,c,d) in zip([1,2,3,4,6,7,9,10,11,12,19], [ 1:28,clins,non_clins, non_clins,clins,clins, clins, clins, 29:52, early, clins], [3,2,3,4,4,2,3, 3, 4, 1, 2]), i in Intern], sum(x[i,k,j] for k in c) == d) IP_soft = #constraint(roster, [i in Intern], sum(x[i,k,1] for k in Week) >= 5) whole_year = #constraint(roster, [(j,d) in zip([1,2,3,4,6,7,9,10,11,12,13,19], [5,2,3,4,4,2,3, 3, 4, 1, 2, 2]), i in Intern], sum(x[i,k,j] for k in Week) == d) # public holiday constraints no_pubs = #constraint(roster, [i in Intern, k in [4,7,10,13,14,22,24,29,39,44,52], j in [12,13]], x[i,k,j] == 0 ) z = #expression(roster, sum(x[i,k,j] for i in Intern, j in Rotation, k in Week)) obj_z = #objective(roster, Max, z) optimize!(roster) Tried a few approaches, such as those recommended here and here but ran into errors. Tried Gurobi.computeIIS(roster) but came up with: The C API of Gurobi.jl has been rewritten to expose the complete C API, and all old functions have been removed. For more information, see the Discourse announcement: https://discourse.julialang.org/t/ann-upcoming-breaking-changes-to-cplex-jl-and-gurobi-jl Here is a brief summary of the changes. ... Any help or advise appreciated. Please feel free to edit the example for clarity. Many thanks.
This is a little advanced, and some of the plumbing is missing (you have to use direct_model at present), but you can go: using JuMP, Gurobi model = direct_model(Gurobi.Optimizer()) #variable(model, x >= 0) #constraint(model, c1, x <= -1) #constraint(model, c2, 2 * x <= 1) optimize!(model) #assert termination_status(model) == MOI.INFEASIBLE compute_conflict!(model) julia> MOI.get(model, MOI.ConstraintConflictStatus(), LowerBoundRef(x)) IN_CONFLICT::ConflictParticipationStatusCode = 1 julia> MOI.get(model, MOI.ConstraintConflictStatus(), c1) IN_CONFLICT::ConflictParticipationStatusCode = 1 julia> MOI.get(model, MOI.ConstraintConflictStatus(), c2) NOT_IN_CONFLICT::ConflictParticipationStatusCode = 0
Finding the proportion of connected nodes in igraph that have a certain attribute value
So currently I run this code to find the number of connected nodes with a value of 1 for the node attribute "a". How would I modify the following code so that it outputs proportion of connected nodes with attribute "a" value of 1 rather than the actual count? library(igraph) g <- make_empty_graph (2) %>% set_vertex_attr("a", value = 1) %>% set_vertex_attr("xyz", value = 1) %>% add_vertices(2, color = 2, "a" = 1) %>% add_vertices(2, color = 4, "a" = 1) %>% add_edges(c(1,2, 2,1, 1,5, 5,1, 1,4 ,4,1)) V(g)$xyz <- sapply(V(g), function(x) { NeighborList = neighbors(g, x) ; length(NeighborList[NeighborList$a == 1]) } ) V(g)$xyz
You can just divide by the number of neighbors, except when there are none. sapply(V(g), function(x) { NeighborList = neighbors(g, x) ; ifelse(length(NeighborList) > 0, length(NeighborList[NeighborList$a == 1])/length(NeighborList),0) } ) [1] 1 1 0 1 1 0
How to solve ordinary differential equations with time dependent parameters in R?
I am trying to replicate an existing mathematical model in order to get "control" data for an independent research project. Link to article. I want to solve a system of five ordinary differential equations in R, using the deSolve package. However, most of the parameters are seasonal; the previous researchers used the 'pchip' function from the pracma package in order to create functions for the parameters: pchip is a `shape-preserving' piecewise cubic Hermite polynomial approach that apptempts to determine slopes such that function values do not overshoot data values. pchipfun is a wrapper around pchip and returns a function. Both pchip and the function returned by pchipfun are vectorized. xi and yi must be vectors of the same length greater or equal 3 (for cubic interpolation to be possible), and xi must be sorted. pchip can be applied to points outside [min(xi), max(xi)], but the result does not make much sense outside this interval. graphed, 'beta' parameter over five years This is the code I have (just trying to get outputs for one year): x <- c(0, 1, 2, 3, 4) ybeta <- c(500, 1500, 500, 0, 500) yk <- c(8000, 12, 0, 8000, 8000) yrec <- c(0.25, 0.25, 0.25, 0, 0.25) yfb <- c(1.5, 1.5, 1.5, 1.5, 1.5) yno <- c(0, 0, 0, 0.00649, 0) yni <- c(0, 0, 0, 0.00649, 0) ypo <- c(0.08511, 0.08511, 0.08511, 0, 0.08511) ypi <- c(0.16936, 0.16936, 0.16936, 0, 0.16936) yalpha <- c(0.55, 0.12, 0.24, 0, 0.55) yW <- 0.1 ydep <- c(0.2061 * yW, 0.2835 * yW, 0.2527 * yW, yW, 0.2061 * yW) ydec <- c(0.006470, 0.023300, 0.015683, 0, 0.006470) yup <- c(0.15, 0.15, 0.15, 0.15, 0.15) xs <- seq(0, 1, len = 365) nosema <- function(time, state, parameters) { with(as.list(c(state, parameters)), { H <- Ho + Hi Fr <- Fo + Fi Z <- H + Fr dHo <- pchip(x, ybeta, xs)* (Z^n) / (pchip(x, yk, xs)^n + Z^n) - pchip(x, yrec, xs) * Ho + pchip(x, yfb, xs) * Fr/Z * Fo - pchip(x, yno, xs) * Ho - pchip(x, yalpha, xs) * Ho * E/(sr + E) dHi <- -pchip(x, yrec, xs) * Hi + pchip(x, yfb, xs) * Fr/Z * Fi - pchip(x, yni, xs) * Hi + pchip(x, yalpha, xs) * Ho * E/(sr + E) dFo <- pchip(x, yrec, xs) * Ho - pchip(x, yfb, xs) * Fr/Z * Fo - pchip(x, ypo, xs) * Fo dFi <- pchip(x, yrec, xs) * Hi - pchip(x, yfb, xs) * Fr/Z * Fi - pchip(x, ypi, xs) * Fi dE <- pchip(x, ydep, xs) * Hi - pchip(x, ydec, xs) * E - pchip(x, yup, xs) * Ho * E / (sr + E) return(list(c(dHo, dHi, dFo, dFi, dE))) }) } init <- c(Ho = 10^4, Hi = 0, Fo = 10000, Fi = 0, E = 0) parameters <- c(n = 2, sr = 10000) out <- ode(y = init, times = seq(0, 365, len = 365), func = nosema, parms = parameters) out <- as.data.frame(out) out$time <- NULL head(out) However, when I run the code above... The number of derivatives returned by func() (1825) must equal the length of the initial conditions vector (5) I think I have an inkling of why this isn't working, but I have no clue how to go about fixing it. Does anyone have any advice on how to proceed?
library(deSolve) ?forcings see page 72ff at http://desolve.r-forge.r-project.org/user2014/tutorial.pdf
Determine fourth set of UV coordinates from the first three
Say that I have a quad, and I am applying a pool table texture to it. I know the UV coordinates for the first three vertices (highlighted in blue) but not the fourth. For example in the image above, I know that the top left coordinates are [0, 0], the top right coordinates are [1, 0] and the bottom left are [0, 1]. How can I mathematically determine the bottom right UV coordinates are [1, 1]? I want to figure out what the fourth set of UV coordinates will be mathematically, such that the texture displayed in the triangle I do know the UV coordinates of will fit in the quad. I need to be able to handle complex situations. The UV coordinates and X, Y and Z coordinates could be anything, except I do know that it will always be a flat face. Finally, I need to be able to calculate this for faces with any number of vertices. My initial approach to this involved checking how much the U coordinate changes over a certain X distance, and then the same for Y and Z until I found a relationship, but I keep finding new exceptions to that logic and I'm wondering if there's a simpler way. How can I interpolate the UVs to calculate the fourth, fifth... nth point? In no particular coding language - I'm just looking for the approach.
Compute the barycentric coordinates of the vertices for which the U/V attributes are missing and use these coordinates to extrapolate the missing attributes (U/V or any other actually). Here is a complete derivation and solution for an additional face vertex with position P and U/V coordinates T: P1 = (x1, y1, z1), T1 = (u1, v1) P2 = (x2, y2, z2), T2 = (u2, v2) P3 = (x3, y3, z3), T3 = (u3, v3) P = (x , y , z ), T = (u , v ) = ? A point P on the plane supporting the triangle (P1, P2, P3) (and thus the whole face) has barycentric coordinates l1, l2, l3: P = l1 P1 + l2 P2 + l3 P3 with l1 + l2 + l3 = 1 This can be rewritten as: P - P1 = ( l1 - 1 ) P1 + l2 P2 + l3 P3 = -( l2 + l3 ) P1 + l2 P2 + l3 P3 = l2 ( P2 - P1 ) + l3 ( P3 - P1 ) with l1 = 1 - l2 - l3 Projecting the vector V = P - P1 onto the vectors V21 = P2 - P1 and V31 = P3 - P1 gives: < V, V21 > = l2 < V21, V21 > + l3 < V31, V21 > < V, V31 > = l2 < V21, V31 > + l3 < V31, V31 > where < V1, V2 > is the dot product of 3D vectors V1 and V2. So (l1, l2, l3) can be found for P by solving the linear system: G L = ( < V21, V21 > < V31, V21 > ) ( l2 ) = ( < V, V21 > ) = D ( < V21, V31 > < V31, V31 > ) ( l3 ) ( < V, V31 > ) L = ( l2 ) = G^-1 D ( l3 ) and l1 = 1 - l2 - l3 This can be solved explicitly: d = < V21, V21 > < V31, V31 > - < V21, V31 >^2 l1 = 1 - l2 - l3 l2 = ( < V31, V31 > < V, V21 > - < V21, V31 > < V, V31 > ) / d l3 = ( < V21, V21 > < V, V31 > - < V21, V31 > < V, V21 > ) / d Note that since the position P of the additional vertex is outside the triangle (P1, P2, P3), the inequalities l1, l2, l3 >= 0 won't necessarily hold anymore. Finally compute the extrapolated U/V coordinates T of the additional vertex: T = l1 T1 + l2 T2 + l3 T3