init
This commit is contained in:
13
lab1/CMakeLists.txt
Normal file
13
lab1/CMakeLists.txt
Normal file
@@ -0,0 +1,13 @@
|
||||
cmake_minimum_required(VERSION 3.6)
|
||||
project(Lab1)
|
||||
|
||||
set(SFML_COMPONENTS graphics window system network audio)
|
||||
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
find_package(SFML 2.1 COMPONENTS ${SFML_COMPONENTS} REQUIRED)
|
||||
message(STATUS "SFML found: ${SFML_FOUND}")
|
||||
include_directories(${SFML_INCLUDE_DIR})
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
|
||||
|
||||
set(SOURCE_FILES main.cpp mtrand.cpp mtrand.h)
|
||||
add_executable(Lab1 ${SOURCE_FILES})
|
||||
target_link_libraries (Lab1 ${SFML_LIBRARIES} ${SFML_DEPENDENCIES})
|
||||
BIN
lab1/CS417_Lab1.pdf
Normal file
BIN
lab1/CS417_Lab1.pdf
Normal file
Binary file not shown.
369
lab1/FindSFML.cmake
Normal file
369
lab1/FindSFML.cmake
Normal file
@@ -0,0 +1,369 @@
|
||||
# This script locates the SFML library
|
||||
# ------------------------------------
|
||||
#
|
||||
# Usage
|
||||
# -----
|
||||
#
|
||||
# When you try to locate the SFML libraries, you must specify which modules you want to use (system, window, graphics, network, audio, main).
|
||||
# If none is given, the SFML_LIBRARIES variable will be empty and you'll end up linking to nothing.
|
||||
# example:
|
||||
# find_package(SFML COMPONENTS graphics window system) # find the graphics, window and system modules
|
||||
#
|
||||
# You can enforce a specific version, either MAJOR.MINOR or only MAJOR.
|
||||
# If nothing is specified, the version won't be checked (i.e. any version will be accepted).
|
||||
# example:
|
||||
# find_package(SFML COMPONENTS ...) # no specific version required
|
||||
# find_package(SFML 2 COMPONENTS ...) # any 2.x version
|
||||
# find_package(SFML 2.4 COMPONENTS ...) # version 2.4 or greater
|
||||
#
|
||||
# By default, the dynamic libraries of SFML will be found. To find the static ones instead,
|
||||
# you must set the SFML_STATIC_LIBRARIES variable to TRUE before calling find_package(SFML ...).
|
||||
# Since you have to link yourself all the SFML dependencies when you link it statically, the following
|
||||
# additional variables are defined: SFML_XXX_DEPENDENCIES and SFML_DEPENDENCIES (see their detailed
|
||||
# description below).
|
||||
# In case of static linking, the SFML_STATIC macro will also be defined by this script.
|
||||
# example:
|
||||
# set(SFML_STATIC_LIBRARIES TRUE)
|
||||
# find_package(SFML 2 COMPONENTS network system)
|
||||
#
|
||||
# On Mac OS X if SFML_STATIC_LIBRARIES is not set to TRUE then by default CMake will search for frameworks unless
|
||||
# CMAKE_FIND_FRAMEWORK is set to "NEVER" for example. Please refer to CMake documentation for more details.
|
||||
# Moreover, keep in mind that SFML frameworks are only available as release libraries unlike dylibs which
|
||||
# are available for both release and debug modes.
|
||||
#
|
||||
# If SFML is not installed in a standard path, you can use the SFML_ROOT CMake (or environment) variable
|
||||
# to tell CMake where SFML is.
|
||||
#
|
||||
# Output
|
||||
# ------
|
||||
#
|
||||
# This script defines the following variables:
|
||||
# - For each specified module XXX (system, window, graphics, network, audio, main):
|
||||
# - SFML_XXX_LIBRARY_DEBUG: the name of the debug library of the xxx module (set to SFML_XXX_LIBRARY_RELEASE is no debug version is found)
|
||||
# - SFML_XXX_LIBRARY_RELEASE: the name of the release library of the xxx module (set to SFML_XXX_LIBRARY_DEBUG is no release version is found)
|
||||
# - SFML_XXX_LIBRARY: the name of the library to link to for the xxx module (includes both debug and optimized names if necessary)
|
||||
# - SFML_XXX_FOUND: true if either the debug or release library of the xxx module is found
|
||||
# - SFML_XXX_DEPENDENCIES: the list of libraries the module depends on, in case of static linking
|
||||
# - SFML_LIBRARIES: the list of all libraries corresponding to the required modules
|
||||
# - SFML_FOUND: true if all the required modules are found
|
||||
# - SFML_INCLUDE_DIR: the path where SFML headers are located (the directory containing the SFML/Config.hpp file)
|
||||
# - SFML_DEPENDENCIES: the list of libraries SFML depends on, in case of static linking
|
||||
#
|
||||
# example:
|
||||
# find_package(SFML 2 COMPONENTS system window graphics audio REQUIRED)
|
||||
# include_directories(${SFML_INCLUDE_DIR})
|
||||
# add_executable(myapp ...)
|
||||
# target_link_libraries(myapp ${SFML_LIBRARIES})
|
||||
|
||||
# define the SFML_STATIC macro if static build was chosen
|
||||
if(SFML_STATIC_LIBRARIES)
|
||||
add_definitions(-DSFML_STATIC)
|
||||
endif()
|
||||
|
||||
# define the list of search paths for headers and libraries
|
||||
set(FIND_SFML_PATHS
|
||||
${SFML_ROOT}
|
||||
$ENV{SFML_ROOT}
|
||||
~/Library/Frameworks
|
||||
/Library/Frameworks
|
||||
/usr/local
|
||||
/usr
|
||||
/sw
|
||||
/opt/local
|
||||
/opt/csw
|
||||
/opt)
|
||||
|
||||
# find the SFML include directory
|
||||
find_path(SFML_INCLUDE_DIR SFML/Config.hpp
|
||||
PATH_SUFFIXES include
|
||||
PATHS ${FIND_SFML_PATHS})
|
||||
|
||||
# check the version number
|
||||
set(SFML_VERSION_OK TRUE)
|
||||
if(SFML_FIND_VERSION AND SFML_INCLUDE_DIR)
|
||||
# extract the major and minor version numbers from SFML/Config.hpp
|
||||
# we have to handle framework a little bit differently:
|
||||
if("${SFML_INCLUDE_DIR}" MATCHES "SFML.framework")
|
||||
set(SFML_CONFIG_HPP_INPUT "${SFML_INCLUDE_DIR}/Headers/Config.hpp")
|
||||
else()
|
||||
set(SFML_CONFIG_HPP_INPUT "${SFML_INCLUDE_DIR}/SFML/Config.hpp")
|
||||
endif()
|
||||
FILE(READ "${SFML_CONFIG_HPP_INPUT}" SFML_CONFIG_HPP_CONTENTS)
|
||||
STRING(REGEX REPLACE ".*#define SFML_VERSION_MAJOR ([0-9]+).*" "\\1" SFML_VERSION_MAJOR "${SFML_CONFIG_HPP_CONTENTS}")
|
||||
STRING(REGEX REPLACE ".*#define SFML_VERSION_MINOR ([0-9]+).*" "\\1" SFML_VERSION_MINOR "${SFML_CONFIG_HPP_CONTENTS}")
|
||||
STRING(REGEX REPLACE ".*#define SFML_VERSION_PATCH ([0-9]+).*" "\\1" SFML_VERSION_PATCH "${SFML_CONFIG_HPP_CONTENTS}")
|
||||
if (NOT "${SFML_VERSION_PATCH}" MATCHES "^[0-9]+$")
|
||||
set(SFML_VERSION_PATCH 0)
|
||||
endif()
|
||||
math(EXPR SFML_REQUESTED_VERSION "${SFML_FIND_VERSION_MAJOR} * 10000 + ${SFML_FIND_VERSION_MINOR} * 100 + ${SFML_FIND_VERSION_PATCH}")
|
||||
|
||||
# if we could extract them, compare with the requested version number
|
||||
if (SFML_VERSION_MAJOR)
|
||||
# transform version numbers to an integer
|
||||
math(EXPR SFML_VERSION "${SFML_VERSION_MAJOR} * 10000 + ${SFML_VERSION_MINOR} * 100 + ${SFML_VERSION_PATCH}")
|
||||
|
||||
# compare them
|
||||
if(SFML_VERSION LESS SFML_REQUESTED_VERSION)
|
||||
set(SFML_VERSION_OK FALSE)
|
||||
endif()
|
||||
else()
|
||||
# SFML version is < 2.0
|
||||
if (SFML_REQUESTED_VERSION GREATER 10900)
|
||||
set(SFML_VERSION_OK FALSE)
|
||||
set(SFML_VERSION_MAJOR 1)
|
||||
set(SFML_VERSION_MINOR x)
|
||||
set(SFML_VERSION_PATCH x)
|
||||
endif()
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# find the requested modules
|
||||
set(SFML_FOUND TRUE) # will be set to false if one of the required modules is not found
|
||||
foreach(FIND_SFML_COMPONENT ${SFML_FIND_COMPONENTS})
|
||||
string(TOLOWER ${FIND_SFML_COMPONENT} FIND_SFML_COMPONENT_LOWER)
|
||||
string(TOUPPER ${FIND_SFML_COMPONENT} FIND_SFML_COMPONENT_UPPER)
|
||||
set(FIND_SFML_COMPONENT_NAME sfml-${FIND_SFML_COMPONENT_LOWER})
|
||||
|
||||
# no suffix for sfml-main, it is always a static library
|
||||
if(FIND_SFML_COMPONENT_LOWER STREQUAL "main")
|
||||
# release library
|
||||
find_library(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE
|
||||
NAMES ${FIND_SFML_COMPONENT_NAME}
|
||||
PATH_SUFFIXES lib64 lib
|
||||
PATHS ${FIND_SFML_PATHS})
|
||||
|
||||
# debug library
|
||||
find_library(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG
|
||||
NAMES ${FIND_SFML_COMPONENT_NAME}-d
|
||||
PATH_SUFFIXES lib64 lib
|
||||
PATHS ${FIND_SFML_PATHS})
|
||||
else()
|
||||
# static release library
|
||||
find_library(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_STATIC_RELEASE
|
||||
NAMES ${FIND_SFML_COMPONENT_NAME}-s
|
||||
PATH_SUFFIXES lib64 lib
|
||||
PATHS ${FIND_SFML_PATHS})
|
||||
|
||||
# static debug library
|
||||
find_library(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_STATIC_DEBUG
|
||||
NAMES ${FIND_SFML_COMPONENT_NAME}-s-d
|
||||
PATH_SUFFIXES lib64 lib
|
||||
PATHS ${FIND_SFML_PATHS})
|
||||
|
||||
# dynamic release library
|
||||
find_library(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DYNAMIC_RELEASE
|
||||
NAMES ${FIND_SFML_COMPONENT_NAME}
|
||||
PATH_SUFFIXES lib64 lib
|
||||
PATHS ${FIND_SFML_PATHS})
|
||||
|
||||
# dynamic debug library
|
||||
find_library(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DYNAMIC_DEBUG
|
||||
NAMES ${FIND_SFML_COMPONENT_NAME}-d
|
||||
PATH_SUFFIXES lib64 lib
|
||||
PATHS ${FIND_SFML_PATHS})
|
||||
|
||||
# choose the entries that fit the requested link type
|
||||
if(SFML_STATIC_LIBRARIES)
|
||||
if(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_STATIC_RELEASE)
|
||||
set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_STATIC_RELEASE})
|
||||
endif()
|
||||
if(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_STATIC_DEBUG)
|
||||
set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_STATIC_DEBUG})
|
||||
endif()
|
||||
else()
|
||||
if(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DYNAMIC_RELEASE)
|
||||
set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DYNAMIC_RELEASE})
|
||||
endif()
|
||||
if(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DYNAMIC_DEBUG)
|
||||
set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DYNAMIC_DEBUG})
|
||||
endif()
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if (SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG OR SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE)
|
||||
# library found
|
||||
set(SFML_${FIND_SFML_COMPONENT_UPPER}_FOUND TRUE)
|
||||
|
||||
# if both are found, set SFML_XXX_LIBRARY to contain both
|
||||
if (SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG AND SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE)
|
||||
set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY debug ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG}
|
||||
optimized ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE})
|
||||
endif()
|
||||
|
||||
# if only one debug/release variant is found, set the other to be equal to the found one
|
||||
if (SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG AND NOT SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE)
|
||||
# debug and not release
|
||||
set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG})
|
||||
set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG})
|
||||
endif()
|
||||
if (SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE AND NOT SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG)
|
||||
# release and not debug
|
||||
set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE})
|
||||
set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE})
|
||||
endif()
|
||||
else()
|
||||
# library not found
|
||||
set(SFML_FOUND FALSE)
|
||||
set(SFML_${FIND_SFML_COMPONENT_UPPER}_FOUND FALSE)
|
||||
set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY "")
|
||||
set(FIND_SFML_MISSING "${FIND_SFML_MISSING} SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY")
|
||||
endif()
|
||||
|
||||
# mark as advanced
|
||||
MARK_AS_ADVANCED(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY
|
||||
SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE
|
||||
SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG
|
||||
SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_STATIC_RELEASE
|
||||
SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_STATIC_DEBUG
|
||||
SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DYNAMIC_RELEASE
|
||||
SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DYNAMIC_DEBUG)
|
||||
|
||||
# add to the global list of libraries
|
||||
set(SFML_LIBRARIES ${SFML_LIBRARIES} "${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY}")
|
||||
endforeach()
|
||||
|
||||
# in case of static linking, we must also define the list of all the dependencies of SFML libraries
|
||||
if(SFML_STATIC_LIBRARIES)
|
||||
|
||||
# detect the OS
|
||||
if(${CMAKE_SYSTEM_NAME} MATCHES "Windows")
|
||||
set(FIND_SFML_OS_WINDOWS 1)
|
||||
elseif(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
|
||||
set(FIND_SFML_OS_LINUX 1)
|
||||
elseif(${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD")
|
||||
set(FIND_SFML_OS_FREEBSD 1)
|
||||
elseif(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
|
||||
set(FIND_SFML_OS_MACOSX 1)
|
||||
endif()
|
||||
|
||||
# start with an empty list
|
||||
set(SFML_DEPENDENCIES)
|
||||
set(FIND_SFML_DEPENDENCIES_NOTFOUND)
|
||||
|
||||
# macro that searches for a 3rd-party library
|
||||
macro(find_sfml_dependency output friendlyname)
|
||||
# No lookup in environment variables (PATH on Windows), as they may contain wrong library versions
|
||||
find_library(${output} NAMES ${ARGN} PATHS ${FIND_SFML_PATHS} PATH_SUFFIXES lib NO_SYSTEM_ENVIRONMENT_PATH)
|
||||
if(${${output}} STREQUAL "${output}-NOTFOUND")
|
||||
unset(output)
|
||||
set(FIND_SFML_DEPENDENCIES_NOTFOUND "${FIND_SFML_DEPENDENCIES_NOTFOUND} ${friendlyname}")
|
||||
endif()
|
||||
endmacro()
|
||||
|
||||
# sfml-system
|
||||
list(FIND SFML_FIND_COMPONENTS "system" FIND_SFML_SYSTEM_COMPONENT)
|
||||
if(NOT ${FIND_SFML_SYSTEM_COMPONENT} EQUAL -1)
|
||||
|
||||
# update the list -- these are only system libraries, no need to find them
|
||||
if(FIND_SFML_OS_LINUX OR FIND_SFML_OS_FREEBSD OR FIND_SFML_OS_MACOSX)
|
||||
set(SFML_SYSTEM_DEPENDENCIES "pthread")
|
||||
endif()
|
||||
if(FIND_SFML_OS_LINUX)
|
||||
set(SFML_SYSTEM_DEPENDENCIES ${SFML_SYSTEM_DEPENDENCIES} "rt")
|
||||
endif()
|
||||
if(FIND_SFML_OS_WINDOWS)
|
||||
set(SFML_SYSTEM_DEPENDENCIES "winmm")
|
||||
endif()
|
||||
set(SFML_DEPENDENCIES ${SFML_SYSTEM_DEPENDENCIES} ${SFML_DEPENDENCIES})
|
||||
endif()
|
||||
|
||||
# sfml-network
|
||||
list(FIND SFML_FIND_COMPONENTS "network" FIND_SFML_NETWORK_COMPONENT)
|
||||
if(NOT ${FIND_SFML_NETWORK_COMPONENT} EQUAL -1)
|
||||
|
||||
# update the list -- these are only system libraries, no need to find them
|
||||
if(FIND_SFML_OS_WINDOWS)
|
||||
set(SFML_NETWORK_DEPENDENCIES "ws2_32")
|
||||
endif()
|
||||
set(SFML_DEPENDENCIES ${SFML_NETWORK_DEPENDENCIES} ${SFML_DEPENDENCIES})
|
||||
endif()
|
||||
|
||||
# sfml-window
|
||||
list(FIND SFML_FIND_COMPONENTS "window" FIND_SFML_WINDOW_COMPONENT)
|
||||
if(NOT ${FIND_SFML_WINDOW_COMPONENT} EQUAL -1)
|
||||
|
||||
# find libraries
|
||||
if(FIND_SFML_OS_LINUX OR FIND_SFML_OS_FREEBSD)
|
||||
find_sfml_dependency(X11_LIBRARY "X11" X11)
|
||||
find_sfml_dependency(LIBXCB_LIBRARIES "XCB" xcb libxcb)
|
||||
find_sfml_dependency(X11_XCB_LIBRARY "X11-xcb" X11-xcb libX11-xcb)
|
||||
find_sfml_dependency(XCB_RANDR_LIBRARY "xcb-randr" xcb-randr libxcb-randr)
|
||||
find_sfml_dependency(XCB_IMAGE_LIBRARY "xcb-image" xcb-image libxcb-image)
|
||||
endif()
|
||||
|
||||
if(FIND_SFML_OS_LINUX)
|
||||
find_sfml_dependency(UDEV_LIBRARIES "UDev" udev libudev)
|
||||
endif()
|
||||
|
||||
# update the list
|
||||
if(FIND_SFML_OS_WINDOWS)
|
||||
set(SFML_WINDOW_DEPENDENCIES ${SFML_WINDOW_DEPENDENCIES} "opengl32" "winmm" "gdi32")
|
||||
elseif(FIND_SFML_OS_LINUX)
|
||||
set(SFML_WINDOW_DEPENDENCIES ${SFML_WINDOW_DEPENDENCIES} "GL" ${X11_LIBRARY} ${LIBXCB_LIBRARIES} ${X11_XCB_LIBRARY} ${XCB_RANDR_LIBRARY} ${XCB_IMAGE_LIBRARY} ${UDEV_LIBRARIES})
|
||||
elseif(FIND_SFML_OS_FREEBSD)
|
||||
set(SFML_WINDOW_DEPENDENCIES ${SFML_WINDOW_DEPENDENCIES} "GL" ${X11_LIBRARY} ${LIBXCB_LIBRARIES} ${X11_XCB_LIBRARY} ${XCB_RANDR_LIBRARY} ${XCB_IMAGE_LIBRARY} "usbhid")
|
||||
elseif(FIND_SFML_OS_MACOSX)
|
||||
set(SFML_WINDOW_DEPENDENCIES ${SFML_WINDOW_DEPENDENCIES} "-framework OpenGL -framework Foundation -framework AppKit -framework IOKit -framework Carbon")
|
||||
endif()
|
||||
set(SFML_DEPENDENCIES ${SFML_WINDOW_DEPENDENCIES} ${SFML_DEPENDENCIES})
|
||||
endif()
|
||||
|
||||
# sfml-graphics
|
||||
list(FIND SFML_FIND_COMPONENTS "graphics" FIND_SFML_GRAPHICS_COMPONENT)
|
||||
if(NOT ${FIND_SFML_GRAPHICS_COMPONENT} EQUAL -1)
|
||||
|
||||
# find libraries
|
||||
find_sfml_dependency(FREETYPE_LIBRARY "FreeType" freetype)
|
||||
find_sfml_dependency(JPEG_LIBRARY "libjpeg" jpeg)
|
||||
|
||||
# update the list
|
||||
set(SFML_GRAPHICS_DEPENDENCIES ${FREETYPE_LIBRARY} ${JPEG_LIBRARY})
|
||||
set(SFML_DEPENDENCIES ${SFML_GRAPHICS_DEPENDENCIES} ${SFML_DEPENDENCIES})
|
||||
endif()
|
||||
|
||||
# sfml-audio
|
||||
list(FIND SFML_FIND_COMPONENTS "audio" FIND_SFML_AUDIO_COMPONENT)
|
||||
if(NOT ${FIND_SFML_AUDIO_COMPONENT} EQUAL -1)
|
||||
|
||||
# find libraries
|
||||
find_sfml_dependency(OPENAL_LIBRARY "OpenAL" openal openal32)
|
||||
find_sfml_dependency(OGG_LIBRARY "Ogg" ogg)
|
||||
find_sfml_dependency(VORBIS_LIBRARY "Vorbis" vorbis)
|
||||
find_sfml_dependency(VORBISFILE_LIBRARY "VorbisFile" vorbisfile)
|
||||
find_sfml_dependency(VORBISENC_LIBRARY "VorbisEnc" vorbisenc)
|
||||
find_sfml_dependency(FLAC_LIBRARY "FLAC" FLAC)
|
||||
|
||||
# update the list
|
||||
set(SFML_AUDIO_DEPENDENCIES ${OPENAL_LIBRARY} ${FLAC_LIBRARY} ${VORBISENC_LIBRARY} ${VORBISFILE_LIBRARY} ${VORBIS_LIBRARY} ${OGG_LIBRARY})
|
||||
set(SFML_DEPENDENCIES ${SFML_DEPENDENCIES} ${SFML_AUDIO_DEPENDENCIES})
|
||||
endif()
|
||||
|
||||
endif()
|
||||
|
||||
# handle errors
|
||||
if(NOT SFML_VERSION_OK)
|
||||
# SFML version not ok
|
||||
set(FIND_SFML_ERROR "SFML found but version too low (requested: ${SFML_FIND_VERSION}, found: ${SFML_VERSION_MAJOR}.${SFML_VERSION_MINOR}.${SFML_VERSION_PATCH})")
|
||||
set(SFML_FOUND FALSE)
|
||||
elseif(SFML_STATIC_LIBRARIES AND FIND_SFML_DEPENDENCIES_NOTFOUND)
|
||||
set(FIND_SFML_ERROR "SFML found but some of its dependencies are missing (${FIND_SFML_DEPENDENCIES_NOTFOUND})")
|
||||
set(SFML_FOUND FALSE)
|
||||
elseif(NOT SFML_FOUND)
|
||||
# include directory or library not found
|
||||
set(FIND_SFML_ERROR "Could NOT find SFML (missing: ${FIND_SFML_MISSING})")
|
||||
endif()
|
||||
if (NOT SFML_FOUND)
|
||||
if(SFML_FIND_REQUIRED)
|
||||
# fatal error
|
||||
message(FATAL_ERROR ${FIND_SFML_ERROR})
|
||||
elseif(NOT SFML_FIND_QUIETLY)
|
||||
# error but continue
|
||||
message("${FIND_SFML_ERROR}")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# handle success
|
||||
if(SFML_FOUND AND NOT SFML_FIND_QUIETLY)
|
||||
message(STATUS "Found SFML ${SFML_VERSION_MAJOR}.${SFML_VERSION_MINOR}.${SFML_VERSION_PATCH} in ${SFML_INCLUDE_DIR}")
|
||||
endif()
|
||||
|
||||
34
lab1/Shekel's_Foxhole_Data.txt
Normal file
34
lab1/Shekel's_Foxhole_Data.txt
Normal file
@@ -0,0 +1,34 @@
|
||||
C={0.806,0.517,0.1,0.908,0.965,0.669,0.524,0.902,0.351,0.876,0.462,
|
||||
0.491,0.463,0.741,0.352,0.869,0.813,0.811,0.0828,0.964,0.789,0.360,0.369,
|
||||
0.992,0.332,0.817,0.632,0.883,0.608,0.326};
|
||||
|
||||
Ai,j={{9.681,0.667,4.783,9.095,3.517,9.325,6.544,0.211,5.122,2.02},
|
||||
{9.4,2.041,3.788,7.931,2.882,2.672,3.568,1.284,7.033,7.374},
|
||||
{8.025,9.152,5.114,7.621,4.564,4.711,2.996,6.126,0.734,4.982},
|
||||
{2.196,0.415,5.649,6.979,9.510,9.166,6.304,6.054,9.377,1.426},
|
||||
{8.074,8.777,3.467,1.863,6.708,6.349,4.534,0.276,7.633,1.567},
|
||||
{7.650,5.658,0.720,2.764,3.278,5.283,7.474,6.274,1.409,8.208},
|
||||
{1.256,3.605,8.623,6.905,4.584,8.133,6.071,6.888,4.187,5.448},
|
||||
{8.314,2.261,4.24,1.781,4.124,0.932,8.129,8.658,1.208,5.762},
|
||||
{0.226,8.858,1.42,0.954,1.622,4.698,6.228,9.096,0.972,7.637},
|
||||
{7.305,2.228,1.242,5.928,9.133,1.826,4.06,5.204,8.713,8.247},
|
||||
{0.652,7.027,0.508,4.876,8.807,4.632,5.808,6.937,3.291,7.016},
|
||||
{2.699,3.516,5.847,4.119,4.461,7.496,8.817,0.69,6.593,9.789},
|
||||
{8.327,3.897,2.017,9.57,9.825,1.15,1.395,3.885,6.354,0.109},
|
||||
{2.132,7.006,7.136,2.641,1.882,5.943,7.273,7.691,2.88,0.564},
|
||||
{4.707,5.579,4.08,0.581,9.698,8.542,8.077,8.515,9.231,4.67},
|
||||
{8.304,7.559,8.567,0.322,7.128,8.392,1.472,8.524,2.277,7.826},
|
||||
{8.632,4.409,4.832,5.768,7.05,6.715,1.711,4.323,4.405,4.591},
|
||||
{4.887,9.112,0.17,8.967,9.693,9.867,7.508,7.77,8.382,6.74},
|
||||
{2.44,6.686,4.299,1.007,7.008,1.427,9.398,8.48,9.95,1.675},
|
||||
{6.306,8.583,6.084,1.138,4.350,3.134,7.853,6.061,7.457,2.258},
|
||||
{0.652,2.343,1.37,0.821,1.31,1.063,0.689,8.819,8.833,9.07},
|
||||
{5.558,1.272,5.756,9.857,2.279,2.764,1.284,1.677,1.244,1.234},
|
||||
{3.352,7.549,9.817,9.437,8.687,4.167,2.57,6.54,0.228,0.027},
|
||||
{8.798,0.88,2.37,0.168,1.701,3.68,1.231,2.39,2.499,0.064},
|
||||
{1.46,8.057,1.337,7.217,7.914,3.615,9.981,9.198,5.292,1.224},
|
||||
{0.432,8.645,8.774,0.249,8.081,7.461,4.416,0.652,4.002,4.644},
|
||||
{0.679,2.8,5.523,3.049,2.968,7.225,6.73,4.199,9.614,9.229},
|
||||
{4.263,1.074,7.286,5.599,8.291,5.2,9.214,8.272,4.398,4.506},
|
||||
{9.496,4.83,3.15,8.27,5.079,1.231,5.731,9.494,1.883,9.732},
|
||||
{4.138,2.562,2.532,9.661,5.611,5.5,6.886,2.341,9.699,6.5}};
|
||||
133
lab1/latex
Normal file
133
lab1/latex
Normal file
@@ -0,0 +1,133 @@
|
||||
|
||||
%%% Preamble
|
||||
\documentclass[paper=a4, fontsize=11pt]{scrartcl}
|
||||
\usepackage[T1]{fontenc}
|
||||
\usepackage{fourier}
|
||||
|
||||
\usepackage[english]{babel} % English language/hyphenation
|
||||
\usepackage[protrusion=true,expansion=true]{microtype}
|
||||
\usepackage{amsmath,amsfonts,amsthm} % Math packages
|
||||
\usepackage[pdftex]{graphicx}
|
||||
\usepackage{url}
|
||||
|
||||
|
||||
%%% Custom sectioning
|
||||
\usepackage{sectsty}
|
||||
\allsectionsfont{\centering \normalfont\scshape}
|
||||
|
||||
|
||||
%%% Custom headers/footers (fancyhdr package)
|
||||
\usepackage{fancyhdr}
|
||||
\pagestyle{fancyplain}
|
||||
\fancyhead{} % No page header
|
||||
\fancyfoot[L]{} % Empty
|
||||
\fancyfoot[C]{} % Empty
|
||||
\fancyfoot[R]{\thepage} % Pagenumbering
|
||||
\renewcommand{\headrulewidth}{0pt} % Remove header underlines
|
||||
\renewcommand{\footrulewidth}{0pt} % Remove footer underlines
|
||||
\setlength{\headheight}{13.6pt}
|
||||
|
||||
|
||||
%%% Equation and float numbering
|
||||
\numberwithin{equation}{section} % Equationnumbering: section.eq#
|
||||
\numberwithin{figure}{section} % Figurenumbering: section.fig#
|
||||
\numberwithin{table}{section} % Tablenumbering: section.tab#
|
||||
|
||||
|
||||
%%% Maketitle metadata
|
||||
\newcommand{\horrule}[1]{\rule{\linewidth}{#1}} % Horizontal rule
|
||||
|
||||
\title{
|
||||
%\vspace{-1in}
|
||||
\usefont{OT1}{bch}{b}{n}
|
||||
\normalfont \normalsize \textsc{Central Washington University} \\ [1pt]
|
||||
\normalfont \normalsize \textsc{CS 427} \\ [15pt]
|
||||
\horrule{0.5pt} \\[0.4cm]
|
||||
\huge Lab 1 \\
|
||||
\horrule{2pt} \\[0.5cm]
|
||||
}
|
||||
\author{
|
||||
\normalfont \normalsize
|
||||
Mitchell Hansen\\[-3pt] \normalsize
|
||||
\today
|
||||
}
|
||||
\date{}
|
||||
|
||||
|
||||
%%% Begin document
|
||||
\begin{document}
|
||||
\maketitle
|
||||
\section{Introduction}
|
||||
For our first lab, we took 15 functions from various optimization test suites and ran them for varying dimensionality and input. The goal being to determine the range of the functions output, it's standard deviation from input to input, average time to run the function, and other such tests.
|
||||
|
||||
\section {Methods}
|
||||
For each test, I generated an array of random integers using the C rand() function, seeded at the begginning of the application with srand() and the time. In future tests I will be utilizing the Mersenne Twister method of generating random numbers, which is far more random. After generating the random numbers, I passed each set into the functions at 10, 20, and 30 dimensionality intervals, logging what they return.
|
||||
|
||||
\section{Experimentation Results}
|
||||
\begin{center}
|
||||
\begin{tabular}{ | l | l | l | l | l | l |}
|
||||
\hline
|
||||
Function & Dimensionality & Mean & Median & Deviation & Avg. Time \\ \hline
|
||||
|
||||
schwefel & 10 & -1.36058 & -103.102 & 618.227 & 1.7575 \\ \hline
|
||||
& 20 & -5.22963 & 1366.82 & 863.447 & 2.7682 \\ \hline
|
||||
& 30 & -3.35496 & 441.352 & 1071.53 & 3.8081 \\ \hline
|
||||
de jong & 10 & 33264 & 40362 & 9402.19 & 0.7467 \\ \hline
|
||||
& 20 & 66737.3 & 51890 & 13442.9 & 0.7351 \\ \hline
|
||||
& 30 & 100044 & 95177 & 16432.6 & 0.3675 \\ \hline
|
||||
rosenbrok & 10 & 1.79827e+10 & 1.67874e+10 & 7.98429e+09 & 0.0835 \\ \hline
|
||||
& 20 & 3.78563e+10 & 2.30065e+10 & 1.16052e+10 & 1.76 \\ \hline
|
||||
& 30 & 5.81661e+10 & 5.26908e+10 & 1.43576e+10 & 1.0109 \\ \hline
|
||||
rastrigin & 10 & 58068.8 & 37720 & 17104 & 1.0163 \\ \hline
|
||||
& 20 & 231345 & 264520 & 47672.9 & 2.5908 \\ \hline
|
||||
& 30 & 521790 & 523500 & 89713.1 & 3.1789 \\ \hline
|
||||
griegwangk & 10 & 208.696 & 201.88 & 58.559 & 1.0334 \\ \hline
|
||||
& 20 & 417.247 & 502.07 & 82.0775 & 3.7124 \\ \hline
|
||||
& 30 & 626.906 & 633.49 & 101.588 & 4.0333 \\ \hline
|
||||
sine envelope sine wave & 10 & 7.484 & 8.42901 & 0.776108 & 1.3989 \\ \hline
|
||||
& 20 & 15.7985 & 14.7606 & 1.13035 & 3.3113 \\ \hline
|
||||
& 30 & 24.1052 & 26.4525 & 1.40737 & 5.025 \\ \hline
|
||||
stretched v sine wave & 10 & 30.0648 & 37.1503 & 5.56455 & 3.8074 \\ \hline
|
||||
& 20 & 63.5978 & 61.1979 & 8.1662 & 6.1452 \\ \hline
|
||||
& 30 & 96.8599 & 98.3275 & 9.95237 & 8.8907 \\ \hline
|
||||
ackleys one & 10 & 180.243 & 149.991 & 31.6898 & 2.0144 \\ \hline
|
||||
& 20 & 380.786 & 442.58 & 45.7357 & 5.0182 \\ \hline
|
||||
& 30 & 583.005 & 681.962 & 57.6042 & 7.0651 \\ \hline
|
||||
ackleys two & 10 & 152.968 & 152.204 & 4.22097 & 3.3884 \\ \hline
|
||||
& 20 & 322.851 & 326.451 & 6.31728 & 6.0586 \\ \hline
|
||||
& 30 & 492.805 & 498.597 & 7.74626 & 8.9194 \\ \hline
|
||||
egg holder & 10 & -28.1673 & -1334.07 & 872.855 & 2.2148 \\ \hline
|
||||
& 20 & -46.6518 & 718.965 & 1265.43 & 5.0378 \\ \hline
|
||||
& 30 & -120.776 & -2238.62 & 1563.34 & 7.0771 \\ \hline
|
||||
rana & 10 & 5.96939 & 33.1935 & 602.187 & 4.1041 \\ \hline
|
||||
& 20 & 17.3048 & -816.859 & 874.721 & 9.1242 \\ \hline
|
||||
& 30 & 3.58453 & -1667.75 & 1077.64 & 13.6967 \\ \hline
|
||||
pathological & 10 & 4.50048 & 4.50316 & 0.226241 & 2.0145 \\ \hline
|
||||
& 20 & 9.50768 & 9.77562 & 0.326493 & 3.9414 \\ \hline
|
||||
& 30 & 14.5061 & 14.8958 & 0.409931 & 5.4952 \\ \hline
|
||||
michalewicz & 10 & -0.0472368 & 0 & 0.535466 & 1.5615 \\ \hline
|
||||
& 20 & -0.0359855 & 0 & 0.773229 & 3.0854 \\ \hline
|
||||
& 30 & -0.038839 & 0 & 0.969944 & 4.5795 \\ \hline
|
||||
masters cosine wave & 10 & 0.0242817 & -2.82524 & 2.07645 & 2.4656 \\ \hline
|
||||
& 20 & 0.000543481 & 1.27202 & 3.02895 & 3.886 \\ \hline
|
||||
& 30 & -0.073613 & 2.43497 & 3.70944 & 5.1847 \\ \hline
|
||||
shekels foxholes & 10 & -3.18527 & 0 & 4.95254 & 4.8601 \\ \hline
|
||||
& 20 & -3.06529 & 0 & 4.60784 & 7.5344 \\ \hline
|
||||
& 30 & -3.39603 & -5.22145 & 5.05984 & 11.7375 \\ \hline
|
||||
|
||||
|
||||
\end{tabular}
|
||||
\end{center}
|
||||
|
||||
|
||||
\section{Analysis}
|
||||
Viewing the results of the experiments yields expected and unexpected results. First off, and probably the most easily predicted is the steady increase in computation time required for each function as measured in microseconds. Where steady increases in dimensionality yielded steady increases in computation time. Everything further from here was unexpected for me as I had no idea what to expect from these computations.
|
||||
|
||||
|
||||
For most of the functions, each steady increase in dimensionality was met with an equally steady increasing Mean, Median, and Standard Deviation. Notable exceptions to this are the Schwefel, Michalewicz, and Shekels Foxholes functions. Each either showed little to no change in values, or the values increased and decreased in a random manner.
|
||||
|
||||
I also graphed each of the 15 functions in 2D to view their behavior and found that the Michalewicz and Shekels Foxholes functions had a behavior very different than the other 13. The Michalewicz was similar to how it looks when viewing 3rd party sources, but the Shekels Foxholes function does not match 3rd party sources, which leads me to believe that the input data I had is weird, or I have the function wrong.
|
||||
|
||||
|
||||
%%% End document
|
||||
\end{document}
|
||||
611
lab1/main.cpp
Normal file
611
lab1/main.cpp
Normal file
@@ -0,0 +1,611 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <cmath>
|
||||
#include <map>
|
||||
#include <chrono>
|
||||
#include "SFML/Graphics.hpp"
|
||||
#include "mtrand.h"
|
||||
|
||||
|
||||
// In order to compile and run this project you're going to either comment out the SFML
|
||||
// code or install SFML which is trivial on *NIX.
|
||||
|
||||
// Just apt-get install libsfml-dev
|
||||
// or brew install sfml
|
||||
|
||||
|
||||
std::vector<double> c = {0.806,0.517,0.1,0.908,0.965,0.669,0.524,0.902,0.351,0.876,0.462,
|
||||
0.491,0.463,0.741,0.352,0.869,0.813,0.811,0.0828,0.964,0.789,0.360,0.369,
|
||||
0.992,0.332,0.817,0.632,0.883,0.608,0.326};
|
||||
|
||||
double a[][10] =
|
||||
{
|
||||
{9.681,0.667,4.783,9.095,3.517,9.325,6.544,0.211,5.122,2.02},
|
||||
{9.4,2.041,3.788,7.931,2.882,2.672,3.568,1.284,7.033,7.374},
|
||||
{8.025,9.152,5.114,7.621,4.564,4.711,2.996,6.126,0.734,4.982},
|
||||
{2.196,0.415,5.649,6.979,9.510,9.166,6.304,6.054,9.377,1.426},
|
||||
{8.074,8.777,3.467,1.863,6.708,6.349,4.534,0.276,7.633,1.567},
|
||||
{7.650,5.658,0.720,2.764,3.278,5.283,7.474,6.274,1.409,8.208},
|
||||
{1.256,3.605,8.623,6.905,4.584,8.133,6.071,6.888,4.187,5.448},
|
||||
{8.314,2.261,4.24,1.781,4.124,0.932,8.129,8.658,1.208,5.762},
|
||||
{0.226,8.858,1.42,0.954,1.622,4.698,6.228,9.096,0.972,7.637},
|
||||
{7.305,2.228,1.242,5.928,9.133,1.826,4.06,5.204,8.713,8.247},
|
||||
{0.652,7.027,0.508,4.876,8.807,4.632,5.808,6.937,3.291,7.016},
|
||||
{2.699,3.516,5.847,4.119,4.461,7.496,8.817,0.69,6.593,9.789},
|
||||
{8.327,3.897,2.017,9.57,9.825,1.15,1.395,3.885,6.354,0.109},
|
||||
{2.132,7.006,7.136,2.641,1.882,5.943,7.273,7.691,2.88,0.564},
|
||||
{4.707,5.579,4.08,0.581,9.698,8.542,8.077,8.515,9.231,4.67},
|
||||
{8.304,7.559,8.567,0.322,7.128,8.392,1.472,8.524,2.277,7.826},
|
||||
{8.632,4.409,4.832,5.768,7.05,6.715,1.711,4.323,4.405,4.591},
|
||||
{4.887,9.112,0.17,8.967,9.693,9.867,7.508,7.77,8.382,6.74},
|
||||
{2.44,6.686,4.299,1.007,7.008,1.427,9.398,8.48,9.95,1.675},
|
||||
{6.306,8.583,6.084,1.138,4.350,3.134,7.853,6.061,7.457,2.258},
|
||||
{0.652,2.343,1.37,0.821,1.31,1.063,0.689,8.819,8.833,9.07},
|
||||
{5.558,1.272,5.756,9.857,2.279,2.764,1.284,1.677,1.244,1.234},
|
||||
{3.352,7.549,9.817,9.437,8.687,4.167,2.57,6.54,0.228,0.027},
|
||||
{8.798,0.88,2.37,0.168,1.701,3.68,1.231,2.39,2.499,0.064},
|
||||
{1.46,8.057,1.337,7.217,7.914,3.615,9.981,9.198,5.292,1.224},
|
||||
{0.432,8.645,8.774,0.249,8.081,7.461,4.416,0.652,4.002,4.644},
|
||||
{0.679,2.8,5.523,3.049,2.968,7.225,6.73,4.199,9.614,9.229},
|
||||
{4.263,1.074,7.286,5.599,8.291,5.2,9.214,8.272,4.398,4.506},
|
||||
{9.496,4.83,3.15,8.27,5.079,1.231,5.731,9.494,1.883,9.732},
|
||||
{4.138,2.562,2.532,9.661,5.611,5.5,6.886,2.341,9.699,6.5}
|
||||
};
|
||||
|
||||
double schwefel(std::vector<double> input){
|
||||
|
||||
int upper_bound = 512;
|
||||
int lower_bound = -512;
|
||||
|
||||
double sum = 0;
|
||||
|
||||
for (int i = 0; i < input.size(); i++){
|
||||
sum += (-input[i]) * std::sin(std::sqrt(std::abs(input[i])));
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
|
||||
double first_de_jong(std::vector<double> input){
|
||||
|
||||
int upper_bound = 100;
|
||||
int lower_bound = -100;
|
||||
|
||||
double sum = 0;
|
||||
|
||||
for (int i = 0; i < input.size(); i++){
|
||||
sum += std::pow(input[i], 2);
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
double rosenbrock(std::vector<double> input){
|
||||
|
||||
int upper_bound = 100;
|
||||
int lower_bound = -100;
|
||||
|
||||
double sum = 0;
|
||||
|
||||
for (int i = 0; i < input.size() - 1; i++){
|
||||
sum += 100 * std::pow((std::pow(input[i], 2) - input[i + 1]), 2) + std::pow((1 - input[i]), 2);
|
||||
}
|
||||
|
||||
return sum;
|
||||
|
||||
}
|
||||
|
||||
double rastrigin(std::vector<double> input){
|
||||
|
||||
int upper_bound = 30;
|
||||
int lower_bound = -30;
|
||||
|
||||
double sum = 0;
|
||||
|
||||
for (int i = 0; i < input.size(); i++){
|
||||
sum += std::pow(input[i], 2) - 10 * std::cos(2 * M_PI * input[i]);
|
||||
}
|
||||
|
||||
sum *= 2 * input.size();
|
||||
|
||||
return sum;
|
||||
|
||||
}
|
||||
|
||||
double griewangk(std::vector<double> input){
|
||||
|
||||
int upper_bound = 500;
|
||||
int lower_bound = -500;
|
||||
|
||||
double sum = 0;
|
||||
|
||||
for (int i = 0; i < input.size(); i++){
|
||||
sum += std::pow(input[i], 2) / 4000;
|
||||
}
|
||||
|
||||
double product = 0;
|
||||
|
||||
for (int i = 0; i < input.size(); i++){
|
||||
product *= std::cos(input[i] / sqrt(i + 1));
|
||||
}
|
||||
|
||||
return 1 + sum - product;
|
||||
|
||||
}
|
||||
|
||||
|
||||
double sine_envelope_sine_wave(std::vector<double> input){
|
||||
|
||||
int upper_bound = 30;
|
||||
int lower_bound = -30;
|
||||
|
||||
double sum = 0;
|
||||
|
||||
for (int i = 0; i < input.size() - 1; i++){
|
||||
sum += 0.5 + (std::pow(std::sin(std::pow(input[i], 2) + std::pow(input[i + 1], 2) - 0.5), 2)) /
|
||||
(1 + 0.001 * (std::pow(input[i], 2) + std::pow(input[i + 1], 2)));
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
double stretched_v_sine_wave(std::vector<double> input){
|
||||
|
||||
int upper_bound = 30;
|
||||
int lower_bound = -30;
|
||||
|
||||
double sum = 0;
|
||||
|
||||
for (int i = 0; i < input.size() - 1; i++){
|
||||
sum += std::pow(std::pow(input[i], 2) + std::pow(input[i + 1], 2), 1.0 / 4) *
|
||||
std::pow(std::sin(50 * std::pow(std::pow(input[i], 2) + std::pow(input[i + 1], 2), 1.0 / 10)), 2) + 1;
|
||||
}
|
||||
|
||||
return sum;
|
||||
|
||||
}
|
||||
|
||||
|
||||
double ackleys_one(std::vector<double> input){
|
||||
|
||||
int upper_bound = 32;
|
||||
int lower_bound = -32;
|
||||
|
||||
double sum = 0;
|
||||
|
||||
for (int i = 0; i < input.size() - 1; i++){
|
||||
sum += (1.0 / pow(M_E, 0.2)) *
|
||||
std::sqrt(std::pow(input[i], 2) + std::pow(input[i + 1], 2)) +
|
||||
3 * std::cos(2 * input[i]) +
|
||||
std::sin(2 * input[i + 1]);
|
||||
}
|
||||
|
||||
return sum;
|
||||
|
||||
}
|
||||
|
||||
|
||||
double ackleys_two(std::vector<double> input){
|
||||
|
||||
int upper_bound = 32;
|
||||
int lower_bound = -32;
|
||||
|
||||
double sum = 0;
|
||||
|
||||
for (int i = 0; i < input.size() - 1; i++){
|
||||
sum += 20 + M_E -
|
||||
(20 / (std::pow(M_E, 0.2) * std::sqrt(((std::pow(input[i], 2) + std::pow(input[i+1], 2) + 1) / 2)))) -
|
||||
std::pow(M_E, 0.5 * std::cos(2 * M_PI * input[i]) + cos(2 * M_PI * input[i + 1]));
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
|
||||
|
||||
double egg_holder(std::vector<double> input){
|
||||
|
||||
int upper_bound = 500;
|
||||
int lower_bound = -500;
|
||||
|
||||
double sum = 0;
|
||||
|
||||
for (int i = 0; i < input.size() - 1; i++) {
|
||||
sum += -input[i] * std::sin(std::sqrt(abs(input[i] - input[i + 1] - 47))) -
|
||||
(input[i + 1] + 47) * std::sin(std::sqrt(std::abs(input[i + 1] + 47 + input[i] / 2)));
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
double rana(std::vector<double> input){
|
||||
|
||||
int upper_bound = 500;
|
||||
int lower_bound = -500;
|
||||
|
||||
double sum = 0;
|
||||
|
||||
for (int i = 0; i < input.size() - 1; i++) {
|
||||
sum += input[i] * std::sin(std::sqrt(std::abs(input[i + 1] - input[i] + 1))) *
|
||||
std::cos(std::sqrt(std::abs(input[i + 1] + input[i] + 1))) +
|
||||
(input[i + 1] + 1) *
|
||||
std::cos(std::sqrt(std::abs(input[i + 1] - input[i] + 1))) *
|
||||
std::sin(std::sqrt(std::abs(input[i + 1] + input[i] + 1)));
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
|
||||
double pathological(std::vector<double> input){
|
||||
|
||||
int upper_bound = 100;
|
||||
int lower_bound = -100;
|
||||
|
||||
double sum = 0;
|
||||
|
||||
for (int i = 0; i < input.size() - 1; i++) {
|
||||
sum += 0.5 +
|
||||
(std::pow(std::sin(std::sqrt(100 * std::pow(input[i], 2) + std::pow(input[i + 1], 2))), 2) - 0.5) /
|
||||
(1 + 0.001 * std::pow(std::pow(input[i], 2) - 2 * input[i] * input[i + 1] + std::pow(input[i + 1], 2), 2));
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
double michalewicz(std::vector<double> input){
|
||||
|
||||
int upper_bound = M_PI;
|
||||
int lower_bound = 0;
|
||||
|
||||
double sum = 0;
|
||||
|
||||
for (int i = 0; i < input.size() - 1; i++) {
|
||||
sum += std::sin(input[i]) * std::pow(std::sin(i * std::pow(input[i], 2) / M_PI), 20);
|
||||
}
|
||||
|
||||
return -sum;
|
||||
}
|
||||
|
||||
|
||||
double masters_cosine_wave(std::vector<double> input){
|
||||
|
||||
int upper_bound = 30;
|
||||
int lower_bound = -30;
|
||||
|
||||
double sum = 0;
|
||||
|
||||
for (int i = 0; i < input.size() - 1; i++) {
|
||||
sum += std::pow(M_E, -(1/8) * (std::pow(input[i], 2) + std::pow(input[i + 1], 2) + 0.5 * input[i + 1] * input[i])) *
|
||||
std::cos(4 * std::sqrt(std::pow(input[i], 2) + std::pow(input[i + 1], 2) + 0.5 * input[i] * input[i + 1]));
|
||||
}
|
||||
return -sum;
|
||||
}
|
||||
|
||||
double shekels_foxholes(std::vector<double> input){
|
||||
|
||||
int upper_bound = 10;
|
||||
int lower_bound = 0;
|
||||
|
||||
double sum = 0;
|
||||
|
||||
for (int i = 0; i < c.size() - 1; i++) {
|
||||
|
||||
double bottom_sum = 0;
|
||||
|
||||
for (int q = 0; q < input.size(); q++){
|
||||
bottom_sum = std::pow(input.at(q) - a[i][q], 2);
|
||||
}
|
||||
|
||||
sum += 1 / (bottom_sum + c[i]);
|
||||
}
|
||||
|
||||
return -sum;
|
||||
}
|
||||
|
||||
struct timer{
|
||||
std::chrono::high_resolution_clock::time_point t1;
|
||||
std::chrono::high_resolution_clock::time_point t2;
|
||||
void start(){t1 = std::chrono::high_resolution_clock::now();}
|
||||
void end(){t2 = std::chrono::high_resolution_clock::now();}
|
||||
double duration(){ return std::chrono::duration_cast<std::chrono::microseconds>( t2 - t1 ).count();}
|
||||
};
|
||||
|
||||
|
||||
double set_within(double val, double prior_upper, double prior_lower, double after_upper, double after_lower){
|
||||
return ((after_upper - after_lower) * (val - prior_lower) / (prior_upper - prior_lower)) + after_lower;
|
||||
}
|
||||
|
||||
struct function{
|
||||
|
||||
double (*function_pointer)(std::vector<double>);
|
||||
double range = 0;
|
||||
double upper_bound = 0;
|
||||
double lower_bound = 0;
|
||||
|
||||
timer t;
|
||||
|
||||
function(double (*func)(std::vector<double>), double upper_bound, double lower_bound){
|
||||
function_pointer = func;
|
||||
this->upper_bound = upper_bound;
|
||||
this->lower_bound = lower_bound;
|
||||
}
|
||||
|
||||
double compute_defined(std::vector<double> input){
|
||||
|
||||
for (auto v: input) {
|
||||
if (v >= lower_bound && v <= upper_bound) {
|
||||
return function_pointer(input);
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
enum Test_Data { MEAN, MEDIAN, DEVIATION, AVG_TIME, DIMENSIONALIY, UPPER_RANGE, LOWER_RANGE};
|
||||
|
||||
std::map<Test_Data, double> run_tests_random(int dimensionality, int permutations){
|
||||
|
||||
std::vector<double> times;
|
||||
std::vector<double> vals;
|
||||
|
||||
for (int i = 0; i < permutations; i++) {
|
||||
|
||||
std::vector<double> dimension_vals;
|
||||
|
||||
for (int i = 0; i < dimensionality; i++){
|
||||
dimension_vals.push_back(fmod(std::rand() , (upper_bound * 2)) + lower_bound);
|
||||
}
|
||||
|
||||
t.start();
|
||||
|
||||
vals.push_back(compute_defined(dimension_vals));
|
||||
|
||||
t.end();
|
||||
|
||||
times.push_back(t.duration());
|
||||
}
|
||||
|
||||
// Mean
|
||||
double mean = 0;
|
||||
for (double v: vals){
|
||||
mean += v;
|
||||
}
|
||||
mean /= vals.size();
|
||||
|
||||
// Median
|
||||
double median = vals[std::floor(vals.size() / 2)];
|
||||
|
||||
// Standard Deviation
|
||||
double sum = 0;
|
||||
for (double v: vals){
|
||||
sum += std::pow(v - mean, 2);
|
||||
}
|
||||
double deviation = std::sqrt(sum / vals.size());
|
||||
|
||||
// Time Mean
|
||||
double time_mean = 0;
|
||||
for (double v: times){
|
||||
time_mean += v;
|
||||
}
|
||||
time_mean /= times.size();
|
||||
|
||||
// Range
|
||||
std::sort(vals.begin(), vals.end());
|
||||
|
||||
double lower_range = vals.front();
|
||||
double upper_range = vals.back();
|
||||
|
||||
std::map<Test_Data, double> ret;
|
||||
|
||||
ret[Test_Data::DIMENSIONALIY] = dimensionality;
|
||||
ret[Test_Data::AVG_TIME] = time_mean;
|
||||
ret[Test_Data::DEVIATION] = deviation;
|
||||
ret[Test_Data::MEAN] = mean;
|
||||
ret[Test_Data::MEDIAN] = median;
|
||||
ret[Test_Data::UPPER_RANGE] = upper_range;
|
||||
ret[Test_Data::LOWER_RANGE] = lower_range;
|
||||
|
||||
std::cout << " & " << dimensionality << " & " << mean << " & " << median << " & " << deviation << " & " << time_mean << " \\\\ \\hline" << std::endl;
|
||||
|
||||
return ret;
|
||||
};
|
||||
|
||||
std::map<Test_Data, double> run_tests_defined(std::vector<double> input, int permutations){
|
||||
|
||||
std::vector<double> times;
|
||||
std::vector<double> vals;
|
||||
|
||||
for (int i = 0; i < permutations; i++) {
|
||||
|
||||
t.start();
|
||||
|
||||
vals.push_back(function_pointer(input));
|
||||
|
||||
t.end();
|
||||
|
||||
times.push_back(t.duration());
|
||||
}
|
||||
|
||||
// Mean
|
||||
double mean = 0;
|
||||
for (double v: vals){
|
||||
mean += v;
|
||||
}
|
||||
mean /= vals.size();
|
||||
|
||||
// Median
|
||||
double median = vals[std::floor(vals.size() / 2)];
|
||||
|
||||
// Standard Deviation
|
||||
double sum = 0;
|
||||
for (double v: vals){
|
||||
sum += std::pow(v - mean, 2);
|
||||
}
|
||||
double deviation = std::sqrt(sum / vals.size());
|
||||
|
||||
// Time Mean
|
||||
double time_mean = 0;
|
||||
for (double v: times){
|
||||
time_mean += v;
|
||||
}
|
||||
time_mean /= times.size();
|
||||
|
||||
// Range
|
||||
std::sort(vals.begin(), vals.end());
|
||||
|
||||
double lower_range = vals.front();
|
||||
double upper_range = vals.back();
|
||||
|
||||
std::map<Test_Data, double> ret;
|
||||
|
||||
ret[Test_Data::DIMENSIONALIY] = input.size();
|
||||
ret[Test_Data::AVG_TIME] = time_mean;
|
||||
ret[Test_Data::DEVIATION] = deviation;
|
||||
ret[Test_Data::MEAN] = mean;
|
||||
ret[Test_Data::MEDIAN] = median;
|
||||
ret[Test_Data::UPPER_RANGE] = upper_range;
|
||||
ret[Test_Data::LOWER_RANGE] = lower_range;
|
||||
|
||||
return ret;
|
||||
|
||||
};
|
||||
|
||||
|
||||
void draw(){
|
||||
|
||||
int bounds = fabs(upper_bound) + fabs(lower_bound);
|
||||
int window_xy = 1024;
|
||||
sf::RenderWindow window(sf::VideoMode(window_xy, window_xy), "Functions");
|
||||
sf::Uint8* pixel_array = new sf::Uint8[window_xy * window_xy * 4];
|
||||
sf::Texture texture;
|
||||
texture.create(window_xy, window_xy);
|
||||
sf::Sprite sprite(texture);
|
||||
|
||||
|
||||
double min = 9999999;
|
||||
double max = 0;
|
||||
|
||||
for (int i = 0; i < window_xy * window_xy * 4; i += 4) {
|
||||
|
||||
std::vector<double> position =
|
||||
{static_cast<double>(set_within(((i / 4) % window_xy) - window_xy/2, 512, -512, upper_bound, lower_bound)),
|
||||
static_cast<double>(set_within(((i / 4) / window_xy) - window_xy/2, 512, -512, upper_bound, lower_bound))};
|
||||
|
||||
auto res = static_cast<double>(compute_defined(position));
|
||||
if (res > max)
|
||||
max = res;
|
||||
if (res < min)
|
||||
min = res;
|
||||
}
|
||||
|
||||
for (int i = 0; i < window_xy * window_xy * 4; i += 4){
|
||||
|
||||
std::vector<double> position =
|
||||
{static_cast<double>(set_within(((i / 4) % window_xy) - window_xy/2, 512, -512, upper_bound, lower_bound)),
|
||||
static_cast<double>(set_within(((i / 4) / window_xy) - window_xy/2, 512, -512, upper_bound, lower_bound))};
|
||||
|
||||
auto res = static_cast<int>((((compute_defined(position) - min) * (16581375 - 0)) / (max - min)) + 0);
|
||||
|
||||
pixel_array[i + 0] = res & 0xff;
|
||||
pixel_array[i + 1] = (res>>8) & 0xff;
|
||||
pixel_array[i + 2] = (res>>16) & 0xff;
|
||||
pixel_array[i + 3] = 255;
|
||||
|
||||
//pixel_array[i + 0] = res;
|
||||
//pixel_array[i + 1] = 255;
|
||||
//pixel_array[i + 2] = 255;
|
||||
//pixel_array[i + 3] = 255;
|
||||
|
||||
}
|
||||
|
||||
texture.update(pixel_array);
|
||||
|
||||
while (window.isOpen()) {
|
||||
|
||||
sf::Event event;
|
||||
while (window.pollEvent(event)) {
|
||||
if (event.type == sf::Event::Closed)
|
||||
window.close();
|
||||
}
|
||||
|
||||
window.clear();
|
||||
window.draw(sprite);
|
||||
window.display();
|
||||
|
||||
}
|
||||
|
||||
delete pixel_array;
|
||||
};
|
||||
};
|
||||
|
||||
int main() {
|
||||
|
||||
MTRand r(time(NULL));
|
||||
std::cout << r();
|
||||
|
||||
|
||||
srand(time(NULL));
|
||||
|
||||
function schwefel_f(&schwefel, 512, -512);
|
||||
function de_jong_f(&first_de_jong, 100, -100);
|
||||
function rosenbrock_f(&rosenbrock, 100, -100);
|
||||
function rastrigin_f(&rastrigin, 30, -30);
|
||||
function griegwangk_f(&griewangk, 500, -500);
|
||||
function sine_envelope_sine_wave_f(&sine_envelope_sine_wave, 30, -30);
|
||||
function stretched_v_sine_wave_f(&stretched_v_sine_wave, 30, -30);
|
||||
function ackleys_one_f(&ackleys_one, 32, -32);
|
||||
function ackleys_two_f(&ackleys_two, 32, -32);
|
||||
function egg_holder_f(&egg_holder, 500, -500);
|
||||
function rana_f(&rana, 500, -500);
|
||||
function pathological_f(&pathological, 100, -100);
|
||||
function michalewicz_f(&michalewicz, M_PI, 0);
|
||||
function masters_cosine_wave_f(&masters_cosine_wave, 30, -30);
|
||||
function shekels_foxholes_f(&shekels_foxholes, 10, 0);
|
||||
|
||||
// shekels_foxholes_f.draw();
|
||||
// schwefel_f.draw();
|
||||
// de_jong_f.draw();
|
||||
// rosenbrock_f.draw();
|
||||
// rastrigin_f.draw();
|
||||
// griegwangk_f.draw();
|
||||
// sine_envelope_sine_wave_f.draw();
|
||||
// stretched_v_sine_wave_f.draw();
|
||||
// ackleys_one_f.draw();
|
||||
// ackleys_two_f.draw();
|
||||
// egg_holder_f.draw();
|
||||
// rana_f.draw();
|
||||
// pathological_f.draw();
|
||||
// michalewicz_f.draw();
|
||||
// masters_cosine_wave_f.draw();
|
||||
|
||||
std::string vals[] {
|
||||
"MEAN", "MEDIAN", "DEVIATION", "AVG_TIME", "DIMENSIONALIY", "UPPER_RANGE", "LOWER_RANGE"
|
||||
};
|
||||
|
||||
int permutations = 10000;
|
||||
|
||||
for (int i = 10; i < 40; i += 10) {
|
||||
|
||||
auto ret = schwefel_f.run_tests_random(i, permutations);
|
||||
ret = de_jong_f.run_tests_random(i, permutations);
|
||||
ret = rosenbrock_f.run_tests_random(i, permutations);
|
||||
ret = rastrigin_f.run_tests_random(i, permutations);
|
||||
ret = griegwangk_f.run_tests_random(i, permutations);
|
||||
ret = sine_envelope_sine_wave_f.run_tests_random(i, permutations);
|
||||
ret = stretched_v_sine_wave_f.run_tests_random(i, permutations);
|
||||
ret = ackleys_one_f.run_tests_random(i, permutations);
|
||||
ret = ackleys_two_f.run_tests_random(i, permutations);
|
||||
ret = egg_holder_f.run_tests_random(i, permutations);
|
||||
ret = rana_f.run_tests_random(i, permutations);
|
||||
ret = pathological_f.run_tests_random(i, permutations);
|
||||
ret = michalewicz_f.run_tests_random(i, permutations);
|
||||
ret = masters_cosine_wave_f.run_tests_random(i, permutations);
|
||||
ret = shekels_foxholes_f.run_tests_random(i, permutations);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
51
lab1/mtrand.cpp
Normal file
51
lab1/mtrand.cpp
Normal file
@@ -0,0 +1,51 @@
|
||||
// mtrand.cpp, see include file mtrand.h for information
|
||||
|
||||
#include "mtrand.h"
|
||||
// non-inline function definitions and static member definitions cannot
|
||||
// reside in header file because of the risk of multiple declarations
|
||||
|
||||
// initialization of static private members
|
||||
unsigned long MTRand_int32::state[n] = {0x0UL};
|
||||
int MTRand_int32::p = 0;
|
||||
bool MTRand_int32::init = false;
|
||||
|
||||
void MTRand_int32::gen_state() { // generate new state vector
|
||||
for (int i = 0; i < (n - m); ++i)
|
||||
state[i] = state[i + m] ^ twiddle(state[i], state[i + 1]);
|
||||
for (int i = n - m; i < (n - 1); ++i)
|
||||
state[i] = state[i + m - n] ^ twiddle(state[i], state[i + 1]);
|
||||
state[n - 1] = state[m - 1] ^ twiddle(state[n - 1], state[0]);
|
||||
p = 0; // reset position
|
||||
}
|
||||
|
||||
void MTRand_int32::seed(unsigned long s) { // init by 32 bit seed
|
||||
state[0] = s & 0xFFFFFFFFUL; // for > 32 bit machines
|
||||
for (int i = 1; i < n; ++i) {
|
||||
state[i] = 1812433253UL * (state[i - 1] ^ (state[i - 1] >> 30)) + i;
|
||||
// see Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier
|
||||
// in the previous versions, MSBs of the seed affect only MSBs of the array state
|
||||
// 2002/01/09 modified by Makoto Matsumoto
|
||||
state[i] &= 0xFFFFFFFFUL; // for > 32 bit machines
|
||||
}
|
||||
p = n; // force gen_state() to be called for next random number
|
||||
}
|
||||
|
||||
void MTRand_int32::seed(const unsigned long* array, int size) { // init by array
|
||||
seed(19650218UL);
|
||||
int i = 1, j = 0;
|
||||
for (int k = ((n > size) ? n : size); k; --k) {
|
||||
state[i] = (state[i] ^ ((state[i - 1] ^ (state[i - 1] >> 30)) * 1664525UL))
|
||||
+ array[j] + j; // non linear
|
||||
state[i] &= 0xFFFFFFFFUL; // for > 32 bit machines
|
||||
++j; j %= size;
|
||||
if ((++i) == n) { state[0] = state[n - 1]; i = 1; }
|
||||
}
|
||||
for (int k = n - 1; k; --k) {
|
||||
state[i] = (state[i] ^ ((state[i - 1] ^ (state[i - 1] >> 30)) * 1566083941UL)) - i;
|
||||
state[i] &= 0xFFFFFFFFUL; // for > 32 bit machines
|
||||
if ((++i) == n) { state[0] = state[n - 1]; i = 1; }
|
||||
}
|
||||
state[0] = 0x80000000UL; // MSB is 1; assuring non-zero initial array
|
||||
p = n; // force gen_state() to be called for next random number
|
||||
}
|
||||
|
||||
156
lab1/mtrand.h
Normal file
156
lab1/mtrand.h
Normal file
@@ -0,0 +1,156 @@
|
||||
// mtrand.h
|
||||
// C++ include file for MT19937, with initialization improved 2002/1/26.
|
||||
// Coded by Takuji Nishimura and Makoto Matsumoto.
|
||||
// Ported to C++ by Jasper Bedaux 2003/1/1 (see http://www.bedaux.net/mtrand/).
|
||||
// The generators returning floating point numbers are based on
|
||||
// a version by Isaku Wada, 2002/01/09
|
||||
//
|
||||
// Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions
|
||||
// are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright
|
||||
// notice, this list of conditions and the following disclaimer in the
|
||||
// documentation and/or other materials provided with the distribution.
|
||||
//
|
||||
// 3. The names of its contributors may not be used to endorse or promote
|
||||
// products derived from this software without specific prior written
|
||||
// permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
//
|
||||
// Any feedback is very welcome.
|
||||
// http://www.math.keio.ac.jp/matumoto/emt.html
|
||||
// email: matumoto@math.keio.ac.jp
|
||||
//
|
||||
// Feedback about the C++ port should be sent to Jasper Bedaux,
|
||||
// see http://www.bedaux.net/mtrand/ for e-mail address and info.
|
||||
|
||||
#ifndef MTRAND_H
|
||||
#define MTRAND_H
|
||||
|
||||
class MTRand_int32 { // Mersenne Twister random number generator
|
||||
public:
|
||||
// default constructor: uses default seed only if this is the first instance
|
||||
MTRand_int32() { if (!init) seed(5489UL); init = true; }
|
||||
// constructor with 32 bit int as seed
|
||||
MTRand_int32(unsigned long s) { seed(s); init = true; }
|
||||
// constructor with array of size 32 bit ints as seed
|
||||
MTRand_int32(const unsigned long* array, int size) { seed(array, size); init = true; }
|
||||
// the two seed functions
|
||||
void seed(unsigned long); // seed with 32 bit integer
|
||||
void seed(const unsigned long*, int size); // seed with array
|
||||
// overload operator() to make this a generator (functor)
|
||||
unsigned long operator()() { return rand_int32(); }
|
||||
// 2007-02-11: made the destructor virtual; thanks "double more" for pointing this out
|
||||
virtual ~MTRand_int32() {} // destructor
|
||||
protected: // used by derived classes, otherwise not accessible; use the ()-operator
|
||||
unsigned long rand_int32(); // generate 32 bit random integer
|
||||
private:
|
||||
static const int n = 624, m = 397; // compile time constants
|
||||
// the variables below are static (no duplicates can exist)
|
||||
static unsigned long state[n]; // state vector array
|
||||
static int p; // position in state array
|
||||
static bool init; // true if init function is called
|
||||
// private functions used to generate the pseudo random numbers
|
||||
unsigned long twiddle(unsigned long, unsigned long); // used by gen_state()
|
||||
void gen_state(); // generate new state
|
||||
// make copy constructor and assignment operator unavailable, they don't make sense
|
||||
MTRand_int32(const MTRand_int32&); // copy constructor not defined
|
||||
void operator=(const MTRand_int32&); // assignment operator not defined
|
||||
};
|
||||
|
||||
// inline for speed, must therefore reside in header file
|
||||
inline unsigned long MTRand_int32::twiddle(unsigned long u, unsigned long v) {
|
||||
return (((u & 0x80000000UL) | (v & 0x7FFFFFFFUL)) >> 1)
|
||||
^ ((v & 1UL) * 0x9908B0DFUL);
|
||||
// 2013-07-22: line above modified for performance according to http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/Ierymenko.html
|
||||
// thanks Vitaliy FEOKTISTOV for pointing this out
|
||||
}
|
||||
|
||||
inline unsigned long MTRand_int32::rand_int32() { // generate 32 bit random int
|
||||
if (p == n) gen_state(); // new state vector needed
|
||||
// gen_state() is split off to be non-inline, because it is only called once
|
||||
// in every 624 calls and otherwise irand() would become too big to get inlined
|
||||
unsigned long x = state[p++];
|
||||
x ^= (x >> 11);
|
||||
x ^= (x << 7) & 0x9D2C5680UL;
|
||||
x ^= (x << 15) & 0xEFC60000UL;
|
||||
return x ^ (x >> 18);
|
||||
}
|
||||
|
||||
// generates double floating point numbers in the half-open interval [0, 1)
|
||||
class MTRand : public MTRand_int32 {
|
||||
public:
|
||||
MTRand() : MTRand_int32() {}
|
||||
MTRand(unsigned long seed) : MTRand_int32(seed) {}
|
||||
MTRand(const unsigned long* seed, int size) : MTRand_int32(seed, size) {}
|
||||
~MTRand() {}
|
||||
double operator()() {
|
||||
return static_cast<double>(rand_int32()) * (1. / 4294967296.); } // divided by 2^32
|
||||
private:
|
||||
MTRand(const MTRand&); // copy constructor not defined
|
||||
void operator=(const MTRand&); // assignment operator not defined
|
||||
};
|
||||
|
||||
// generates double floating point numbers in the closed interval [0, 1]
|
||||
class MTRand_closed : public MTRand_int32 {
|
||||
public:
|
||||
MTRand_closed() : MTRand_int32() {}
|
||||
MTRand_closed(unsigned long seed) : MTRand_int32(seed) {}
|
||||
MTRand_closed(const unsigned long* seed, int size) : MTRand_int32(seed, size) {}
|
||||
~MTRand_closed() {}
|
||||
double operator()() {
|
||||
return static_cast<double>(rand_int32()) * (1. / 4294967295.); } // divided by 2^32 - 1
|
||||
private:
|
||||
MTRand_closed(const MTRand_closed&); // copy constructor not defined
|
||||
void operator=(const MTRand_closed&); // assignment operator not defined
|
||||
};
|
||||
|
||||
// generates double floating point numbers in the open interval (0, 1)
|
||||
class MTRand_open : public MTRand_int32 {
|
||||
public:
|
||||
MTRand_open() : MTRand_int32() {}
|
||||
MTRand_open(unsigned long seed) : MTRand_int32(seed) {}
|
||||
MTRand_open(const unsigned long* seed, int size) : MTRand_int32(seed, size) {}
|
||||
~MTRand_open() {}
|
||||
double operator()() {
|
||||
return (static_cast<double>(rand_int32()) + .5) * (1. / 4294967296.); } // divided by 2^32
|
||||
private:
|
||||
MTRand_open(const MTRand_open&); // copy constructor not defined
|
||||
void operator=(const MTRand_open&); // assignment operator not defined
|
||||
};
|
||||
|
||||
// generates 53 bit resolution doubles in the half-open interval [0, 1)
|
||||
class MTRand53 : public MTRand_int32 {
|
||||
public:
|
||||
MTRand53() : MTRand_int32() {}
|
||||
MTRand53(unsigned long seed) : MTRand_int32(seed) {}
|
||||
MTRand53(const unsigned long* seed, int size) : MTRand_int32(seed, size) {}
|
||||
~MTRand53() {}
|
||||
double operator()() {
|
||||
return (static_cast<double>(rand_int32() >> 5) * 67108864. +
|
||||
static_cast<double>(rand_int32() >> 6)) * (1. / 9007199254740992.); }
|
||||
private:
|
||||
MTRand53(const MTRand53&); // copy constructor not defined
|
||||
void operator=(const MTRand53&); // assignment operator not defined
|
||||
};
|
||||
|
||||
#endif // MTRAND_H
|
||||
|
||||
Reference in New Issue
Block a user