FICO
FICO Xpress Optimization Examples Repository
FICO Optimization Community FICO Xpress Optimization Home
Back to examples browserNext example

Mandelbrot

Description
This model implements a queued execution of remote models (modelmandelhttpsub.mos) with result graphics displayed in a web browser controlled by the root model mandelhttp.mos.

On each node in the specified list we start K model instances. Each submodel instance is sent a rectangular area for which to calculate the pixel color values based on the Mandelbrot function f(z) = z^2 + c where z,c are complex numbers.

The results are passed back via a file (located at the same place as this model, no write access to remote instances is required). Once the result has been displayed, the submodel is restarted for a new data set.

mandelhttp.zip[download all files]

Source Files
By clicking on a file name, a preview is opened at the bottom of this page.
mandelhttp.mos[download]
mandelhttpsub.mos[download]

Data Files





mandelhttpsub.mos

(!*******************************************************
   Mosel Example Problems
   ======================

   file mandelhttpsub.mos
   ``````````````````````
   Mandelbrot function: f(z) = z^2 + c with z,c complex numbers.

   Submodel for solving the function for all points in the
   rectangular box (MINX,MINY,MAXX,MAXY).

   - Testing HTTP -

   *** Not intended to be run standalone - run from mandelhttp.mos ***
   
   (c) 2013 Fair Isaac Corporation
       author: Y. Colombani, S. Heipcke, May 2013
  *******************************************************!)

model "mandelbrot (sub)"
  uses "mmjobs" 
 
  parameters
    CONFIG=3                    ! Color scheme and zoom
    MINX = 0                    ! Min/Max X/Y coordinates of box solved by this model
    MAXX = 0
    MINY = 0
    MAXY = 0
    HX = 0.1                    ! Distance between points
    HY = 0.1
    NUM = 0                     ! Model ID
    MAX_ITER = 1000             ! Iteration limit for Mandelbrot function
  end-parameters	

  declarations
    pixel=record
           r,g,b:integer
	  end-record
    RS=0..((MAXX-MINX+1)*(MAXY-MINY+1)*3)
    SOL:record
            x,y:integer
            h,w:integer
	    points:array(RS) of integer
	   end-record
    black,point:pixel
  end-declarations 
 

!***************** Subroutines ******************
  function PXcolor(r,g,b:real): pixel
    returned.r:=round(r)
    returned.g:=round(g)
    returned.b:=round(b)
  end-function 

! Color for a pixel on the screen    (x0,y0) = (x,y) co-ordinates of pixel
  function pixel_color(x0,y0: real): pixel
    x:= 0.0
    y:= 0.0
    iterct:= 0
 
    while ( x*x + y*y <= (2*2)  and  iterct < MAX_ITER ) do
      xtemp := x*x - y*y + x0
      y:= 2*x*y + y0
      x:= xtemp
      iterct += 1
    end-do
    if iterct = MAX_ITER then 
      returned := black
    else 
      if CONFIG = 0 then
       returned := PXcolor(round((iterct) mod 255), 255-round(iterct*2 mod 255), 255-round((ln(iterct)*40) mod 255))
      elif CONFIG = 1 then
       returned := PXcolor(round((iterct) mod 255), round((ln(iterct)*50) mod 255), 255-round(iterct mod 255))
      elif CONFIG = 2 then
       returned := PXcolor(iterct*3 mod 255, round(iterct*2 mod 255), 255-round(iterct*3 mod 255))
      elif CONFIG = 3 then
       returned := PXcolor(ln(iterct)*50 mod 255, round(iterct*2 mod 255), 255-round(iterct*1.5 mod 255))
      elif CONFIG = 4 then
       returned := PXcolor(255-(iterct mod 255), 255-round(iterct*2 mod 255), round(iterct mod 255))
      end-if
    end-if  
 end-function

!***************** Main ******************
  
 ! Do the actual calculation for a box and save the solution
   c:=0
   forall(y0 in MINY..MAXY,x0 in MINX..MAXX)
   do
    point:=pixel_color(x0*HX,y0*HY)
    SOL.points(c):=point.r
    SOL.points(c+1):=point.g
    SOL.points(c+2):=point.b
    c+=3
   end-do
   SOL.x:=MINX
   SOL.y:=MINY
   SOL.w:=MAXX-MINX+1
   SOL.h:=MAXY-MINY+1
   initializations to "bin:rmt:solmod"+NUM+".txt"
     SOL as "sol"
   end-initializations

   exit(NUM)

end-model 

Back to examples browserNext example