(* Content-type: application/vnd.wolfram.mathematica *) (*** Wolfram Notebook File ***) (* http://www.wolfram.com/nb *) (* CreatedBy='Mathematica 12.0' *) (*CacheID: 234*) (* Internal cache information: NotebookFileLineBreakTest NotebookFileLineBreakTest NotebookDataPosition[ 158, 7] NotebookDataLength[ 35671, 867] NotebookOptionsPosition[ 35243, 849] NotebookOutlinePosition[ 35651, 865] CellTagsIndexPosition[ 35608, 862] WindowFrame->Normal*) (* Beginning of Notebook Content *) Notebook[{ Cell[CellGroupData[{ Cell[TextData[StyleBox["Weighted incremental algorithm", FontColor->RGBColor[1, 0, 0]]], "Subsubsection", CellChangeTimes->{{3.73112424028771*^9, 3.7311242546173754`*^9}, { 3.8106047392458076`*^9, 3.8106047456549196`*^9}, {3.8131286886441045`*^9, 3.8131286920600576`*^9}, 3.8209872570601444`*^9, 3.821011326978313*^9, { 3.821011386608663*^9, 3.821011391156393*^9}}, ExpressionUUID -> "e66d6e93-d6b2-4c5b-9154-8959b5b959a5"], Cell[BoxData[ RowBox[{"\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{ StyleBox["Input", FontColor->RGBColor[1, 0, 0]], StyleBox[":", FontColor->RGBColor[1, 0, 0]], StyleBox[" ", FontColor->RGBColor[0, 0, 1]], StyleBox[ RowBox[{ RowBox[{"data", " ", "set", " ", "\[ScriptCapitalA]"}], " ", "\[Subset]", " ", SuperscriptBox["\[DoubleStruckCapitalR]", "n"]}], FontColor->RGBColor[0, 0, 1]]}], StyleBox[",", FontColor->RGBColor[0, 0, 1]], StyleBox[" ", FontColor->RGBColor[0, 0, 1]], StyleBox[ RowBox[{"data", " ", "weights", " ", "w"}], FontColor->RGBColor[0, 0, 1]], StyleBox[",", FontColor->RGBColor[0, 0, 1]], StyleBox[" ", FontColor->RGBColor[0, 0, 1]], StyleBox[ RowBox[{"initial", " ", "center", RowBox[{"(", "s", ")"}], " ", StyleBox["cen", FontSlant->"Italic"]}], FontColor->RGBColor[0, 0, 1]], StyleBox[",", FontSlant->"Italic", FontColor->RGBColor[0, 0, 1]], StyleBox[" ", FontSlant->"Italic", FontColor->RGBColor[0, 0, 1]], StyleBox[ RowBox[{ RowBox[{"number", " ", "of", " ", "iterations", " ", "K"}], ";", RowBox[{ RowBox[{ "Each", " ", "additional", " ", "center", " ", "is", " ", "obtained", " ", "by", " ", "solving", " ", "a", " ", "GOP", " ", "using", " ", RowBox[{ StyleBox["Compile", FontSlant->"Italic"], ".", " ", "In"}], " ", "case", " ", "when", " ", StyleBox["n", FontSlant->"Italic"]}], StyleBox[">", FontSlant->"Italic"], StyleBox["2", FontSlant->"Italic"]}]}], FontColor->RGBColor[0, 0, 1]], StyleBox[",", FontColor->RGBColor[0, 0, 1]], StyleBox[" ", FontColor->RGBColor[0, 0, 1]], StyleBox[ RowBox[{ RowBox[{ "one", " ", "has", " ", "to", " ", "adapt", " ", "the", " ", "minimizing", " ", RowBox[{"function", ".", " ", "Test"}]}], "-", RowBox[{"examples", " ", "9.2"}]}], FontColor->RGBColor[0, 0, 1]], StyleBox["\[Dash]", FontColor->RGBColor[0, 0, 1]], StyleBox[ RowBox[{ RowBox[{"9.4", " ", "can", " ", "be", " ", "used"}], ";"}], FontColor->RGBColor[0, 0, 1]]}], StyleBox["\[IndentingNewLine]", FontColor->RGBColor[0, 0, 1]], RowBox[{ RowBox[{ StyleBox["Output", FontColor->RGBColor[1, 0, 0]], StyleBox[":", FontColor->RGBColor[1, 0, 0]], StyleBox[" ", FontColor->RGBColor[0, 0, 1]], StyleBox[ RowBox[{"set", " ", "of", " ", "partitions"}], FontColor->RGBColor[0, 0, 1]]}], StyleBox[",", FontColor->RGBColor[0, 0, 1]], StyleBox[" ", FontColor->RGBColor[0, 0, 1]], StyleBox["CH", FontColor->RGBColor[0, 0, 1]], StyleBox[",", FontColor->RGBColor[0, 0, 1]], StyleBox[" ", FontColor->RGBColor[0, 0, 1]], StyleBox["DB", FontColor->RGBColor[0, 0, 1]], StyleBox[",", FontColor->RGBColor[0, 0, 1]], StyleBox[" ", FontColor->RGBColor[0, 0, 1]], StyleBox["SSWC", FontColor->RGBColor[0, 0, 1]], StyleBox[",", FontColor->RGBColor[0, 0, 1]], StyleBox[ RowBox[{ RowBox[{ RowBox[{"and", " ", "Dunn", " ", RowBox[{"indexes", ".", " ", "Each"}], " ", "partition", " ", "consists", " ", "of", " ", "clusters", " ", "with", " ", "corresponding", " ", "weights", " ", "associated", " ", "to", " ", "their", " ", RowBox[{"elements", ".", " ", "Graphical"}], " ", "depictions", " ", "of", " ", "each", " ", "iteration", " ", "are", " ", "presented", " ", "only", " ", "for", " ", "n"}], "=", "2"}], ";"}], FontColor->RGBColor[0, 0, 1]]}], StyleBox["\[IndentingNewLine]", FontColor->RGBColor[0, 0, 1]], RowBox[{ RowBox[{ StyleBox["Submodules", FontColor->RGBColor[1, 0, 0]], StyleBox[":", FontColor->RGBColor[1, 0, 0]], StyleBox[" ", FontColor->RGBColor[0, 0, 1]], StyleBox[ RowBox[{"CH", "[", "]"}], FontColor->RGBColor[0, 0, 1]]}], StyleBox[",", FontColor->RGBColor[0, 0, 1]], StyleBox[" ", FontColor->RGBColor[0, 0, 1]], StyleBox[ RowBox[{"DB", "[", "]"}], FontColor->RGBColor[0, 0, 1]], StyleBox[",", FontColor->RGBColor[0, 0, 1]], StyleBox[" ", FontColor->RGBColor[0, 0, 1]], StyleBox[ RowBox[{"SSWC", "[", "]"}], FontColor->RGBColor[0, 0, 1]], StyleBox[",", FontColor->RGBColor[0, 0, 1]], StyleBox[" ", FontColor->RGBColor[0, 0, 1]], StyleBox[ RowBox[{"Dunn", "[", "]"}], FontColor->RGBColor[0, 0, 1]], StyleBox[",", FontColor->RGBColor[0, 0, 1]], StyleBox[" ", FontColor->RGBColor[0, 0, 1]], StyleBox[ RowBox[{"DIRECT", "[", "]"}], FontColor->RGBColor[0, 0, 1]], StyleBox[",", FontColor->RGBColor[0, 0, 1]], StyleBox[" ", FontColor->RGBColor[0, 0, 1]], StyleBox[ RowBox[{ RowBox[{"WKMeans", "[", "]"}], ";"}], FontColor->RGBColor[0, 0, 1]], StyleBox[" ", FontColor->RGBColor[0, 0, 1]]}]}]}]], "Text", CellChangeTimes->{{3.7311239388490767`*^9, 3.7311241887665453`*^9}, { 3.731124262852821*^9, 3.7311242818390455`*^9}, {3.731124334688675*^9, 3.7311243931480875`*^9}, {3.731124464437002*^9, 3.7311245820590296`*^9}, { 3.731124626641944*^9, 3.7311246685996666`*^9}, 3.73112478187739*^9, 3.7311249716632557`*^9, 3.731125008229858*^9, {3.731131363164089*^9, 3.7311315307912507`*^9}, {3.8106047626816826`*^9, 3.810604763185858*^9}, { 3.810606445967278*^9, 3.810606530225013*^9}, {3.810606641466092*^9, 3.810606684833495*^9}, {3.810606749868683*^9, 3.810606752984932*^9}, { 3.8106068274532113`*^9, 3.810606866094061*^9}, {3.810606901083395*^9, 3.810607019637113*^9}, {3.810607054277118*^9, 3.8106071829172134`*^9}, { 3.8106072199097958`*^9, 3.8106072546405845`*^9}, 3.8106074697262*^9, { 3.8131282699269085`*^9, 3.813128270182798*^9}, {3.8131283217992973`*^9, 3.8131283433443713`*^9}, {3.8131285602764816`*^9, 3.813128631734671*^9}, { 3.813128716159551*^9, 3.8131287310664387`*^9}, {3.813128829720832*^9, 3.8131288437367177`*^9}, {3.8131288968861256`*^9, 3.8131288976456766`*^9}, {3.8208206004327154`*^9, 3.820820602339464*^9}, { 3.8208232348027654`*^9, 3.820823235970901*^9}, {3.8209872382550364`*^9, 3.820987241863156*^9}, {3.8209873121364293`*^9, 3.820987370577606*^9}, { 3.820987470767851*^9, 3.8209875036173415`*^9}, {3.8209875433950415`*^9, 3.8209876048601007`*^9}, {3.82098765240862*^9, 3.820987699634896*^9}, 3.8209877620138216`*^9, {3.820987851324072*^9, 3.820987889081056*^9}, { 3.820987919084846*^9, 3.8209879407206955`*^9}, {3.8209879711907043`*^9, 3.8209882843502274`*^9}, {3.821011449753599*^9, 3.8210114696073275`*^9}, { 3.821011610511365*^9, 3.8210116654232597`*^9}, {3.8210117015210137`*^9, 3.8210117532993703`*^9}, {3.821011792082283*^9, 3.821011851642008*^9}, { 3.8210119003088837`*^9, 3.8210119491817203`*^9}, {3.8210120600131407`*^9, 3.821012083221542*^9}, {3.821012125973072*^9, 3.821012128942088*^9}, { 3.8210121679764605`*^9, 3.8210121904921026`*^9}, {3.8210122763394556`*^9, 3.821012280565492*^9}, {3.8223103140877924`*^9, 3.8223103624714737`*^9}}, FontSize->12, ExpressionUUID -> "a16ffa28-7742-4592-bcc2-c1a697822085"], Cell[BoxData[ RowBox[{ RowBox[{"Inc", "[", RowBox[{"w_", ",", "A_", ",", "cen_", ",", "KK_", ",", RowBox[{"Ind_:", "0"}]}], "]"}], ":=", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", RowBox[{"k", ",", "centri", ",", "centriN", ",", RowBox[{"m", "=", RowBox[{"Length", "[", "A", "]"}]}], ",", RowBox[{"n", "=", RowBox[{"Length", "[", RowBox[{"A", "[", RowBox[{"[", "1", "]"}], "]"}], "]"}]}], ",", "dom"}], "}"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"AG", "=", RowBox[{"Table", "[", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{ RowBox[{"Min", "[", RowBox[{"A", "[", RowBox[{"[", RowBox[{"All", ",", "i"}], "]"}], "]"}], "]"}], "-", ".5"}], ",", RowBox[{ RowBox[{"Max", "[", RowBox[{"A", "[", RowBox[{"[", RowBox[{"All", ",", "i"}], "]"}], "]"}], "]"}], "+", ".5"}]}], "}"}], ",", RowBox[{"{", RowBox[{"i", ",", "n"}], "}"}]}], "]"}]}], ";", "\[IndentingNewLine]", "\t\t\t\t\t", RowBox[{"(*", " ", "Initialization", " ", "*)"}], "\[IndentingNewLine]", RowBox[{"centri", "=", "cen"}], ";", "\[IndentingNewLine]", RowBox[{"PART", "=", RowBox[{"{", "}"}]}], ";", RowBox[{"IndCH", "=", RowBox[{"IndDB", "=", RowBox[{"IndSSWC", "=", RowBox[{"IndDunn", "=", RowBox[{"IndH", "=", RowBox[{"{", "}"}]}]}]}]}]}], ";", "\[IndentingNewLine]", "\t\t\t\t", RowBox[{"(*", " ", RowBox[{"Iterative", " ", "procedure"}], " ", "*)"}], "\[IndentingNewLine]", RowBox[{"Do", "[", "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"Print", "[", RowBox[{"\"\<\\nTo existing \>\"", ",", RowBox[{"Length", "[", "centri", "]"}], ",", "\"\< centers one new is added\>\""}], "]"}], ";", "\[IndentingNewLine]", "\t", RowBox[{"(*", " ", RowBox[{ RowBox[{"Compile", " ", "in", " ", "the", " ", "case", " ", "n"}], "=", RowBox[{"2", " ", "-", " ", RowBox[{ "can", " ", "be", " ", "extended", " ", "to", " ", "an", " ", "arbitrary", " ", "n"}]}]}], " ", "*)"}], "\[IndentingNewLine]", RowBox[{"Clear", "[", RowBox[{"u", ",", "v"}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"FCompile", "=", RowBox[{"Compile", "[", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{"u", ",", "_Real"}], "}"}], ",", RowBox[{"{", RowBox[{"v", ",", "_Real"}], "}"}]}], "}"}], ",", "\[IndentingNewLine]", RowBox[{"Evaluate", "[", RowBox[{"Sum", "[", RowBox[{ RowBox[{ RowBox[{"w", "[", RowBox[{"[", "i", "]"}], "]"}], RowBox[{"Min", "[", RowBox[{"Append", "[", RowBox[{ RowBox[{"Table", "[", RowBox[{ RowBox[{ RowBox[{"Norm", "[", RowBox[{ RowBox[{"A", "[", RowBox[{"[", "i", "]"}], "]"}], "-", RowBox[{"centri", "[", RowBox[{"[", "s", "]"}], "]"}]}], "]"}], "^", "2"}], ",", RowBox[{"{", RowBox[{"s", ",", RowBox[{"Length", "[", "centri", "]"}]}], "}"}]}], "]"}], ",", RowBox[{ RowBox[{"Norm", "[", RowBox[{ RowBox[{"A", "[", RowBox[{"[", "i", "]"}], "]"}], "-", RowBox[{"{", RowBox[{"u", ",", "v"}], "}"}]}], "]"}], "^", "2"}]}], "]"}], "]"}]}], ",", RowBox[{"{", RowBox[{"i", ",", "m"}], "}"}]}], "]"}], " ", "]"}]}], "]"}]}], ";", "\t", "\[IndentingNewLine]", RowBox[{ RowBox[{"F2", "[", RowBox[{"u_", ",", "v_"}], "]"}], ":=", RowBox[{"Apply", "[", RowBox[{"FCompile", ",", RowBox[{"{", RowBox[{"u", ",", "v"}], "}"}]}], "]"}]}], ";", "\t", "\[IndentingNewLine]", "\t\t\t\t\t\t", RowBox[{"(*", " ", RowBox[{ RowBox[{ RowBox[{"Non", " ", "Compile", " ", "\[IndentingNewLine]", RowBox[{"F2", "[", RowBox[{"u_", ",", "v_"}], "]"}]}], ":=", RowBox[{"Sum", "[", RowBox[{ RowBox[{ RowBox[{"w", "[", RowBox[{"[", "i", "]"}], "]"}], RowBox[{"Min", "[", RowBox[{"Append", "[", RowBox[{ RowBox[{"Table", "[", RowBox[{ RowBox[{ RowBox[{"Norm", "[", RowBox[{ RowBox[{"A", "[", RowBox[{"[", "i", "]"}], "]"}], "-", RowBox[{"centri", "[", RowBox[{"[", "s", "]"}], "]"}]}], "]"}], "^", "2"}], ",", RowBox[{"{", RowBox[{"s", ",", RowBox[{"Length", "[", "centri", "]"}]}], "}"}]}], "]"}], ",", RowBox[{ RowBox[{"Norm", "[", RowBox[{ RowBox[{"A", "[", RowBox[{"[", "i", "]"}], "]"}], "-", RowBox[{"{", RowBox[{"u", ",", "v"}], "}"}]}], "]"}], "^", "2"}]}], "]"}], "]"}]}], ",", RowBox[{"{", RowBox[{"i", ",", "m"}], "}"}]}], "]"}]}], ";"}], "\[IndentingNewLine]", "*)"}], "\[IndentingNewLine]", "\t\t\t\t\t", RowBox[{"(*", " ", RowBox[{ RowBox[{"Applying", " ", "of", " ", "the", " ", "DIRECT"}], "-", "algorithm"}], " ", "*)"}], "\[IndentingNewLine]", RowBox[{"dom", "=", "AG"}], ";", "\[IndentingNewLine]", RowBox[{"minp", "=", RowBox[{ RowBox[{"DIRECT", "[", RowBox[{"F2", ",", "dom", ",", RowBox[{"DMin", "\[Rule]", RowBox[{"10.", "^", RowBox[{"(", RowBox[{"-", "3"}], ")"}]}]}], ",", RowBox[{"MaxIter", "\[Rule]", "50"}], ",", " ", RowBox[{"Eps", "\[Rule]", RowBox[{"10", "^", RowBox[{"(", RowBox[{"-", "4"}], ")"}]}]}]}], "]"}], "//", "Timing"}]}], ";", " ", RowBox[{"min", "=", RowBox[{"minp", "[", RowBox[{"[", "2", "]"}], "]"}]}], ";", RowBox[{"(*", " ", RowBox[{ RowBox[{"Print", "[", RowBox[{"\"\\"", ",", "min"}], "]"}], ";"}], " ", "*)"}], "\[IndentingNewLine]", RowBox[{"centriN", "=", RowBox[{"Append", "[", RowBox[{"centri", ",", RowBox[{"min", "[", RowBox[{"[", "1", "]"}], "]"}]}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"Ind", "\[NotEqual]", "0"}], ",", RowBox[{"Print", "[", RowBox[{"\"\\"", ",", "centriN"}], "]"}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"Ind", "\[NotEqual]", "0"}], ",", RowBox[{"slcen", "=", RowBox[{"ListPlot", "[", RowBox[{"centriN", ",", RowBox[{"PlotMarkers", "\[Rule]", RowBox[{"{", RowBox[{"\"\<\[SixPointedStar]\>\"", ",", "20"}], "}"}]}], ",", RowBox[{"PlotStyle", "\[Rule]", RowBox[{"{", RowBox[{"Gray", ",", RowBox[{"PointSize", "[", ".025", "]"}], ",", RowBox[{"Opacity", "[", ".7", "]"}]}], "}"}]}]}], "]"}]}]}], "]"}], ";", "\[IndentingNewLine]", "\t\t\t\t\t", RowBox[{"(*", " ", RowBox[{"Call", " ", "WKMeans"}], " ", "*)"}], "\[IndentingNewLine]", RowBox[{"t1", "=", RowBox[{"Timing", "[", RowBox[{"sol", "=", RowBox[{"WKMeans", "[", RowBox[{"w", ",", "A", ",", "centriN"}], "]"}]}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"k", "=", RowBox[{"Length", "[", RowBox[{"sol", "[", RowBox[{"[", "1", "]"}], "]"}], "]"}]}], ";", " ", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"Ind", "\[NotEqual]", "0"}], ",", RowBox[{"Print", "[", RowBox[{"\"\\"", ",", RowBox[{"sol", "[", RowBox[{"[", "2", "]"}], "]"}]}], "]"}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"Ind", "\[NotEqual]", "0"}], ",", RowBox[{"Print", "[", RowBox[{"\"\\"", ",", RowBox[{"{", RowBox[{ RowBox[{"WF", "[", RowBox[{"sol", "[", RowBox[{"[", "1", "]"}], "]"}], "]"}], ",", RowBox[{"WG", "[", RowBox[{"sol", "[", RowBox[{"[", "1", "]"}], "]"}], "]"}]}], "}"}], ",", "\"\<; mj: \>\"", ",", RowBox[{"Table", "[", RowBox[{ RowBox[{"Length", "[", RowBox[{"sol", "[", RowBox[{"[", RowBox[{"1", ",", "j"}], "]"}], "]"}], "]"}], ",", RowBox[{"{", RowBox[{"j", ",", RowBox[{"Length", "[", "PI", "]"}]}], "}"}]}], "]"}]}], "]"}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"Ind", "\[NotEqual]", "0"}], ",", RowBox[{"Print", "[", RowBox[{"\"\