-
Notifications
You must be signed in to change notification settings - Fork 4
/
objective.m
60 lines (48 loc) · 3.5 KB
/
objective.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
(* ::Package:: *)
(************************************************************************)
(* This file was generated automatically by the Mathematica front end. *)
(* It contains Initialization cells from a Notebook file, which *)
(* typically will have the same name as this file except ending in *)
(* ".nb" instead of ".m". *)
(* *)
(* This file is intended to be loaded into the Mathematica kernel using *)
(* the package loading commands Get or Needs. Doing so is equivalent *)
(* to using the Evaluate Initialization Cells menu command in the front *)
(* end. *)
(* *)
(* DO NOT EDIT THIS FILE. This entire file is regenerated *)
(* automatically each time the parent Notebook file is saved in the *)
(* Mathematica front end. Any changes you make to this file will be *)
(* overwritten. *)
(************************************************************************)
(* ::Input::Initialization:: *)
Echo["Loaded objective.m"];
(* ::Input::Initialization:: *)
ClearAll[coefficient1];
coefficient1::usage="To normalize the payoff function, we set the first coefficient to either 1 or -1 coefficient1=1 (default) or coefficient=-1";
coefficient1=1;
(* ::Input::Initialization:: *)
(*********************************************************************************)
(* Objective function *)
(*********************************************************************************)
ClearAll[objective];
objective::usage="objective[dataArray,x1,x2,...,xn] defines the objective function to maximize the number of satisfied inequalities. An inequality is satisfied when the left side is weakly greater than the right side (>=).";
objective[dataArray_,b__?NumericQ]:=(*Defining as a pure function may speed this up*)
(++objectivecounter;Total@UnitStep@(*http://mathematica.stackexchange.com/a/9673/7966*)
(dataArray.Prepend[{b},N[coefficient1]]));
Information[objective,LongForm->False]
(* ::Input::Initialization:: *)
(*********************************************************************************)
(* Verbose Objective function *)(* to calculate number of satisfied inequalities per market *)
(*********************************************************************************)
ClearAll[objectiveV];
objectiveV::usage="objectiveV[dataArray,x1,x2,...,xn] is the verbose version of objective routine. It also uses groupIDs produced by CdataArray routine. It returns more information about how many inequalities are satisfied for each market. It is obviously slower than the plain objective and it is used as the final step after the maximization process.";
objectiveV[dataArray_,b__?NumericQ]:=Module[{dataarray,groupids,satisfiedineqs,totalineqs,stats},
dataarray=dataArray.Prepend[{b},N[coefficient1]];
groupids=Flatten@groupIDs;
totalineqs=(Tally@groupIDs)[[All,2]];
satisfiedineqs=Total[UnitStep[#]]&/@((GatherBy[Transpose[{dataarray,groupids}],Last])[[All,All,1]]);
stats=MapThread[Insert,{
{Range@Length@satisfiedineqs,satisfiedineqs,totalineqs,Chop@Round[100. satisfiedineqs/totalineqs,1]},{"Market no","Satisfied","Total","Percentage %"},Table[1,{4}]}]
];
Information[objectiveV,LongForm->False]